Python Socket編程之多線程聊天室
更新時間:2018年07月28日 08:54:02 作者:zyaphone
這篇文章主要為大家詳細(xì)介紹了Python Socket編程之多線程聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文為大家分享了Python多線程聊天室,是一個Socket,兩個線程,一個是服務(wù)器,一個是客戶端。
最近公司培訓(xùn),要寫個大富翁的小程序,準(zhǔn)備做個服務(wù)器版的,先練練手。
代碼:
#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()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python socket多線程通訊實(shí)例分析(聊天室)
- Python socket實(shí)現(xiàn)簡單聊天室
- Python聊天室?guī)Ы缑鎸?shí)現(xiàn)的示例代碼(tkinter,Mysql,Treading,socket)
- python socket 聊天室實(shí)例代碼詳解
- python socket實(shí)現(xiàn)聊天室
- Python基于Socket實(shí)現(xiàn)簡單聊天室
- 基于Python socket實(shí)現(xiàn)簡易網(wǎng)絡(luò)聊天室
- python使用socket制作聊天室詳細(xì)源碼(可以直接運(yùn)行)
相關(guān)文章
運(yùn)行django項(xiàng)目指定IP和端口的方法
今天小編就為大家分享一篇運(yùn)行django項(xiàng)目指定IP和端口的方法。具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05對python3標(biāo)準(zhǔn)庫httpclient的使用詳解
今天小編就為大家分享一篇對python3標(biāo)準(zhǔn)庫httpclient的使用詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12使用Python實(shí)現(xiàn)Excel表格轉(zhuǎn)圖片
在數(shù)據(jù)處理與信息分享過程中,Excel表格作為一種強(qiáng)大的數(shù)據(jù)管理工具被廣泛應(yīng)用,這篇文章主要為大家詳細(xì)介紹了如何使用Python將Excel表格轉(zhuǎn)換為圖片,需要的可以參考下2024-04-04Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()
Pandas具有很多強(qiáng)大的功能,transform就是其中之一,利用它可以高效地匯總數(shù)據(jù)且不改變數(shù)據(jù)行數(shù),下面這篇文章主要給大家介紹了關(guān)于Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()的相關(guān)資料,需要的朋友可以參考下2023-01-01