server下的方法:bind() , listen(), accept(), recv(), send(bytes(String)), sendall(), client的方法:connect(), recv(), send(), sendall()### 建立通信的過程:創建socket,綁定i ...
server下的方法:bind() , listen(), accept(), recv(), send(bytes(String)), sendall(),
client的方法:connect(), recv(), send(), sendall()
### 建立通信的過程:創建socket,綁定ip和埠,監聽,接收連接socket,用接受到的socket進行通訊。然後才能發送連接數據
server.py
import socket sk = socket.socket() # 創建對象,構造函數裡面的參數,一般都不需要參數 # family:AF_INET-伺服器之間的通信,AF_UNIX-Unix不同進程之間的通信 type:SOCK_STREAM-TCP連接,SOCK_Dgram-UDP連接 address = ('127.0.0.1',8888) sk.bind(address) sk.listen(3) # 讓多少個通信進行等待 # print(sk.accept()) # 等待連接,收到的是一個元組,裡面有個 socket對象,以及客戶端的ip和埠 # (<socket.socketfd=420,family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM,proto=0,laddr=('127.0.0.1',8888),raddr=('127.0.0.1',50510)>, ('127.0.0.1',50510)) conn,addr = sk.accept() # 會在這裡阻塞,就是會在這裡等著別人連接 ########註意這裡用的socket對象 conn.send(bytes('不要幻想了','utf8')) # 不能發送String類型,要發送byte類型server.py
伺服器通過conn.close()關掉通訊
client.py
1 import socket 2 sk = socket.socket() 3 address = ('127.0.0.1',8888) 4 sk.connect(address) 5 data = sk.recv(1024) 6 print(str(data,'utf8'))client.py
客戶端關閉自己的socket就可以關閉連接
#######通訊改進一#######
每人發一條信息,不能夠停止,停止可以加個判斷
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 if __name__ == '__main__': 9 sk = get_server_socket() 10 print('waiting...') 11 conn,addr = sk.accept() 12 while True: 13 data = conn.recv(1024) 14 print(str(data,'utf8')) 15 inp = input('>>>') 16 conn.send(bytes(inp,'utf8')) 17 print('waiting...') 18 conn.close()server.py
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == '__main__': 8 sk = connect_server() 9 while True: 10 inp = input('>>>') 11 sk.send(bytes(inp,'utf8')) 12 print('waiting...') 13 data = sk.recv(1024) 14 print(str(data,'utf8')) 15 sk.close()client.py
##########通訊改進二################
客戶端能正常退出
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 if__name__ == '__main__': 9 sk = get_server_socket() 10 print('waiting...') 11 conn,addr = sk.accept() 12 while True: 13 data = conn.recv(1024) 14 print(str(data,'utf8')) 15 if not data: 16 print('waitconnect...') 17 conn, addr = sk.accept() 18 continue 19 inp = input('>>>') 20 conn.send(bytes(inp,'utf8')) 21 print('waiting...') 22 conn.close()server.py
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == '__main__': 8 sk = connect_server() 9 while True: 10 inp = input('>>>') 11 if inp == 'exit': 12 break 13 sk.send(bytes(inp,'utf8')) 14 print('waiting...') 15 data = sk.recv(1024) 16 print(str(data,'utf8')) 17 18 sk.close()client
#############通訊改進三#############################
當一個客戶端退出後,能夠接受其他客戶端的信息
server.py
1 import socket 2 def get_server_socket(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.bind(server_address) 6 sk.listen(5) 7 return sk 8 def get_conn(sk): 9 print('waitconnect...') 10 conn, addr = sk.accept() 11 return conn 12 if __name__ == '__main__': 13 sk = get_server_socket() 14 conn = get_conn(sk) 15 while True: 16 try: 17 data = conn.recv(1024) 18 ##Linux這裡不會報錯,如果強行關閉了conn,data就會變成空,也就是說Linux的直接不try就行了。 19 except Exception as e: 20 conn = get_conn(sk) 21 print(str(data,'utf8')) 22 if not data: 23 conn = get_conn(sk) 24 continue 25 inp = input('>>>') 26 conn.send(bytes(inp,'utf8')) 27 print('waiting...') 28 conn.close()View Code
client.py
1 import socket 2 def connect_server(): 3 sk = socket.socket() 4 server_address = ('127.0.0.1',8888) 5 sk.connect(server_address) 6 return sk 7 if __name__ == '__main__': 8 sk = connect_server() 9 while True: 10 inp = input('>>>') 11 if inp == 'exit': 12 break 13 sk.send(bytes(inp,'utf8')) 14 print('waiting...') 15 data = sk.recv(1024) 16 print(str(data,'utf8')) 17 sk.close()client.py