1. 程式人生 > >python socket程式設計簡要

python socket程式設計簡要

socket


tcp/ip send, recv
udp

family address
AF.INET ipv4
AF.INET6
AF.UNIX 。local
socket protocol type
sock.SOCK_STREAM tcp/ip
socket.SOCK_DGRAM   資料報式socket , for UDP


服務端
server = socket.socket(AF.INET,sock.SOCK_STREAM)
server.bind(localhost,9999)
server.listen()
while True:
conn,addr = server.accept() #阻塞
while True:
print("new conn",addr)
data = conn.recv(1024) #8192 #recv預設是阻塞的

if not data:
break #客戶端已斷開, conn.recv收到的就都是空資料,
print(data)
conn.send(data.upper())
client
client = socket.socket()
client.connect(serverip, 9999 )
client.send(data)
client.send(data)
client.recv(dat)