python Socket网络编程
網(wǎng)絡(luò)調(diào)試助手:https://pan.baidu.com/s/1Do-v8XMDaIYJsXRQok5RhQ?提取碼:ya4g?(便于測(cè)試)
?? ? ? ?套接字(Socket)是計(jì)算機(jī)之間進(jìn)行通信的一種約定。通過Socket,一臺(tái)計(jì)算機(jī)可以接受其他計(jì)算機(jī)的數(shù)據(jù),也可以向其他計(jì)算機(jī)發(fā)送數(shù)據(jù)。遠(yuǎn)程管理軟件和黑客軟件大多依賴于Socket來實(shí)現(xiàn)特定功能的,其包括兩個(gè)部分:運(yùn)行于服務(wù)器端稱之為ServerSocket,運(yùn)行于客戶機(jī)端稱之ClientSocket。
TCP
TCP是因特網(wǎng)中的傳輸層協(xié)議,使用三次握手協(xié)議建立連接
??
TCP_Client.py
import socketdef main():# 創(chuàng)建TCP套接字tcp_client_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)# 服務(wù)器地址sever_ip = input("請(qǐng)輸入服務(wù)器ip:")sever_port = input("請(qǐng)輸入服務(wù)器端口:")# 連接服務(wù)器(元組)tcp_client_socket.connect((sever_ip,int(sever_port)))# 輸入發(fā)送的數(shù)據(jù)data = input("請(qǐng)輸入要發(fā)送的數(shù)據(jù):")# 發(fā)送數(shù)據(jù)tcp_client_socket.send(data.encode("utf-8"))#接收數(shù)據(jù)recv_data = tcp_client_socket.recv(1024)print("對(duì)方的回復(fù):"recv_data.decode("utf-8"))if __name__ == '__main__':main()nc -lvp 8888?監(jiān)聽8888端口?
(一次完整對(duì)話)
?
?TCP_Sever.py?
?
import socketdef main():# 創(chuàng)建套接字tcp_server_socket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)# 綁定本地IP和端口tcp_server_socket.bind(("192.168.12.1",8888))# 被動(dòng) listentcp_server_socket.listen(128)while True:# 等待客戶端信息print("等待客戶端連接")client_socket,client_addr = tcp_server_socket.accept()print("客戶端為:",client_addr)#接收對(duì)方發(fā)送數(shù)據(jù)recv_data = client_socket.recv(1024)print("接收到信息為:",recv_data.decode("utf-8"))#發(fā)送數(shù)據(jù)到客戶端client_socket.send("Yasso".encode("utf-8"))client_socket.close()if __name__ == "__main__":main()?
UDP
UDP 為應(yīng)用程序提供了一種無需建立連接就可以發(fā)送封裝的 IP 數(shù)據(jù)包的方法。
?
UDP_Client_send.py?
import socket #創(chuàng)建udp套接字 udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # 目標(biāo)ip和端口 target_addr = ('192.168.12.128',8888)#獲取數(shù)據(jù) data = input("請(qǐng)輸入要發(fā)送的數(shù)據(jù):")#發(fā)送數(shù)據(jù) udp_socket.sendto(data.encode('utf-8'),target_addr)udp_socket.close()UDP_Client_receive.py?
import socket #創(chuàng)建udp套接字 udp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)local_addr=('192.168.12.128',8888) #綁定ip(必須本地)和端口 udp_socket.bind(local_addr)#等待接受對(duì)方發(fā)送的數(shù)據(jù) recv_data = udp_socket.recvfrom(1024) #表示本次接受的最大字節(jié)數(shù)1024# 顯示接受的數(shù)據(jù) print(recv_data[0].decode('utf-8')) udp_socket.close()?liunx等待接受數(shù)據(jù)->win10發(fā)送數(shù)據(jù)->liunx成功接收數(shù)據(jù)
nc -ulp 8888?監(jiān)聽udp模式下的8888端口?
私密聊天室
# UDP應(yīng)用-私密聊天室(極簡(jiǎn)) import socketdef send(chat_ip,chat_port):udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)"""發(fā)送消息"""address = (chat_ip,int(chat_port))print(address)data = input("請(qǐng)輸入發(fā)送的消息:")udp_socket.sendto(data.encode("utf-8"),address)def receive():"""接收消息"""udp_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)# 綁定本地IP和端口udp_socket.bind(("192.168.12.1",8888))recv_data = udp_socket.recvfrom(1024)print(recv_data[0].decode('utf-8'))def main():chat_ip = input("請(qǐng)輸入您聊天對(duì)方IP地址:")chat_port = input("請(qǐng)輸入您聊天對(duì)方端口:")# 循環(huán)調(diào)用while True:print("++++++歡迎進(jìn)入私密聊天室++++++")print("0:發(fā)送消息")print("1:接收消息")print("2:退出聊天")function = input("請(qǐng)輸入您要用的模塊")if function == "0":send(chat_ip,chat_port)elif function == "1":receive()elif function == "2":breakelse:print("輸入有誤,請(qǐng)重新輸入")if __name__ == '__main__':main()總結(jié)
以上是生活随笔為你收集整理的python Socket网络编程的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机操作系统(10):集群和分布式
- 下一篇: 玩转oracle 11g(48):ora