python多线程聊天(模拟qq)

from socket import *
from threading import Thread
#收数据然后打印
def recvData():
while True:
recvInfo = udpsocket.recvfrom(1024)
print(’>>%s:%s’%(recvInfo[1],recvInfo[0].decode(‘gb2312’)))

#发数据
def sendData():
while True:
sendInfo = input("<<")
udpsocket.sendto(sendInfo.encode(‘gb2312’),(destIp,destPort))

udpsocket = None
destIp = ‘’
destPort = 0

def main():
global udpsocket
global destIp
global destPort
destIp = input(“对方ip:”)
destPort = int(input(‘对方端口号’))
udpsocket = socket(AF_INET,SOCK_DGRAM)
udpsocket.bind((’’,4567))

tr = Thread(target=recvData)
ts = Thread(target=sendData)
tr.start()
ts.start()
tr.join()
ts.join()

if name == ‘main’:
main()

你可能感兴趣的:(python)