Up until this point, I have shown you how you can use the socket module to make use of socket connections permitted by other clients and programs. Now it is time to cover how to do this ourselves!
The way this is done should sound fairly expectable, as you know the requirements of sockets. First you bind a socket, then you listen on a port for incoming connections.
import socket import sys HOST = '' PORT = 5555 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Socket import for sockets, sys import to execute a quit if there is a failure.
Next, we specify our host and port. You don't really need the host to be specified, but it is good practice to include it in your code, so we have. Finally, we're specifying a port. Pick whatever you want, just choose a high number so it hopefully doesn't conflict with another program of yours.
try: s.bind((HOST, PORT)) except socket.error as msg: print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]) sys.exit() print('Socket bind complete')
Here, we are simply attempeting to bind a socket locally, on port 5555. If that fails, then we post the error and exit the program.
s.listen(10) conn, addr = s.accept() print('Connected with ' + addr[0] + ':' + str(addr[1]))
Next, we're going to use s.listen to listen. Here, the "10" stands for how many incoming connections we're willing to queue before denying any more.
Now you can run your script. Once you've done that, you should be able to make a connection. You will likely get a security notifcation that you must accept in order to continue with the tutorial. You are getting this notification because the program is trying to open and listen for incoming connections on your behalf. As you might imagine, people might attempt to give you malicious software to do just this.
Accept the warning if you get one, and now you should be able to telnet localhost on the port you chose, which was 5555 for me. So, opening bash, a shell, or cmd.exe, type:
telnet localhost 5555
For now, nothing much will happen, but you should see a black window and your running python script should update with the incoming connection address.