網(wǎng)絡(luò)服務(wù) 套接字 向網(wǎng)絡(luò)發(fā)起請(qǐng)求或者等待網(wǎng)絡(luò)響應(yīng)
Socket
SocketServer
Demo 服務(wù)器端
import socket
#創(chuàng)建socket對(duì)象
serversocket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
#獲取本地地址
host = socket.gethostname()
#設(shè)置端口號(hào)
port = 9921
serversocket.bind((host,port))
#最大鏈接數(shù)
serversocket.listen(5)
while True:
#建立客戶端鏈接
clientsocket, addr = serversocket.accept()
print("鏈接地址:{%s}".format(addr))
#響應(yīng)
clientsocket.send("Hello boy")
clientsocket.close()
客戶端
import socket
#創(chuàng)建socket對(duì)象
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#獲取本地地址
host = socket.gethostname()
#設(shè)置鏈接端口號(hào)
port = 9921
c.connect((host,port))
#最大接收字節(jié)數(shù),返回信息內(nèi)容
msg = c.recv(1024)
c.close()
print(msg)