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使用多線程編寫(xiě)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如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲(chóng)
這篇文章主要介紹了Python如何使用隊(duì)列方式實(shí)現(xiàn)多線程爬蟲(chóng),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
Python使用cx_Oracle庫(kù)連接Oracle數(shù)據(jù)庫(kù)指南
這篇文章主要為大家介紹了Python使用cx_Oracle庫(kù)連接Oracle數(shù)據(jù)庫(kù)指南,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
TensorFlow神經(jīng)網(wǎng)絡(luò)優(yōu)化策略學(xué)習(xí)
這篇文章主要介紹了TensorFlow神經(jīng)網(wǎng)絡(luò)優(yōu)化策略2018-03-03
Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法分析
這篇文章主要介紹了Python多進(jìn)程方式抓取基金網(wǎng)站內(nèi)容的方法,結(jié)合實(shí)例形式分析了Python多進(jìn)程抓取網(wǎng)站內(nèi)容相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下2019-06-06

