Udp实现省略编码

class My_Socket(socket.socket):

    def __init__(self, encoding='utf-8'):
        self.encoding = encoding
        super(My_Socket, self).__init__(type=socket.SOCK_DGRAM)

    def mysendto(self, msg, addr):
        return self.sendto(msg.encode(self.encoding), addr)

    def myrecvfrom(self, num):
        data, addr = self.recvfrom(num)
        return data.decode(self.encoding), addr

 

import my_UDP

sk = my_UDP.My_Socket()

while 1:

    msg = input('>>>')


    sk.mysendto(msg,('127.0.0.1', 8080))



    data, addr = sk.myrecvfrom(1024)

    print(data)
client端
import my_UDP


sk = my_UDP.My_Socket()

sk.bind(('127.0.0.1', 8080))

while 1:

    data, addr = sk.myrecvfrom(1024)

    print(data)

    msg = input('>>>')

    sk.mysendto(msg, addr)
server端

 

posted @ 2018-03-14 14:58  小学弟-  阅读(106)  评论(0编辑  收藏  举报