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)
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 8080Check 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' # 服务端正在收数据...
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' # >>:
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))
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 时间
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()
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()
Enter the
dir command. Since the server sends less than 1024 bytes, the client can accept. Enter the
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.
5. 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()
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'
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()
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'))
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()
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('utf8'))
2、自定义固定长度报头(struct模块)
struct模块解析
import struct import json # 'i'是格式 try: obj = struct.pack('i', 1222222222223) except Exception as e: print(e) obj = struct.pack('i', 1222) print(obj, len(obj)) # 'i' format requires -2147483648 <= number <= 2147483647 # b'\xc6\x04\x00\x00' 4 res = struct.unpack('i', obj) print(res[0]) # 1222
解决粘包问题的核心就是:为字节流加上自定义固定长度报头,报头中包含字节流长度,然后一次send到对端,对端在接收时,先从缓存中取出定长的报头,然后再取真实数据。
1、 使用struct模块创建报头:
import json import struct header_dic = { 'filename': 'a.txt', 'total_size':111111111111111111111111111111111222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222223131232, 'hash': 'asdf123123x123213x' } header_json = json.dumps(header_dic) header_bytes = header_json.encode('utf-8') print(len(header_bytes))# 223 # 'i'是格式 obj = struct.pack('i', len(header_bytes)) print(obj, len(obj)) # b'\xdf\x00\x00\x00' 4 res = struct.unpack('i', obj) print(res[0]) # 223
2、服务端:
from socket import * import subprocess import struct import json 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() print(conn, client_addr) while True: cmd = conn.recv(1024) obj = subprocess.Popen(cmd.decode('utf8'), shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE) stderr = obj.stderr.read() stdout = obj.stdout.read() # 制作报头 header_dict = { 'filename': 'a.txt', 'total_size': len(stdout) + len(stderr), 'hash': 'xasf123213123' } header_json = json.dumps(header_dict) header_bytes = header_json.encode('utf8') # 1. 先把报头的长度len(header_bytes)打包成4个bytes,然后发送 conn.send(struct.pack('i', len(header_bytes))) # 2. 发送报头 conn.send(header_bytes) # 3. 发送真实的数据 conn.send(stdout) conn.send(stderr) conn.close() server.close()
3、 客户端:
from socket import * import json import struct client = socket(AF_INET, SOCK_STREAM) client.connect(('127.0.0.1', 8000)) while True: cmd = input('please enter your cmd you want>>>') if len(cmd) == 0: continue client.send(cmd.encode('utf8')) # 1. 先收4个字节,这4个字节中包含报头的长度 header_len = struct.unpack('i', client.recv(4))[0] # 2. 再接收报头 header_bytes = client.recv(header_len) # 3. 从包头中解析出想要的东西 header_json = header_bytes.decode('utf8') header_dict = json.loads(header_json) total_size = header_dict['total_size'] # 4. 再收真实的数据 recv_size = 0 res = b'' while recv_size < total_size: data = client.recv(1024) res += data recv_size += len(data) print(res.decode('utf8')) client.close()
二、基于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(('127.0.0.1', 8080)) while True: data, client_addr = server.recvfrom(1024) print('===>', data, client_addr) server.sendto(data.upper(), client_addr) server.close()
2、客户端
import socket client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》UDP while True: msg = input('>>: ').strip() # msg='' client.sendto(msg.encode('utf-8'), ('127.0.0.1', 8080)) data, server_addr = client.recvfrom(1024) print(data) client.close()
三、基于socketserver实现并发的socket编程
1、基于TCP协议
基于tcp的套接字,关键就是两个循环,一个链接循环,一个通信循环
socketserver模块中分两大类:server类(解决链接问题)和request类(解决通信问题)。
1、 server类
2、 request类
基于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__ == '__main__': s = socketserver.ThreadingTCPServer(('127.0.0.1', 8080), MyHandler, bind_and_activate=True) s.serve_forever() # 代表连接循环 # 循环建立连接,每建立一个连接就会启动一个线程(服务员)+调用Myhanlder类产生一个对象,调用该对象下的handle方法,专门与刚刚建立好的连接做通信循环
4、 客户端
import socket phone = socket.socket(socket.AF_INET, socket.SOCK_STREAM) phone.connect(('127.0.0.1', 8080)) # 指定服务端ip和端口 while True: # msg=input('>>: ').strip() #msg='' msg = 'client33333' # msg='' if len(msg) == 0: continue phone.send(msg.encode('utf-8')) data = phone.recv(1024) print(data) phone.close()
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('客户消息', data) self.request[1].sendto(data.upper(), self.client_address) if __name__ == '__main__': s = socketserver.ThreadingUDPServer(('127.0.0.1', 8080), MyHandler) s.serve_forever()
2、 客户端
import socket client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # 数据报协议-》udp while True: # msg=input('>>: ').strip() #msg='' msg = 'client1111' client.sendto(msg.encode('utf-8'), ('127.0.0.1', 8080)) data, server_addr = client.recvfrom(1024) print(data) client.close()
四、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!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.