Python socket网络模块

Posted 林中龙虾

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python socket网络模块相关的知识,希望对你有一定的参考价值。

一、基于TCP协议的socket通信

以打电话为理解方式进行TCP的通信。
Server端代码:
import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #购买电话卡,AF_INET服务器之间网络通信,socket.SOCK_STREAM,流式协议,就是TCP协议 phone.bind((127.0.0.1, 8080)) #选择电话号码,绑定IP地址 phone.listen(5) #开机,监控5个请求,最多能进入6个,第7个会报错 conn, addr = phone.accept() #等待接听电话,阻塞状态,获取IP和端口 print(conn, addr) #打印IP和端口 client_data = conn.recv(1024) #交流过程 print(client_data) #打印接收到的数据 conn.send(client_data.upper()) #接收到的英文字母变成大写并回传数据 conn.close() phone.close() Client端代码: import socket phone = socket.socket() phone.connect((127.0.0.1, 8080)) msg = input(>>>).strip() phone.send(msg.encode(utf-8)) server_data = phone.recv(1024) #限制最大接收字节 print(server_data) phone.close() 先运行Server端,再运行Client端,输入:Others laugh at me for being mad. I laugh at others for not being able to see through. ------------------------------------Client端运行结果--------------------------------------- >>>Others laugh at me for being mad. I laugh at others for not being able to see through. bOTHERS LAUGH AT ME FOR BEING MAD. I LAUGH AT OTHERS FOR NOT BEING ABLE TO SEE THROUGH. ------------------------------------------------------------------------------------------ ------------------------------------Server端运行结果---------------------------------------- <socket.socket fd=244, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=(127.0.0.1, 8080), raddr=(127.0.0.1, 64096)> (127.0.0.1, 64096) bOthers laugh at me for being mad. I laugh at others for not being able to see through. -------------------------------------------------------------------------------------------

 二、单循环模式

Server端代码:
import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  #使用TCP协议创建网络通信
phone.bind((127.0.0.1, 8080))                            #绑定IP地址和端口
phone.listen(5)                                            #设置最大连接数,但是同一时间只能处理一个请求while 1:                                                   #使服务端能够一直接受数据
conn, addr = phone.accept() #获取连接到服务器的IP和端口
try: client_data = conn.recv(1024) #接收1024字节的数据 print(client_data) #打印数据(数字为bytes类型) conn.send(client_data + b-yes-) #源数据加上-yes-并返回给客户端 except Exception: break conn.close() phone.close() Client端代码: import socket phone = socket.socket() phone.connect((127.0.0.1, 8080)) while 1: msg = input(>>>).strip() #可输入要传输的字符 if msg.upper() == Q: #如果输出q则退出
break
elif not msg: #如果msg接到到的内容为空,则结束本次循环
continue phone.send(msg.encode(utf-8)) #使用utf-8的编码进行发送数据 server_data = phone.recv(1024) #最多只能接受1024字节,只问题会产生粘包 print(server_data.decode(utf-8)) #打印解码后的数据 phone.close() ------------------------------------Client端运行结果--------------------------------------- >>>laugh #输入要传到S端的内容 laugh-yes- #打印出来的是S端返回的内容 >>>crazy crazy-yes- >>>q Process finished with exit code 0 -------------------------------------------------------------------------------------------
------------------------------------Server端运行结果----------------------------------------
blaugh                                                   #收到C端发送来的内容
b
crazy

-------------------------------------------------------------------------------------------

 三、远程执行命令

 









以上是关于Python socket网络模块的主要内容,如果未能解决你的问题,请参考以下文章

python网络编程

Python中的socket模块

python网络设计模块

Python socket 模块

python socket模块

Python3-socketserver模块-网络服务器框架