Set up a server that processes multiple connections at the same time

We often see examples of using SocketServer.TCPServer to set up a TCP server in python.

SocketServer.Example using TCP Server


import SocketServer

HOST, PORT = "", 12345

class SampleHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        client = self.request
        address = self.client_address[0]
        client.send("May I ask your name? ")
        name = client.recv(80).strip()
        self.request.send("Welcome %s from %s\n" % (name, address))

if __name__ == "__main__":
    server = SocketServer.TCPServer((HOST, PORT), SampleHandler)
    server.serve_forever()

However, with this, only one connection can be processed at one time, and if a new connection request comes during processing, it will wait until the previous processing is completed. Simply change this SocketServer.TCPServer to SocketServer.ThreadingTCPServer` and you will be able to handle multiple connections at the same time.

py:SocketServer.TCP Server Socket Server.Change to Threading TCP Server(Main part only)


if __name__ == "__main__":
    server = SocketServer.ThreadingTCPServer((HOST, PORT), SampleHandler)
    server.serve_forever()

However, since the number of sockets used will increase, it is advisable to take measures such as setting the upper limit of the number of connections with iptables and shortening the TIME_WAIT time with sysctl. Please adjust the upper limit and time according to the service.

Limit 15 connections from one IP address with iptables settings


-A INPUT -p tcp -m tcp --dport 12345 --tcp-flags FIN,SYN,RST,ACK SYN -m connlimit --connlimit-above 15 --connlimit-mask 32 --connlimit-saddr -j REJECT --reject-with tcp-reset

/etc/sysctl.TIME in conf_WAIT time reduced to 15 seconds(60 seconds when not set)


net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15

Also, if restarting the script causes an error while using the address, it is a good idea to set the address reuse setting on the script side as follows.

import SocketServer
import socket

HOST, PORT = "", 12345

class SampleHandler(SocketServer.BaseRequestHandler, object):
    def handle(self):
        client = self.request
        address = self.client_address[0]
        client.send("May I ask your name? ")
        name = client.recv(80).strip()
        self.request.send("Welcome %s from %s\n" % (name, address))

class SampleServer(SocketServer.ThreadingTCPServer, object):
    def server_bind(self):
        self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.socket.bind(self.server_address)

if __name__ == "__main__":
    server = SampleServer((HOST, PORT), SampleHandler)
    server.serve_forever()

I tried to operate about two CTF problem servers using ThreadingTCPServer.

SECCON CTF 2014 winter online qualifications Choose the number : https://github.com/shiracamus/seccon2014/blob/master/number/number.py Let's disassemble : https://github.com/shiracamus/seccon2014/blob/master/disassemble/disassemble.py

Recommended Posts

Set up a server that processes multiple connections at the same time
Turn multiple lists with a for statement at the same time in Python
Set up a Samba server with Docker
How to set the server time to Japanese time
Set up a mail server using Twisted
Plot multiple maps and data at the same time with Python's matplotlib
Set up a simple HTTPS server with asyncio
Set up a local server with Go-File upload-
Set up a test SMTP server in Python.
A program that searches for the same image
Set up a UDP server in C language
Type conversion of multiple columns of pandas DataFrame with astype at the same time
Loop variables at the same time in the template
How to set up a local development server
Set up a simple SMTP server in Python
Find a guideline for the number of processes / threads to set in the application server
Set up a simple local server on your Mac
[GoLang] Set a space at the beginning of the comment
Set up a Minecraft resource (Spigot) server via docker (2)
Set up a file server on Ubuntu 20.04 using Samba
Visualize data and understand correlation at the same time
A story that struggled with the common set HTTP_PROXY = ~
Set up a free server on AWS in 30 minutes
Set up a Minecraft resource (Spigot) server via docker
[Vagrant] Set up a simple API server with python
How to set up a simple SMTP server that can be tested locally in Python
wxPython: Draw animation and graph drawing at the same time
Launch a simple WEB server that can check the header
Set up a web server with CentOS7 + Anaconda + Django + Apache
Grep so that grep does not appear at the time of grep
Reload the server set up with gunicorn when changing the code
Even though it's this time, I try to set up a Linux server at home. I will think about how to use it later.
[Python 3.8 ~] Rewrite arrays etc. at the same time as definition [tips]
A function that measures the processing time of a method in python
Set up Ubuntu as a Linux cheat sheet and https server
Browse .loc and .iloc at the same time in pandas DataFrame
I started to work at different times, so I made a bot that tells me the time to leave
Set client-specific ssh host keys on multiple diskless clients that utilize the exact same root file system
Set up a dummy SMTP server in Python and check the operation of sending from Action Mailer