这篇文章将为大家详细讲解有关Python中怎么利用Socket编程实现多线程聊天室,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
#coding = utf-8
import socket
import threading
class UdpServer(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.address = ('127.0.0.1', 10000)
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.s.bind(self.address)
self.stop_flag = False
def recieve_msg(self):
(data, addr) = self.s.recvfrom(2048)
if data:
print 'recieve data from', addr
print data
def run(self):
while not self.stop_flag:
self.recieve_msg()
def stop(self):
self.stop_flag = True
class UdpClient(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.address = ('127.0.0.1', 10001)
self.s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.stop_flag = False
def send_msg(self):
data = raw_input()
if not data:
print 'Wrong inpiut'
return
else:
self.s.sendto(data, self.address)
def run(self):
while not True:
self.send_msg()
def stop(self):
self.stop_flag = True
def main():
t1 = UdpServer()
t2 = UdpClient()
t1.start()
t2.start()
if __name__ == '__main__':
main()
关于Python中怎么利用Socket编程实现多线程聊天室就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。