I have this program which is for now supposed to only listen on port 80 and receive data either from browser connections or from another python scripts. this code:
import socket # Import socket module
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname() # Get local machine name
port = 80 # Reserve a port for your service.
s.bind(("192.168.252.7", port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
print c.recv(1024)
c.close() # Close the connection
which is all copied from tutorialspoint. This code receives data, when the port is set to anything but 80 (eg 8080, 12345), but when it is 80, it only accepts the client but seems to not receive any data despite the data being successfully sent from somewhere else.... PLEASE HELP GUYS
Port 80 and all ports <1024 are privileged ports, your program must run as root in order to properly bind to these ports. I'm guessing you are running on Windows, since on any unix calling
s.bind(("127.0.0.1", 80))
results inPermissionError: [Errno 13] Permission denied
exception immediately.I'm not sure how Windows deals with priveleged ports, but quick google search points towards windows firewall messing with your program.
Proper web servers, such as Nginx or Apache, start as root, bind to the port 80 and immediately drop to a less privileged user, since running under root is dangerous.
P.S.: A couple of suggestions:
You can skip the
socket.gethostname()
. Use ip127.0.0.1
if you want your program to be accessible only from your machine, or use ip0.0.0.0
if you want to be accessible from any machine on your network.You should try to switch to Python 3 ASAP, since Python 2 is basically dead at this point. Don't get used to two's syntax, you gonna relearn it in a couple of years tops.