python中的socket套接字用于實(shí)現(xiàn)網(wǎng)絡(luò)通信
tcp套接字的使用流程如下:
- 創(chuàng)建套接字對(duì)象 --- clientSocket = socket(AF_INET,SOCK_STREAM)
- 連接服務(wù)器,指定服務(wù)器的ip地址和端口(參數(shù)是個(gè)元組)------------clientSocket.connect(('192.168.99.173',9999))
- 發(fā)送信息 -------------clientSocket.send(msg.encode('utf8'))
- 接收信息 ----------------msg = clientSocket.recv(1024)
我們先實(shí)現(xiàn)一個(gè)tcp的客戶端
由于socket在接收數(shù)據(jù)時(shí)默認(rèn)為堵塞狀態(tài),當(dāng)接收數(shù)據(jù)是進(jìn)程就刮起了史侣,這時(shí)就無(wú)法發(fā)送信息钝侠,要實(shí)現(xiàn)全雙工的通信趁啸,需要給接收數(shù)據(jù)和發(fā)送數(shù)據(jù)各自分配一個(gè)線程诱贿。
from socket import *
from threading import Thread
def sendMsg(clientSocket):
while True:
msg = input('>>')
clientSocket.send(msg.encode('utf8'))
def recvMsg(clientSocket):
while True:
msg = clientSocket.recv(1024)
print('\r>>%s'%msg.decode('utf8'))
def main():
clientSocket = socket(AF_INET,SOCK_STREAM)
clientSocket.connect(('192.168.99.173',9999))
tr = Thread(target=recvMsg,args=(clientSocket,)) #將套接字作為參數(shù)傳給新線程克滴,各自的線程中分別執(zhí)行收,發(fā)數(shù)據(jù)
ts = Thread(target=sendMsg,args=(clientSocket,))
tr.start()
ts.start()
if __name__ == '__main__':
main()
接下來(lái)實(shí)現(xiàn)服務(wù)端
服務(wù)端的套接字比較特殊搓蚪,因?yàn)槭菫榭蛻舳朔?wù),所以要確定下來(lái)ip地址和端口號(hào)以便客戶端訪問(wèn)丁鹉,然后服務(wù)器還要能生成新的客服套接字單獨(dú)為一個(gè)客戶端服務(wù)妒潭,利用多進(jìn)程就可以生成多個(gè)同時(shí)工作的客服socket了。
服務(wù)器的套接字工作流程如下:
- 創(chuàng)建主套接字 --------serverSocket = socket(AF_INET,SOCK_STREAM)
- 綁定ip和端口 --------------------- serverSocket.bind(('',9999))
- 改為被動(dòng)套接字 ------------------------------serverSocket.listen(5)
- 檢測(cè)客戶端連接 揣钦,若連接則返回 新的服務(wù)套接字和客戶端地址ip----- newSocket,destAdr = serverSocket.accept()
- 每個(gè)服務(wù)套接字進(jìn)行與客戶端的數(shù)據(jù)交換(在各自的進(jìn)程里)
# 要求
#1. 使用tcp協(xié)議通信
#2. 使用多進(jìn)程配合多線程配合多線程的方式實(shí)現(xiàn)多個(gè)全雙工的對(duì)話雳灾。
#實(shí)現(xiàn)
#1. 創(chuàng)建tcp的套接字,綁定拂盯,監(jiān)聽(tīng)佑女,變?yōu)楸粍?dòng)
#2. 每收到一次請(qǐng)求返回新的套接字,同時(shí)創(chuàng)建子進(jìn)程谈竿,用來(lái)單獨(dú)用新套接字對(duì)話
#3. 子進(jìn)程中創(chuàng)建兩個(gè)線程团驱,用來(lái)收發(fā)數(shù)據(jù)
import os
from socket import *
from multiprocessing import Process
from threading import Thread,local
import time
def worker(newSocket,destAdr):
print('創(chuàng)建子會(huì)話進(jìn)程成功..')
#local_school = local() #創(chuàng)建ThreadLocal對(duì)象,用來(lái)儲(chǔ)存各線程的局部變量
ts = Thread(target=sendMsg,args=(newSocket,))
tr = Thread(target=recvMsg,args=(newSocket,))
ts.start()
tr.start()
ts.join()
tr.join()
def sendMsg(newSocket):
print('發(fā)送進(jìn)程準(zhǔn)備完畢空凸!')
print(os.getpid())
while True:
msg = str( os.getpid())
newSocket.send(msg.encode('utf8'))
time.sleep(5)
def ps_is_end():
print('子進(jìn)程結(jié)束')
def recvMsg(newSocket):
print('接受進(jìn)程準(zhǔn)備完畢嚎花!')
while True:
msg = newSocket.recv(1024)
if msg.decode('utf8')!= '': #在ubantu中測(cè)試當(dāng)客戶端連接關(guān)閉時(shí)會(huì)自動(dòng)發(fā)送‘’,而在windows下運(yùn)行卻會(huì)報(bào)錯(cuò)
print('\r>>%s'%msg.decode('utf8'))
else:
print('%d下限了'%os.getpid())
newSocket.close()
break
def main():
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',9999))
serverSocket.listen(5)
while True:
newSocket,destAdr = serverSocket.accept()
p = Process(target=worker,args=(newSocket,destAdr,))
p.start()
newSocket.close()#拷貝到了新的進(jìn)程中呀洲,這里的可以刪掉了
serverSocket.close()
if __name__ == '__main__':
main()
試一下效果紊选,先啟動(dòng)服務(wù)器啼止,再啟動(dòng)客戶端,然后客戶端發(fā)條信息