本文介绍了需要关闭python socket /在我的开发环境中找到当前正在运行的服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图学习Python(使用Flask微框架),我很困惑,因为在我的代码中的某个地方,我保持服务器打开,我相信。



真的有两个问题。



第一:如何找到活动的连接/套接字并将其关闭



这是一个文件应用程序(减去一个配置文件和静态内容),所以我可以发布的代码,如果需要的话。

改变):

$ $ p $ / Development / flask_projects / test_email / env / bin / python /Development/flask_projects/test_email/app.py
*运行于http://127.0.0.1:5000/
Traceback(最近的最后一次调用):
在<第58行中的Development / flask_projects / test_email / app.py文件;模块>
app.run()
文件Development / flask_projects / wtchn_email / env / lib / python2.7 / site-packages / Flask-0.8-py2.7.egg / flask / app.py,第703行,运行
run_simple(host,port,self,** options)
文件/Library/Python/2.7/site-packages/Werkzeug-0.7.1-py2.7.egg/ werkzeug / serving.py,第612行,在run_simple
test_socket.bind((hostname,port))
文件/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2 (self._sock,name)(* args)
socket.error:[Errno 48]已经在使用的地址


解决方案

如果使用linux,则可以使用 lsof 要找出哪个进程正在使用给定的端口,你可能需要先安装它,但用法很简单:

  lsof -i:5000 


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
解决方案

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

这篇关于需要关闭python socket /在我的开发环境中找到当前正在运行的服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 07:20