Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to close python socket / Find the current running server on my dev environment

Tags:

I'm trying to learn python (using the Flask micro-framework) and I am confused because somewhere in my code i'm keeping the server open I believe.

I spin up my server with 'python app.py' and then close it however.... it still lives!

I'm not sure how this is possible but i must have done something wrong with a connection.

There are two questions here really.

First: How can I find the active connection/socket and close it

Second: Is there a way I can diagnose what is having an open connection, my hunch is that sqlLite is not closing as it is the last thing I implemented.

This is a one file application (minus a config file and static content) so I can post the code if required.

Error generated (Folder locations changed):

/Development/flask_projects/test_email/env/bin/python /Development/flask_projects/test_email/app.py  * Running on http://127.0.0.1:5000/ Traceback (most recent call last):   File "Development/flask_projects/test_email/app.py", line 58, in <module>     app.run()   File "Development/flask_projects/wtchn_email/env/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/app.py", line 703, in run     run_simple(host, port, self, **options)   File "/Library/Python/2.7/site-packages/Werkzeug-0.7.1-py2.7.egg/werkzeug/serving.py", line 612, in run_simple     test_socket.bind((hostname, port))   File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 224, in meth     return getattr(self._sock,name)(*args) socket.error: [Errno 48] Address already in use 
like image 949
Jay Avatar asked Oct 09 '11 13:10

Jay


People also ask

How do I close a python socket server?

You need to call shutdown() first and then close(), and shutdown takes an argument. I modified your close() to do this and it works.

How do I close a socket server?

The close() method of ServerSocket class is used to close this socket. Any thread currently blocked in accept() will throw a SocketException. The associated channel is also closed by it if it has any.

Does Python automatically close socket?

Python takes the automatic shutdown a step further, and says that when a socket is garbage collected, it will automatically do a close if it's needed.

How do you check if a socket is connected disconnected in Python?

Short answer: use a non-blocking recv(), or a blocking recv() / select() with a very short timeout. Long answer: The way to handle socket connections is to read or write as you need to, and be prepared to handle connection errors.


2 Answers

If you use linux you can use lsof to find out which process is using a given port, you might have to install it first though, usage is pretty simple:

lsof -i :5000 
like image 135
Ole Avatar answered Dec 29 '22 10:12

Ole


To kill the python process which is listening on port 5000 :

sudo lsof -i :5000 | grep "python" | cut -d " " -f3 | xargs kill -9

like image 22
R‌‌‌.. Avatar answered Dec 29 '22 09:12

R‌‌‌..