ip、port、等。。。
addressfamily:可以選擇af_inet(用於internet程序間通訊,這個是ipv4(ipv6是什麼,待查))或者af_unix(用於同一臺機器程序間通訊),實際工作中常用af_inet
傳送資料的流程:
建立套接字
傳送資料
關閉接收資料的流程:
建立套接字
繫結本地自己的資訊(ip和port)
接收資料
關閉注意:
1(傳送資料)
import socket
def send_eg1():
# 建立乙個udp套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
# 可以用網路除錯助手來接收。
# 可以使用套接字收發資料
# udp_socket.sendto('hahah', 對方的ip和port)
# 上述寫法會出錯,要用bytes的方式傳送,故要在傳送內容上加b
udp_socket.sendto(b'hahah', ("192.168.1.2", 8080))
# 關閉套接字
udp_socket.close()
def send_eg2():
# 建立乙個套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
send_data = input("請輸入要傳送的資料。")
udp_socket.sendto(send_data.encode('utf-8'), ("192.168.1.2", 8080))
# 關閉套接字
udp_socket.close()
def send_eg3():
# 建立乙個套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
while true:
send_data = input("請輸入要傳送的資料。")
if send_data == 'exit':
break
udp_socket.sendto(send_data.encode('utf-8'), ('127.0.0.1', 7788))
# 關閉套接字
print('byebye!')
udp_socket.close()
def receive_eg1():
# 1.建立套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
# 2.繫結乙個本地資訊
localaddr = ("", 7788)
udp_socket.bind(localaddr)
# 3.接收資料
recv_data = udp_socket.recvfrom(1024) # 1024表示本次接收的最大位元組數
# 4. 列印接收到的資料
print(recv_data)
# 5.關閉套接字
udp_socket.close()
if __name__ == "__main__":
func = ("exit", send_eg1, send_eg2, send_eg3, receive_eg1)
chosefunc = int(input("please enter which function to do (1:send_eg1, 2:send_eg2, 3:send_eg3, 4:receive_eg1): "))
# print(type(func[chosefunc]))
if chosefunc>0 and chosefunc<5:
func[chosefunc]()
else:
print("error input!")
結果:
2(接收資料和傳送資料)
import socket
def send_eg1():
# 建立乙個udp套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
# 可以用網路除錯助手來接收。
# 可以使用套接字收發資料
# udp_socket.sendto('hahah', 對方的ip和port)
# 上述寫法會出錯,要用bytes的方式傳送,故要在傳送內容上加b
udp_socket.sendto(b'hahah', ("192.168.1.2", 8080))
# 關閉套接字
udp_socket.close()
def send_eg2():
# 建立乙個套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
send_data = input("請輸入要傳送的資料。")
udp_socket.sendto(send_data.encode('utf-8'), ("192.168.1.2", 8080))
# 關閉套接字
udp_socket.close()
def send_eg3():
# 建立乙個套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
while true:
send_data = input("請輸入要傳送的資料。")
udp_socket.sendto(send_data.encode('utf-8'), ('127.0.0.1', 7788))
if send_data == 'exit':
break
# 關閉套接字
print('byebye!')
udp_socket.close()
def receive_eg1():
# 1.建立套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
# 2.繫結乙個本地資訊
localaddr = ("", 7788) # 乙個原組
udp_socket.bind(localaddr)
# 3.接收資料
recv_data = udp_socket.recvfrom(1024) # 1024表示本次接收的最大位元組數
# 4. 列印接收到的資料
print(recv_data)
# 5.關閉套接字
udp_socket.close()
def receive_eg2():
# 1.建立套接字
udp_socket = socket.socket(socket.af_inet, socket.sock_dgram)
# 2.繫結乙個本地資訊
localaddr = ("", 7788) # 乙個元祖
udp_socket.bind(localaddr)
# 3.接收資料
while true:
# rece_data 中儲存的是乙個元祖(接收到的資料,(傳送方的ip,port))
recv_data = udp_socket.recvfrom(1024) # 1024表示本次接收的最大位元組數
# 將這個元祖進行拆開
recv_msg = recv_data[0] # 儲存接收到的資料
recv_sdaddr = recv_data[1] # 儲存傳送方的位址資訊
# 4.列印接收到的資料
print("%s:%s"%(str(recv_sdaddr), recv_msg.decode("utf-8"))) # 注意這裡的編碼解碼方式,要和傳送端對應!
if recv_msg.decode("utf-8") == "exit":
print('byebye!')
break
# 5.關閉套接字
udp_socket.close()
if __name__ == "__main__":
func = ("exit", send_eg1, send_eg2, send_eg3, receive_eg1, receive_eg2)
chosefunc = int(input("please enter which function to do (1:send_eg1, 2:send_eg2, 3:send_eg3, 4:receive_eg1, 5:receive_eg2): "))
# print(type(func[chosefunc]))
if chosefunc>0 and chosefunc<6:
func[chosefunc]()
else:
print("error input!")
執行結果:
python網路程式設計簡介
1.tcp客戶端 1 coding utf823 import socket 45 target host www.chengzhier.com 6 target port 8078 建立乙個socket 物件 9 socket.af inet表示ipv4位址,或者主機名10 socket.scok...
網路程式設計簡介
c s架構 client server c 客戶端 s 服務端 b s架構 browers server b 瀏覽器 c 伺服器 b s架構本質就是c s架構 手機端在未來b s架構會變得更火 服務端就是24小時,不間斷提供服務 客戶端隨時隨地都能找服務端,體驗服務 七層協議 應用層表示層 會話層傳...
網路程式設計簡介
網路程式設計簡介 什麼是網路程式設計 網路通常指的是計算機中的網際網路,是由多台計算機通過網線或其他媒介相互連線組成的,編寫基於網路的應用程式的過程稱之為網路程式設計 那麼為什麼要學習網路程式設計 我們已經知道計算機,由作業系統,應用程式組成,有了這三個元素,就可以在自己的電腦上執行一些應用程式了,...