Python socket library thinks socket is open when its not

2024/11/19 2:39:07

I'm working with a bit of Python that looks like this:

HOST = '127.0.0.1'
PORT = 43434
single = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:single.bind((HOST, PORT))
except socket.error as e:# Print an error, exit.

While it's been functioning well in the past, we now get the error [Errno 98] Address already in use. The SIGINT handler closes the socket connection, so I'm not sure how it got in that state, but for now I'm just trying to fix it.

Both lsof and netstat say there's nothing using that port:

[$]> sudo netstat -an | grep 43434
[$]> sudo lsof -i :43434

TIME_WAIT is set to 60 seconds, according to /proc/sys/net/ipv4/tcp_fin_timeout, but the error occurs even hours after last run successfully.

I've tried (temporarily) setting REUSEADDR (via single.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)), but that appears to have no effect.

What in tarnation is going on? Will I ever be able to use this port again without having to reboot the machine?

Answer

Try this:
tcpkill -i eth0 port 43434

https://en.xdnf.cn/q/70986.html

Related Q&A

object of type _csv.reader has no len(), csv data not recognized

The following is a self-contained example. Change the "folder_name" to run it. This answers : reader type = _csv.reader list(reader) = [] _csv.reader has no len()I have tried many things but …

Lookup country for GPS coordinates without Internet access

I need to find out in what country given GPS coordinates are, on a device that has no Internet access (e.g. this, but without the easy on-line solution). Having no experience with GIS, I guess Id need …

how to get spyders python recognize external packages on MacOS X?

I have spyderlib installed on my MacOS X (10.6.8) using the official dmg file. In parallel, I have installed packages using both pip and homebrew from the terminal (i.e. opencv, gdal...). As Spyder is …

textcat - architecture extra fields not permitted

Ive been trying to practise what Ive learned from this tutorial:(https://realpython.com/sentiment-analysis-python/) using PyCharm. And this line: textcat.add_label("pos")generated a warning: …

cv2.rectangle() calls overloaded method, although I give other parameter

cv2.rectangle has two ways of calling:img = cv.rectangle( img, pt1, pt2, color[, thickness[, lineType[, shift]]] ) img = cv.rectangle( img, rec, color[, thickness[, lineType[, shift]]]source:h…

Converting xls to csv in Python 3 using xlrd

Im using Python 3.3 with xlrd and csv modules to convert an xls file to csv. This is my code:import xlrd import csvdef csv_from_excel():wb = xlrd.open_workbook(MySpreadsheet.xls)sh = wb.sheet_by_name(S…

HTMLParser.HTMLParser().unescape() doesnt work

I would like to convert HTML entities back to its human readable format, e.g. £ to £, ° to etc.Ive read several posts regarding this question Converting html source content into read…

What security issues need to be addressed when working with Google App Engine?

Ive been considering using Google App Engine for a few hobby projects. While they wont be handling any sensitive data, Id still like to make them relatively secure for a number of reasons, like learnin…

Supporting multiple Python module versions (with the same version of Python)

I looked around but cannot find a clear answer to my question.I have a very legitimate need for supporting N-versions of the same Python module.If they are stored in the same same package/directory, th…

ImportError: cannot import name signals

Im using Django 1.3.0 with Python 2.7.1. In every test I write the following imports I get the importError above:from django.utils import unittest from django.test.client import ClientThe full stack tr…