小小聊天室Python代碼實(shí)現(xiàn)
相對(duì)于Java方式的聊天室,Python同樣可以做得到。而且可以做的更加的優(yōu)雅。想必少了那么多的各種流的Python Socket,你一定會(huì)喜歡的。
至于知識(shí)點(diǎn)相關(guān)的內(nèi)容,這里就不多說(shuō)了。
UDP方式
服務(wù)器端
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/7/7' # __Desc__ = 創(chuàng)建一個(gè)簡(jiǎn)單的套接字監(jiān)聽(tīng)請(qǐng)求 import socket HOST = '192.168.59.255' PORT = 9998 s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(('',PORT)) print '套接字已啟動(dòng)!' while True: data,addr = s.recvfrom(1024) print addr,str(' : ')+data
客戶端
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/7/7' # __Desc__ = socket的客戶端的簡(jiǎn)單實(shí)現(xiàn) import socket PORT = 9998 HOST = '192.168.59.255' s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) words = raw_input('Client:') while words != 'quit': s.sendto(words,(HOST,PORT)) words = raw_input('Client:') s.close()
是不是很簡(jiǎn)單啊。我們需要注意的就是socket的第二個(gè)參數(shù)為SOCK_DGRAM。因?yàn)檫@和TCP方式的SOCK_STREAM 有所不同。
TCP方式
服務(wù)器端
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/7/7' # __Desc__ = 簡(jiǎn)單的tcpsocket的實(shí)現(xiàn) from socket import * from time import ctime HOST = '' PORT = 9999 BUFFERSIZE = 1024 ADDRESS = (HOST,PORT) s = socket(AF_INET,SOCK_STREAM) s.bind(ADDRESS) s.listen(5) while True: print 'Waiting for clients cennect!' tcpclient,addr = s.accept() print 'Connected By ',addr while True: try: data = tcpclient.recv(BUFFERSIZE) except Exception,e: print e.message tcpclient.close() break if not data: print "No Data received!" break senddata = 'Hi,you send me:[%s]%s'%(ctime(),data.encode('utf8')) tcpclient.send(senddata.encode('utf8')) print addr,' Says:',ctime(),data.encode('utf8') tcpclient.close() s.close()
客戶端
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/7/7' # __Desc__ = 簡(jiǎn)單的tcp socket客戶端的實(shí)現(xiàn) from socket import * class TcpClient: # HOST = 'localhost' PORT = 9999 HOST = '192.168.59.225' BUFFSIZ = 1024 ADDR = (HOST,PORT) def __init__(self): self.client = socket(AF_INET,SOCK_STREAM) self.client.connect((self.HOST,self.PORT)) while True: senddata = raw_input('>>>') if not senddata: print 'Please input some words!\n>>>' continue if senddata == "quit": break self.client.send(senddata.encode('utf8')) recvdata = self.client.recv(self.BUFFSIZ) if not recvdata: break print recvdata.encode('utf8') if __name__ == "__main__": client = TcpClient()
TCP方式演示結(jié)果:(注意先開(kāi)啟服務(wù)器端)
服務(wù)器端
D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/sockettest/SimpleTCPServer.py
Waiting for clients cennect!
Connected By ('192.168.59.225', 63095)
('192.168.59.225', 63095) Says: Thu Jul 07 16:01:10 2016 Hello World
('192.168.59.225', 63095) Says: Thu Jul 07 16:01:15 2016 haode
No Data received!
Waiting for clients cennect!
客戶端
D:\Software\Python2\python.exe E:/Code/Python/MyTestSet/sockettest/SimpleTcpClient.py
>>>Hello World
Hi,you send me:[Thu Jul 07 16:01:10 2016]Hello World
>>>
Please input some words!
>>>
>>>haode
Hi,you send me:[Thu Jul 07 16:01:15 2016]haode
>>>quit
Process finished with exit code 0
總結(jié)
簡(jiǎn)單的使用TCP或者是UDP確實(shí)很容易,然而要想更好的利用這兩個(gè)協(xié)議,就需要好好的設(shè)計(jì)一番了。
這里我想強(qiáng)調(diào)的是,注意tcp和udp創(chuàng)建套接字時(shí)指定的參數(shù)即可。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python實(shí)現(xiàn)多人聊天室
- python socket多線程通訊實(shí)例分析(聊天室)
- Python實(shí)現(xiàn)基于C/S架構(gòu)的聊天室功能詳解
- Python socket C/S結(jié)構(gòu)的聊天室應(yīng)用實(shí)現(xiàn)
- python編寫(xiě)簡(jiǎn)易聊天室實(shí)現(xiàn)局域網(wǎng)內(nèi)聊天功能
- python實(shí)現(xiàn)簡(jiǎn)單多人聊天室
- 基于python實(shí)現(xiàn)聊天室程序
- Python實(shí)現(xiàn)的使用telnet登陸聊天室實(shí)例
- Python socket實(shí)現(xiàn)簡(jiǎn)單聊天室
- python實(shí)現(xiàn)簡(jiǎn)易聊天室(Linux終端)
相關(guān)文章
使用PyTorch實(shí)現(xiàn)隨機(jī)搜索策略
這篇文章主要介紹了使用PyTorch實(shí)現(xiàn)隨機(jī)搜索策略,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07Pytorch backward報(bào)錯(cuò)2次訪問(wèn)計(jì)算圖需要retain_graph=True的情況詳解
這篇文章主要介紹了Pytorch backward報(bào)錯(cuò)2次訪問(wèn)計(jì)算圖需要retain_graph=True的情況,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python 實(shí)時(shí)調(diào)取攝像頭的示例代碼
這篇文章主要介紹了python 實(shí)時(shí)調(diào)取攝像頭的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11python實(shí)現(xiàn)上傳文件到linux指定目錄的方法
這篇文章主要介紹了python實(shí)現(xiàn)上傳文件到linux指定目錄的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-01-01