In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains how to solve the problem of port occupation after python process ends. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to solve the problem of port occupation after python process ends"!
socket allocation
A server-side process requests a scoket from the operating system to listen, but when the process exits, the connection that has not been closed does not disappear immediately, but is left to the operating system to handle. The operating system will try to close the connection. However, if there is a problem when closing, the connection will always be in TIME_WAIT or other abnormal state, and this is the corresponding port is still in the occupied state. If the server program is restarted at this time, the address will be occupied.
example
Test Code:
import sockets = socket.socket()s.bind(('0.0.0.0', 12345))s.listen()(client, addr) = s.accept()print(client)print(addr)
Connect with NC:
nc 127.0.0.1 12345
The server prints client and addr and exits normally, but in this case netstat -anop is used| grep 12345 Check and find that the corresponding connection is not immediately released
tcp 0 0 127.0.0.1:12345 127.0.0.1:59408 TIME_WAIT - timewait (28.18/0/0)
At this time, start the server again and find that the error is reported:
Traceback (most recent call last): File "server.py", line 5, in s.bind(('0.0.0.0', 12345))OSError: [Errno 98] Address already in use Solution
Using setsockopt:
import sockets = socket.socket()s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind(('0.0.0.0', 12345))s.listen()(client, addr) = s.accept()print(client)print(addr)
At this point, the address will not appear occupied prompt
There are the same methods in c, but the method declarations are different. The usage of version c is
struct sockaddr_in addr;addr.sin_family = AF_INET;addr.sin_port = htons(12345);addr.sin_addr.s_addr = htonl(INADDR_ANY);int s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);int reuse = 1;setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));bind(s, (struct sockaddr *) &addr, sizeof(addr))listen(s, )struct sockaddr_in in_addr;int len = sizeof(in_addr);int client = accept(socket, (struct sockaddr *) in_addr, &len);//handle client//... other
Found that in addition to SO_REUSEADDR there is also an option SO_REUSEPORT, query found that it is unique to BSD, Linux and can not be used
If the client binds the port with this attribute, the server may receive a FIN for no reason as soon as it connects, causing it to close immediately. Therefore, the client should pay attention when using this option.
At this point, I believe that everyone has a deeper understanding of "how to solve the problem of port occupation after the python process is over." Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.