Python twisted reactor - address already in use

末鹿安然 提交于 2019-12-22 04:08:35

问题


I'm following a tutorial http://www.raywenderlich.com/3932/how-to-create-a-socket-based-iphone-app-and-server for creating a sample using socket programming in Mac OS X enviromment.

I'm writing using post 80 for reactor.listenTCP(80, factory). When I run the server.py file, getting an error:

File "server.py", line 10, in <module>
    reactor.listenTCP(6, factory)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/posixbase.py", line 436, in listenTCP
    p.startListening()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/tcp.py", line 641, in startListening
    raise CannotListenError, (self.interface, self.port, le)
twisted.internet.error.CannotListenError: Couldn't listen on any:80: [Errno 48] Address already in use.

Source code is as follow:

from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor

class IphoneChat(Protocol):
    def connectionMade(self):
        self.factory.clients.append(self)
        print "clients are ", self.factory.clients

    def connectionLost(self, reason):
        self.factory.clients.remove(self)

factory = Factory()
factory.protocol = IphoneChat
factory.clients = []
reactor.listenTCP(80, factory)
print "Iphone Chat server started"
reactor.run()

If I'm using another port no like 6 etc, it is working fine. I just wanted to know, how can I use port 80 for the same application.


回答1:


Open Activity Monitor, search for Python and kill the process. You probably messed up with closing a server once.




回答2:


I encountered this issue too, actually, just now.

Here is what I did:

MacBook-Air:Desktop user$ sudo lsof -i:80
COMMAND  PID USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
Python  1276 root    3u  IPv4 ******      0t0  TCP *:http (LISTEN)
MacBook-Air:Desktop user$ sudo kill 1276
MacBook-Air:Desktop user$ sudo python server.py 
Iphone Chat server started

Then everything goes well.




回答3:


For me the issue was that apache was already listening on port 80. Seems like a lot of people google through this post and other ones with this issue but I don't see this particular answer offered. It was what worked for me so I thought that I would add to it in case it helps: Running Mac/Mavericks but the issue would occur on any other system where Apache runs by default ... or one might forget it's running.

Did

sudo lsof -i TCP:80 | grep LISTEN

Got

httpd      20 root    4u  IPv6 0x1d12a12e12345b12      0t0  TCP *:http (LISTEN)
httpd     109 _www    4u  IPv6 0x1d12a12e12345b12      0t0  TCP *:http (LISTEN)
httpd     437 _www    4u  IPv6 0x1d12a12e12345b12      0t0  TCP *:http (LISTEN)
httpd     438 _www    4u  IPv6 0x1d12a12e12345b12      0t0  TCP *:http (LISTEN)
httpd     439 _www    4u  IPv6 0x1d12a12e12345b12      0t0  TCP *:http (LISTEN)

did

sudo apachectl stop

And the server worked just fine on port 80 after that.




回答4:


You can't listen port if another application already bound socket to this port. For example read socket.bind



来源:https://stackoverflow.com/questions/14640711/python-twisted-reactor-address-already-in-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!