python網(wǎng)絡(luò)編程socket實現(xiàn)服務(wù)端、客戶端操作詳解
本文實例講述了python網(wǎng)絡(luò)編程socket實現(xiàn)服務(wù)端、客戶端操作。分享給大家供大家參考,具體如下:
本文內(nèi)容: socket介紹 TCP: 服務(wù)端 客戶端 UDP: 服務(wù)端 客戶端首發(fā)時間:2018-02-08 01:14
修改:
2018-03-20 :重置了布局,增加了UDP什么是socket: socket又稱'套接字',應(yīng)用程序通常通過'套接字'向網(wǎng)絡(luò)發(fā)出請求或者應(yīng)答網(wǎng)絡(luò)請求。 網(wǎng)絡(luò)上的兩個程序通過一個雙向的通信連接實現(xiàn)數(shù)據(jù)的交換,這個連接的一端稱為一個socket。 socket就像電話線插口,只有電話線插上了,才能通信。 python中使用socket來進(jìn)行網(wǎng)絡(luò)連接傳輸 TCP: 如果使用socket模塊來創(chuàng)建TCP客戶端和服務(wù)端: 首發(fā)需要導(dǎo)入模塊:import socket 然后創(chuàng)建過程在下面 服務(wù)端:TCP服務(wù)端一般需要下面幾個操作:建立,綁定IP地址和端口,監(jiān)聽端口,等待連接,接收數(shù)據(jù),傳輸數(shù)據(jù) ,關(guān)閉連接
建立:server=socket.socket(socket.AF_INET, socket.SOCK_STREAM) 【參數(shù)默認(rèn)就是socket.AF_INET, socket.SOCK_STREAM】 綁定端口:server.bind((’IP地址’,端口)),【地址和端口號是一個 tuple 】 監(jiān)聽:server.listen() 接受連接: conn,addr=server.accept(),返回值是一個連接實例和一個地址,地址是連接過來的客戶端地址,而數(shù)據(jù)操作要利用這個連接實例 傳輸數(shù)據(jù):conn.send(data),【傳輸?shù)臄?shù)據(jù)必須是字節(jié)流,所以對字符串?dāng)?shù)據(jù)需要使用encode() 】 接收數(shù)據(jù)read:conn.recv(size),【傳輸?shù)臄?shù)據(jù)必須是字節(jié)流,size是接收的字節(jié)數(shù),如果需要轉(zhuǎn)成Unicode,需要使用decode() 】 關(guān)閉連接close:close()import socketserver=socket.socket()#建立socketserver.bind((’localhost’,1234))#綁定server.listen()#監(jiān)聽print('開始等待。。。')conn,addr=server.accept()#接收連接print('連接成功')data=conn.recv(1024)#接收數(shù)據(jù)print(data.decode())conn.send(data)#發(fā)送數(shù)據(jù)server.close()#關(guān)閉連接print('--------------------')上述代碼存在一個問題:只能接受一次連接,連接結(jié)束后,服務(wù)端socket將關(guān)閉,更改成不立即關(guān)閉能等待下一個連接的:
#服務(wù)器端import socketserver = socket.socket()server.bind((’localhost’,1234)) #綁定ip和端口server.listen(5) #監(jiān)聽while True: print('開始等待') conn, addr = server.accept() print(conn, addr) print('客戶端連接') while True: data = conn.recv(1024) print('recv:',data) if not data: #當(dāng)data=0時為真 print('連接斷開...') break conn.send(data)server.close()
注:上述代碼中在linux中正常運行,在windows中會報錯!
如果要在windows中運行,需要捕獲異常:
#服務(wù)器端import socketserver = socket.socket()server.bind((’localhost’,1234)) #綁定ip和端口server.listen(5) #監(jiān)聽while True: print('開始等待') conn, addr = server.accept() print(conn, addr) print('客戶端連接') while True: try: data = conn.recv(1024) print('recv:',data) if not data: #當(dāng)data=0時為真print('連接斷開...')break conn.send(data) except ConnectionResetError as e: print(e) breakserver.close() 客戶端:
TCP客戶端一般需要下面幾個操作:建立socket,連接遠(yuǎn)程socket,傳輸數(shù)據(jù) ,接收數(shù)據(jù),關(guān)閉連接
建立:client=socket.socket() 連接:client.connect((’IP地址’,端口)),其中地址和端口號是一個 tuple 傳輸數(shù)據(jù):client.send(data),傳輸?shù)臄?shù)據(jù)必須是字節(jié)流,所以對字符串?dāng)?shù)據(jù)需要使用encode() 接收數(shù)據(jù)recv:client.recv(size),傳輸?shù)臄?shù)據(jù)是字節(jié)流,如果需要轉(zhuǎn)成Unicode,需要使用decode() 關(guān)閉連接close:close()import socketclient=socket.socket()#建立socketclient.connect((’localhost’,1234))#連接client.send('你好'.encode())#發(fā)送數(shù)據(jù)data=client.recv(1024)#接收數(shù)據(jù)print(data.decode())client.close()#關(guān)閉連接上述代碼存在一個問題:只能發(fā)送一次數(shù)據(jù),發(fā)生完數(shù)據(jù)就會斷開連接,改成可以多次發(fā)送數(shù)據(jù),不自動斷開的【前提是服務(wù)端能接收多次】:
import socketclient=socket.socket()client.connect((’localhost’,1234))while True: cmd=input('>>') if len(cmd)==0: continue client.send(cmd.encode()) cmd_res=client.recv(1024) print(cmd_res.decode())client.close() UDP: 服務(wù)端:
UDP服務(wù)端通常有以下幾個操作:創(chuàng)建socket,綁定端口,傳輸數(shù)據(jù),接收數(shù)據(jù)
創(chuàng)建socket:server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 綁定端口:server.bind(addr),【addr是一個元組,內(nèi)容為(地址,端口)】 接收數(shù)據(jù):data,client_addr=server.recvfrom(1024) 傳輸數(shù)據(jù):server.sendto(data,client_addr)import socketimport timeserver=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)server.bind(('localhost',1234))start_time=time.time()while True: data,addr=server.recvfrom(1024) print(data,addr) server.sendto('hello'.encode(),addr) time.sleep(1) if time.time()-start_time>30: breakserver.close() 客戶端:
UDP客戶端通常有以下幾個操作:創(chuàng)建socket,傳輸數(shù)據(jù),接收數(shù)據(jù)
創(chuàng)建socket:client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) 傳輸數(shù)據(jù):server.sendto(data,addr),【addr是一個元組,內(nèi)容為(地址,端口)】 接收數(shù)據(jù):data,server_addr=client.recvfrom(1024)import socket,timeclient=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)addr=('localhost',1234)start_time=time.time()while True: client.sendto(time.ctime().encode(),addr) data,addr= client.recvfrom(1024) print(data) time.sleep(1) if time.time()-start_time>30: breakclient.close()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章:
