Python一個(gè)簡(jiǎn)單的通信程序(客戶端 服務(wù)器)
功能是從客戶端向服務(wù)發(fā)送一個(gè)字符串, 服務(wù)器收到后將字符串重新發(fā)送給客戶端,同時(shí),在連接建立之后,服務(wù)器可以向客戶端發(fā)送任意多的字符串
客戶端:
10.248.27.23是我電腦的IP
import socket, sys host = '10.248.27.23' # host = raw_input("Plz imput destination IP:") # data = raw_input("Plz imput what you want to submit:") port = 51423 s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: s.connect((host, port)) except socket.gaierror, e: print "Address-related error connecting to server: %s" %e sys.exit(1) except socket.error, e: print "Connection error: %s" %e sys.exit(1) data = raw_input("Plz imput what you want to submit:") s.send(data) s.shutdown(1) print "Submit Complete" while 1: buf = s.recv(1024) sys.stdout.write(buf)
服務(wù)器:
import socket, traceback host = '' port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) print "done" while 1: #when connect error happen, skip the error try: ClientSock, ClientAddr = s.accept() except KeyboardInterrupt: raise except: traceback.print_exc() continue #Get informaion form client and reply try: print "Get connect from ", ClientSock.getpeername() data = ClientSock.recv(1024) print "The information we get is %s" % str(data) ClientSock.sendall("I`ve got the information: ") ClientSock.sendall(data) while 1: str = raw_input("What you want to say:") ClientSock.sendall(str) ClientSock.sendall('\n') except (KeyboardInterrupt ,SystemError): raise except: traceback.print_exc() #Clocs socket try: ClientSock.close() except KeyboardInterrupt: raise except: traceback.print_exc()
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- PythonPC客戶端自動(dòng)化實(shí)現(xiàn)原理(pywinauto)
- python中的socket實(shí)現(xiàn)ftp客戶端和服務(wù)器收發(fā)文件及md5加密文件
- python mqtt 客戶端的實(shí)現(xiàn)代碼實(shí)例
- python使用多線程編寫tcp客戶端程序
- 基于Python的ModbusTCP客戶端實(shí)現(xiàn)詳解
- python實(shí)現(xiàn)websocket的客戶端壓力測(cè)試
- python3+PyQt5 創(chuàng)建多線程網(wǎng)絡(luò)應(yīng)用-TCP客戶端和TCP服務(wù)器實(shí)例
- python如何創(chuàng)建TCP服務(wù)端和客戶端
- python搭建服務(wù)器實(shí)現(xiàn)兩個(gè)Android客戶端間收發(fā)消息
- Python 實(shí)現(xiàn)簡(jiǎn)單的客戶端認(rèn)證
相關(guān)文章
python 遺傳算法求函數(shù)極值的實(shí)現(xiàn)代碼
今天小編就為大家分享一篇python 遺傳算法求函數(shù)極值的實(shí)現(xiàn)代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02python 使用while循環(huán)輸出*組成的菱形實(shí)例
這篇文章主要介紹了python 使用while循環(huán)輸出*組成的菱形實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python list數(shù)據(jù)等間隔抽取并新建list存儲(chǔ)的例子
今天小編就為大家分享一篇python list數(shù)據(jù)等間隔抽取并新建list存儲(chǔ)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-11-11Python編寫運(yùn)維進(jìn)程文件目錄操作實(shí)用腳本示例
這篇文章主要為大家介紹了Python編寫實(shí)用運(yùn)維進(jìn)程文件目錄的操作腳本示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python中的魔術(shù)方法Magic?Methods使用實(shí)例全面指南
在Python中,魔術(shù)方法Magic?Methods是一種特殊的方法,它們以雙下劃線開(kāi)頭和結(jié)尾,如__init__、__str__等,這些方法允許定制類的行為,使得對(duì)象更具有靈活性和可定制性,本文將深入探討Python中一些常用的魔術(shù)方法,以及如何使用它們來(lái)定制類與對(duì)象2024-01-01Python?LeNet網(wǎng)絡(luò)詳解及pytorch實(shí)現(xiàn)
LeNet主要用來(lái)進(jìn)行手寫字符的識(shí)別與分類,并在美國(guó)的銀行中投入了使用。本文主要為大家詳細(xì)介紹了LetNet以及通過(guò)pytorch實(shí)現(xiàn)LetNet,感興趣的小伙伴可以學(xué)習(xí)一下2021-11-11