How to use Python's socket and socketserver

王林
Release: 2023-05-28 20:10:06
forward
2471 people have browsed it

1. Socket programming based on TCP protocol

1. Socket workflow

Let’s start with the server side. The server first initializes the Socket, then binds to the port, listens to the port, calls accept to block, and waits for the client to connect. At this time, if a client initializes a Socket and then connects to the server (connect), if the connection is successful, the connection between the client and the server is established. The client sends a data request, the server receives the request and processes the request, then sends the response data to the client, the client reads the data, and finally closes the connection. An interaction ends. Use the following Python code to implement it:

import socket
# socket_family 可以是 AF_UNIX 或 AF_INET。socket_type 可以是 SOCK_STREAM 或 SOCK_DGRAM。protocol 一般不填,默认值为 0
socket.socket(socket_family, socket_type, protocal=0)
# 获取tcp/ip套接字
tcpSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 获取udp/ip套接字
udpSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
Copy after login
1. Server socket function
  • ##s.bind(): Bind (host, port number) to socket

  • ## s.listen(): Start TCP monitoring
  • s.accept(): Passively accept the connection from the TCP client, (blocking) waiting for the arrival of the connection
  • 2. Client socket function
    s.connect(): Actively initialize TCP server connection
  • s.connect_ex (): An extended version of the connect() function, which returns an error code when an error occurs instead of throwing an exception
  • 3. Public-purpose socket function
    s.recv(): Receive TCP data
  • s.send(): Send TCP data (send is used when the amount of data to be sent is greater than the remaining space in the end buffer , data is lost and will not be sent completely)
  • s.sendall(): Send complete TCP data (the essence is to call send in a loop, sendall will be sent when the amount of data to be sent is greater than the own buffer area) When there is remaining space, the data is not lost, and the send is called in a loop until it is sent)
  • s.recvfrom(): Receive UDP data
  • s. sendto(): Send UDP data
  • s.getpeername(): Connect to the remote address of the current socket
  • s. getsockname(): The address of the current socket
  • s.getsockopt(): Returns the parameters of the specified socket
  • s.setsockopt (): Set the parameters of the specified socket
  • s.close(): Close the socket
  • 4. Lock-oriented socket Socket method
    s.setblocking(): Set the blocking and non-blocking mode of the socket
  • s.settimeout(): Set The timeout period for blocking socket operations
  • s.gettimeout(): Get the timeout period for blocking socket operations
  • 5. For File socket function
    s.fileno(): Socket file descriptor
  • s.makefile(): Create a file related to the socket
  • 2. Socket programming based on TCP protocol

can be done through

netstat -an | findstr 8080

Check the socket status1. Server

import socket
# 1、买手机
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # tcp称为流式协议,udp称为数据报协议SOCK_DGRAM
# print(phone)
# 2、插入/绑定手机卡
# phone.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
phone.bind(('127.0.0.1', 8080))
# 3、开机
phone.listen(5) # 半连接池,限制的是请求数
# 4、等待电话连接
print('start....')
while True: # 连接循环
conn, client_addr = phone.accept() # (三次握手建立的双向连接,(客户端的ip,端口))
# print(conn)
print('已经有一个连接建立成功', client_addr)
# 5、通信:收\发消息
while True: # 通信循环
try:
print('服务端正在收数据...')
data = conn.recv(1024) # 最大接收的字节数,没有数据会在原地一直等待收,即发送者发送的数据量必须>0bytes
# print('===>')
if len(data) == 0: break # 在客户端单方面断开连接,服务端才会出现收空数据的情况
print('来自客户端的数据', data)
conn.send(data.upper())
except ConnectionResetError:
break
# 6、挂掉电话连接
 conn.close()
# 7、关机
phone.close()
# start....
# 已经有一个连接建立成功 ('127.0.0.1', 4065)
# 服务端正在收数据...
# 来自客户端的数据 b'\xad'
# 服务端正在收数据...
Copy after login
2. Client
import socket
# 1、买手机
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# print(phone)
# 2、拨电话
phone.connect(('127.0.0.1', 8080)) # 指定服务端ip和端口
# 3、通信:发\收消息
while True: # 通信循环
msg = input('>>: ').strip() # msg=''
if len(msg) == 0: continue
phone.send(msg.encode('utf-8'))
# print('has send----->')
data = phone.recv(1024)
# print('has recv----->')
print(data)
# 4、关闭
phone.close()
# >>: 啊
# b'a'
# >>: 啊啊
# b'\xb0\xa1\xb0\xa1'
# >>:
Copy after login
3. Address occupation problem

This is because of you The server still has the time_wait state of four waves occupying the address (if you don't understand, please study in depth 1.tcp three-way handshake, four waves 2.syn flood attack 3. There will be a large number of time_wait states under high server concurrency Optimization method)

1. Method 1: Add a socket configuration and reuse ip and port

# 

phone=socket(AF_INET,SOCK_STREAM)
phone.setsockopt(SOL_SOCKET,SO_REUSEADDR,1) #就是它,在bind前加
phone.bind(('127.0.0.1',8080))
Copy after login
2. Method 2: By adjusting the linux kernel parameters
发现系统存在大量TIME_WAIT状态的连接,通过调整linux内核参数解决,
vi /etc/sysctl.conf
编辑文件,加入以下内容:
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = 30
然后执行 /sbin/sysctl -p 让参数生效。
net.ipv4.tcp_syncookies = 1 表示开启SYN Cookies。当出现SYN等待队列溢出时,启用cookies来处理,可防范少量SYN攻击,默认为0,表示关闭;
net.ipv4.tcp_tw_reuse = 1 表示开启重用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
net.ipv4.tcp_tw_recycle = 1 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。
net.ipv4.tcp_fin_timeout 修改系統默认的 TIMEOUT 时间
Copy after login
4. Simulation SSH remote execution command

The server executes the command through subprocess, and then returns the result of the command.

Server:

from socket import *
import subprocess
server = socket(AF_INET, SOCK_STREAM)
server.bind(('127.0.0.1', 8000))
server.listen(5)
print('start...')
while True:
conn, client_addr = server.accept()
while True:
print('from client:', client_addr)
cmd = conn.recv(1024)
if len(cmd) == 0: break
print('cmd:', cmd)
obj = subprocess.Popen(cmd.decode('utf8'), # 输入的cmd命令
shell=True, # 通过shell运行
stderr=subprocess.PIPE, # 把错误输出放入管道,以便打印
stdout=subprocess.PIPE) # 把正确输出放入管道,以便打印

stdout = obj.stdout.read() # 打印正确输出
stderr = obj.stderr.read() # 打印错误输出

conn.send(stdout)
conn.send(stderr)
conn.close()
server.close()
Copy after login

Client

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8000))
while True:
data = input('please enter your data')
client.send(data.encode('utf8'))
data = client.recv(1024)
print('from server:', data)
client.close()
Copy after login

Enter the

dir

command. Since the server sends less than 1024 bytes, the client can accept. Enter the

tasklist

command. Since the server sends more bytes than 1024 bytes, the client only accepts part of the data, and when you enter the dir command again , the client will receive the result of the dir command, but it will print the remaining unsent data from the last time. This is a sticky problem.

How to use Pythons socket and socketserver5. Sticky packets

1. The sender needs to wait until the buffer is full before sending out, resulting in sticky packets

It takes time to send data The interval is very short, the amount of data is very small, and they come together to produce sticky packets.

Server

# _*_coding:utf-8_*_
from socket import *
ip_port = ('127.0.0.1', 8080)
TCP_socket_server = socket(AF_INET, SOCK_STREAM)
TCP_socket_server.bind(ip_port)
TCP_socket_server.listen(5)
conn, addr = TCP_socket_server.accept()

data1 = conn.recv(10)
data2 = conn.recv(10)
print('----->', data1.decode('utf-8'))
print('----->', data2.decode('utf-8'))
conn.close()
Copy after login

Client

# _*_coding:utf-8_*_
import socket
BUFSIZE = 1024
ip_port = ('127.0.0.1', 8080)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = s.connect_ex(ip_port)
s.send('hello'.encode('utf-8'))
s.send('world'.encode('utf-8'))

# 服务端一起收到b'helloworld'
Copy after login

2. The receiver does not receive the packets in the buffer in time, causing multiple packets to be received

Client sends After receiving a piece of data, the server only received a small part. The next time the server collects it, it still gets the data left over from the buffer, resulting in sticky packets.

Server

# _*_coding:utf-8_*_
from socket import *
ip_port = ('127.0.0.1', 8080)
TCP_socket_server = socket(AF_INET, SOCK_STREAM)
TCP_socket_server.bind(ip_port)
TCP_socket_server.listen(5)
conn, addr = TCP_socket_server.accept()
data1 = conn.recv(2) # 一次没有收完整
data2 = conn.recv(10) # 下次收的时候,会先取旧的数据,然后取新的
print('----->', data1.decode('utf-8'))
print('----->', data2.decode('utf-8'))
conn.close()
Copy after login

Client

# _*_coding:utf-8_*_
import socket
BUFSIZE = 1024
ip_port = ('127.0.0.1', 8080)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
res = s.connect_ex(ip_port)
s.send('hello feng'.encode('utf-8'))
Copy after login

6. Solving the problem of sticky packets

1. The total size of the byte stream sent first (low version)

The root of the problem is that the receiving end does not know the length of the byte stream to be transmitted by the sending end, so the solution to sticky packets is to focus on how to make the sending end send the words it is about to send before sending data. The total throttling size is known to the receiving end, and then the receiving end creates an infinite loop to receive all the data.

Why is it low: The program runs much faster than the network transmission speed, so before sending a segment of bytes, use send to send the length of the byte stream. This method will amplify the performance caused by network delay. loss.

Server:

import socket, subprocess
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 8000))
server.listen(5)
while True:
conn, addr = server.accept()
print('start...')
while True:
cmd = conn.recv(1024)
print('cmd:', cmd)
obj = subprocess.Popen(cmd.decode('utf8'),
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
stdout = obj.stdout.read()
if stdout:
ret = stdout
else:
stderr = obj.stderr.read()
ret = stderr
ret_len = len(ret)
 conn.send(str(ret_len).encode('utf8'))
data = conn.recv(1024).decode('utf8')
if data == 'recv_ready':
conn.sendall(ret)
conn.close()
server.close()
Copy after login

Client:

import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(('127.0.0.1', 8000))
while True:
msg = input('please enter your cmd you want>>>').strip()
if len(msg) == 0: continue
client.send(msg.encode('utf8'))
length = int(client.recv(1024))
client.send('recv_ready'.encode('utf8'))
send_size = 0
recv_size = 0
data = b''
while recv_size < length:
data = client.recv(1024)
recv_size += len(data)
print(data.decode(&#39;utf8&#39;))
Copy after login
2、自定义固定长度报头(struct模块)

struct模块解析

import struct
import json
# &#39;i&#39;是格式
try:
obj = struct.pack(&#39;i&#39;, 1222222222223)
except Exception as e:
print(e)
obj = struct.pack(&#39;i&#39;, 1222)
print(obj, len(obj))
# &#39;i&#39; format requires -2147483648 <= number <= 2147483647
# b&#39;\xc6\x04\x00\x00&#39; 4

res = struct.unpack(&#39;i&#39;, obj)
print(res[0])
# 1222
Copy after login

解决粘包问题的核心就是:为字节流加上自定义固定长度报头,报头中包含字节流长度,然后一次send到对端,对端在接收时,先从缓存中取出定长的报头,然后再取真实数据。

1、 使用struct模块创建报头:

import json
import struct
header_dic = {
&#39;filename&#39;: &#39;a.txt&#39;,
&#39;total_size&#39;:111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223131232,
&#39;hash&#39;: &#39;asdf123123x123213x&#39;
}
header_json = json.dumps(header_dic)
header_bytes = header_json.encode(&#39;utf-8&#39;)
print(len(header_bytes))# 223
# &#39;i&#39;是格式
obj = struct.pack(&#39;i&#39;, len(header_bytes))
print(obj, len(obj))
# b&#39;\xdf\x00\x00\x00&#39; 4

res = struct.unpack(&#39;i&#39;, obj)
print(res[0])
# 223
Copy after login

2、服务端:

from socket import *
import subprocess
import struct
import json
server = socket(AF_INET, SOCK_STREAM)
server.bind((&#39;127.0.0.1&#39;, 8000))
server.listen(5)
print(&#39;start...&#39;)
while True:
conn, client_addr = server.accept()
print(conn, client_addr)
while True:
cmd = conn.recv(1024)
obj = subprocess.Popen(cmd.decode(&#39;utf8&#39;),
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)
stderr = obj.stderr.read()
stdout = obj.stdout.read()
# 制作报头
header_dict = {
&#39;filename&#39;: &#39;a.txt&#39;,
&#39;total_size&#39;: len(stdout) + len(stderr),
&#39;hash&#39;: &#39;xasf123213123&#39;
}
header_json = json.dumps(header_dict)
header_bytes = header_json.encode(&#39;utf8&#39;)
# 1. 先把报头的长度len(header_bytes)打包成4个bytes,然后发送
conn.send(struct.pack(&#39;i&#39;, len(header_bytes)))
# 2. 发送报头
 conn.send(header_bytes)
# 3. 发送真实的数据
 conn.send(stdout)
conn.send(stderr)
conn.close()
server.close()
Copy after login

3、 客户端:

from socket import *
import json
import struct
client = socket(AF_INET, SOCK_STREAM)
client.connect((&#39;127.0.0.1&#39;, 8000))
while True:
cmd = input(&#39;please enter your cmd you want>>>&#39;)
if len(cmd) == 0: continue
client.send(cmd.encode(&#39;utf8&#39;))
# 1. 先收4个字节,这4个字节中包含报头的长度
header_len = struct.unpack(&#39;i&#39;, client.recv(4))[0]
# 2. 再接收报头
header_bytes = client.recv(header_len)
# 3. 从包头中解析出想要的东西
header_json = header_bytes.decode(&#39;utf8&#39;)
header_dict = json.loads(header_json)
total_size = header_dict[&#39;total_size&#39;]
# 4. 再收真实的数据
recv_size = 0
res = b&#39;&#39;
while recv_size < total_size:
data = client.recv(1024)
res += data
recv_size += len(data)
print(res.decode(&#39;utf8&#39;))
client.close()
Copy after login

二、基于UDP协议的socket套接字编程

  • UDP是无链接的,先启动哪一端都不会报错,并且可以同时多个客户端去跟服务端通信

  • UDP协议是数据报协议,发空的时候也会自带报头,因此客户端输入空,服务端也能收到。

  • UPD协议一般不用于传输大数据。

  • UPD套接字无粘包问题,但是不能替代TCP套接字,因为UPD协议有一个缺陷:如果数据发送的途中,数据丢失,则数据就丢失了,而TCP协议则不会有这种缺陷,因此一般UPD套接字用户无关紧要的数据发送,例如qq聊天。

UDP套接字简单示例

1、服务端
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》UDP
server.bind((&#39;127.0.0.1&#39;, 8080))
while True:
data, client_addr = server.recvfrom(1024)
print(&#39;===>&#39;, data, client_addr)
server.sendto(data.upper(), client_addr)
server.close()
Copy after login
2、客户端
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》UDP
while True:
msg = input(&#39;>>: &#39;).strip() # msg=&#39;&#39;
client.sendto(msg.encode(&#39;utf-8&#39;), (&#39;127.0.0.1&#39;, 8080))
data, server_addr = client.recvfrom(1024)
print(data)
client.close()
Copy after login

三、基于socketserver实现并发的socket编程

1、基于TCP协议

基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环

socketserver模块中分两大类:server类(解决链接问题)和request类(解决通信问题)。

1、 server类

How to use Pythons socket and socketserver

2、 request类

How to use Pythons socket and socketserver

基于tcp的socketserver我们自己定义的类中的。

  • self.server即套接字对象

  • self.request即一个链接

  • self.client_address即客户端地址

3、 服务端
import socketserver
class MyHandler(socketserver.BaseRequestHandler):
def handle(self):
# 通信循环
while True:
# print(self.client_address)
# print(self.request) #self.request=conn
try:
data = self.request.recv(1024)
if len(data) == 0: break
self.request.send(data.upper())
except ConnectionResetError:
break
if __name__ == &#39;__main__&#39;:
s = socketserver.ThreadingTCPServer((&#39;127.0.0.1&#39;, 8080), MyHandler, bind_and_activate=True)
s.serve_forever() # 代表连接循环
# 循环建立连接,每建立一个连接就会启动一个线程(服务员)+调用Myhanlder类产生一个对象,调用该对象下的handle方法,专门与刚刚建立好的连接做通信循环
Copy after login
4、 客户端
import socket
phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
phone.connect((&#39;127.0.0.1&#39;, 8080)) # 指定服务端ip和端口
while True:
# msg=input(&#39;>>: &#39;).strip() #msg=&#39;&#39;
msg = &#39;client33333&#39; # msg=&#39;&#39;
if len(msg) == 0: continue
phone.send(msg.encode(&#39;utf-8&#39;))
data = phone.recv(1024)
print(data)
phone.close()
Copy after login

2、基于UDP协议

基于udp的socketserver我们自己定义的类中的

  • self.request是一个元组(第一个元素是客户端发来的数据,第二部分是服务端的udp套接字对象),如(b'adsf', )

  • self.client_address即客户端地址

1、 服务端
import socketserver
class MyHandler(socketserver.BaseRequestHandler):
def handle(self):
# 通信循环
print(self.client_address)
print(self.request)
data = self.request[0]
print(&#39;客户消息&#39;, data)
self.request[1].sendto(data.upper(), self.client_address)
if __name__ == &#39;__main__&#39;:
s = socketserver.ThreadingUDPServer((&#39;127.0.0.1&#39;, 8080), MyHandler)
s.serve_forever()
Copy after login
2、 客户端
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》udp
while True:
# msg=input(&#39;>>: &#39;).strip() #msg=&#39;&#39;
msg = &#39;client1111&#39;
client.sendto(msg.encode(&#39;utf-8&#39;), (&#39;127.0.0.1&#39;, 8080))
data, server_addr = client.recvfrom(1024)
print(data)
client.close()
Copy after login

四、Python Internet 模块

以下列出了 Python 网络编程的一些重要模块:

协议 功能用处 端口号 Python 模块
HTTP 网页访问 80 httplib, urllib, xmlrpclib
NNTP 阅读和张贴新闻文章,俗称为"帖子" 119 nntplib
FTP 文件传输 20 ftplib, urllib
SMTP 发送邮件 25 smtplib
POP3 接收邮件 110 poplib
IMAP4 获取邮件 143 imaplib
Telnet 命令行 23 telnetlib
Gopher 信息查找 70 gopherlib, urllib

The above is the detailed content of How to use Python's socket and socketserver. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!