I am currently working on a project to create a TCP server on mac os mountain lion. I wrote a script called: Server.py
Within this python script, I used twisted to listen upon port 80 as shown below:
reactor.listenTCP(80, factory)
reactor.run()
How ever I am getting errors as such:
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 13] Permission denied.
I am thinking is it because the tcp.py script has the wrong permission for admin user? or is there a way to set permission on the port < 1024? (To change the permission, how would it affects the security of such a server?)
Any better solutions is apprieciated.
Just go to the terminal and type sudo python server.py. Now it will ask password, type your password. Your problem will be solved. Happy coding
I solved this issue using command sudo + starting your server.
It sounds like you're on the right track. By convention, only a super-user (or, in some newer systems, a normal user which has been granted certain special powers) is allowed to bind to ports below 1024.
The authbind
tool is a handy way to grant this privilege to non-super-users. There appears to be an OS X port, https://github.com/Castaglia/MacOSX-authbind (though I've only ever used authbind on Linux, myself).
Another approach is to have launchd bind the port for you and hand it off to your Twisted program. This approach is more typical of what you might find people doing on OS X, and is accomplished using (the fairly recently introduced) IReactorSocket.adoptStreamPort
API. See the launchd documentation for details about how to configure this and how your Twisted program will learn where the socket it is to adopt.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With