Reputation: 441
I have this code:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind(('10.0.0.253', 8080))
except:
s.close()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('10.0.0.253', 8080))
s.listen(1)
conn, addr = s.accept()
which binds, then if it encounters an error with that, destroys the socket then creates a new one and binds it to the same IP and port. For some reason, even after closing the socket, I get this error:
Traceback (most recent call last):
File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 68, in <module>
main()
File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 66, in main
connect()
File "C:\Users\other\Desktop\tcpReverseShell_Server.py", line 43, in connect
s.bind(('10.0.0.253', 8080))
File "C:\Python27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10048] Only one usage of each socket address (protocol/network
address/port) is normally permitted
The only solution I've found is to use s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
, but when I try that I get this error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
. I
I am on Windows 10, Python 2.7.13.
Upvotes: 9
Views: 45806
Reputation: 9
I face this issue many times , it happens mostly when your terminal crashes due to a network issue, or some other internal error.
solution :- find the port is listening or not and terminate it manual through your command prompt (CMD)
ctrl+R
netstat -ano | find "5005"
note: enter the port surrounded by double-quotes: "port number"]
TCP 0.0.0.0:5005 0.0.0.0:0 LISTENING 12112
here "12112" is process ID
taskkill /F /PID 12112
12112 is process ID , don't give double-quotes here
The following will be displayed by system:
SUCCESS: The process with PID 12112 has been terminated
If it still gives error then restart the system and try again.
Upvotes: 1
Reputation: 2701
I can simulate your problem by running the python script twice concurrently. The problem is that some other application is currently also using the socket.
In your code, the except block is triggered when your socket fails to bind. However, when you try to .close()
the socket and try again, it does nothing as it is trying to .close()
an unbound socket. Hence, your code is equivalent to:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('10.0.0.253', 8080))
s.listen(1)
conn, addr = s.accept()
Note that when you .close()
the socket, it also does not .close()
the currently bound application, it attempts to .close()
to socket object you created. So, trying to bind the socket again will fail.
To find out what is using the socket, you can use netstat
:
netstat -aon | findstr :8080
This should give you something like:
TCP 127.0.0.1:8080 0.0.0.0:0 LISTENING 6604
Then, if you want to kill it forcefully, you can use TaskKill
and the application's PID:
TaskKill /PID 6604 /F
Upvotes: 12