zoukankan      html  css  js  c++  java
  • 03python 基于UDP的socket连接(python网络编程)

            sever:            


     
     
     
    12
     
     
     
     
     
    1
    import socket
    2
    
    
    3
    
    
    4
    sk = socket.socket(type=socket.SOCK_DGRAM)
    5
    sk.bind(('127.0.0.1', 8080))
    6
    
    
    7
    msg, addr = sk.recvfrom(1024)
    8
    print(msg.decode('utf-8'))
    9
    
    
    10
    sk.sendto(b'bye', addr)
    11
    
    
    12
    sk.close()
    13
    
    
    14
    # UDPde server 不需要进行监听也不需要建立连接
    15
    # 在启动服务之后只能被动的等待客户端发送消息过来
    16
    # 客户端发送消息的同时还会自带地址信息
    17
    消息回复的时候不仅需要发送消息,还需要把自己的地址发送出去
     
     
     
            client:        

     
     
     
    12
     
     
     
     
     
    1
    import socket
    2
    
    
    3
    
    
    4
    sk = socket.socket(type=socket.SOCK_DGRAM)
    5
    ip_port = ('127.0.0.1', 8080)
    6
    
    
    7
    sk.sendto(b'hello', ip_port)
    8
    ret, addr = sk.recvfrom(1024)
    9
    print(ret.decode('utf-8'))
    10
    
    
    11
    sk.close()
    12
    
    
     
     

     
            qq  server        

     
     
     
    16
     
     
     
     
     
    1
    import socket
    2
    
    
    3
    
    
    4
    sk = socket.socket(type=socket.SOCK_DGRAM)
    5
    sk.bind(('127.0.0.1', 8080))
    6
    
    
    7
    while True:
    8
        msg, addr = sk.recvfrom(1024)
    9
        print(msg.decode('utf-8'))
    10
        if msg.decode('utf-8') == 'bey':
    11
            sk.sendto(b'bey', addr)
    12
            break
    13
        info = input('>>>').encode('utf-8')
    14
        sk.sendto(info, addr)
    15
    
    
    16
    sk.close()
     
     
     
            qq client 1        

     
     
     
    x
     
     
     
     
     
    1
    import socket
    2
    
    
    3
    sk = socket.socket(type=socket.SOCK_DGRAM)
    4
    ip_port = ('127.0.0.1', 8080)
    5
    while True:
    6
        info = input('>>>').encode('utf-8')
    7
        sk.sendto(info, ip_port)
    8
        msg, addr = sk.recvfrom(1024)
    9
        print(msg.decode('utf-8'))
    10
        if msg.decode('utf-8') == 'bey':
    11
            sk.sendto(b'bey', addr)
    12
            break
    13
    
    
    14
    sk.close()
    15
    
    
     
     
     
            qq client 2        

     
     
     
    x
     
     
     
     
     
    1
    import socket
    2
    
    
    3
    sk = socket.socket(type=socket.SOCK_DGRAM)
    4
    ip_port = ('127.0.0.1', 8080)
    5
    while True:
    6
        info = input('tiger:')
    7
        info = '来自tiger的消息:%s' % info。encode('utf-8')
    8
        sk.sendto(info, ip_port)
    9
        msg, addr = sk.recvfrom(1024)
    10
        print(msg.decode('utf-8'))
    11
        if msg.decode('utf-8') == 'bey':
    12
            sk.sendto(b'bey', addr)
    13
            break
    14
    
    
    15
    sk.close()
     
     
     
     
  • 相关阅读:
    python从可迭代对象中取值
    python中可迭代对象和列表
    python中字典生成式
    Redis源码解析之跳跃表(一)
    Redis高可用集群
    Redis主从&哨兵集群搭建
    Java并发之ThreadPoolExecutor源码解析(三)
    Java并发之ThreadPoolExecutor源码解析(二)
    Java并发之ThreadPoolExecutor源码解析(一)
    并发编程之JMM&Volatile(三)
  • 原文地址:https://www.cnblogs.com/pontoon/p/10242831.html
Copyright © 2011-2022 走看看