Binding and Listening with Sockets

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.

    s.bind((HOST, PORT))
except socket.error as msg:
    print('Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1])
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.


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.

The next tutorial:

  • Python Introduction
  • Print Function and Strings
  • Math with Python
  • Variables Python Tutorial
  • While Loop Python Tutorial
  • For Loop Python Tutorial
  • If Statement Python Tutorial
  • If Else Python Tutorial
  • If Elif Else Python Tutorial
  • Functions Python Tutorial
  • Function Parameters Python Tutorial
  • Function Parameter Defaults Python Tutorial
  • Global and Local Variables Python Tutorial
  • Installing Modules Python Tutorial
  • How to download and install Python Packages and Modules with Pip
  • Common Errors Python Tutorial
  • Writing to a File Python Tutorial
  • Appending Files Python Tutorial
  • Reading from Files Python Tutorial
  • Classes Python Tutorial
  • Frequently asked Questions Python Tutorial
  • Getting User Input Python Tutorial
  • Statistics Module Python Tutorial
  • Module import Syntax Python Tutorial
  • Making your own Modules Python Tutorial
  • Python Lists vs Tuples
  • List Manipulation Python Tutorial
  • Multi-dimensional lists Python Tutorial
  • Reading CSV files in Python
  • Try and Except Error handling Python Tutorial
  • Multi-Line printing Python Tutorial
  • Python dictionaries
  • Built in functions Python Tutorial
  • OS Module Python Tutorial
  • SYS module Python Tutorial
  • Python urllib tutorial for Accessing the Internet
  • Regular Expressions with re Python Tutorial
  • How to Parse a Website with regex and urllib Python Tutorial
  • Tkinter intro
  • Tkinter buttons
  • Tkinter event handling
  • Tkinter menu bar
  • Tkinter images, text, and conclusion
  • Threading module
  • CX_Freeze Python Tutorial
  • The Subprocess Module Python Tutorial
  • Matplotlib Crash Course Python Tutorial
  • Python ftplib Tutorial
  • Sockets with Python Intro
  • Simple Port Scanner with Sockets
  • Threaded Port Scanner
  • Binding and Listening with Sockets
  • Client Server System with Sockets
  • Python 2to3 for Converting Python 2 scripts to Python 3
  • Python Pickle Module for saving Objects by serialization
  • Eval Module with Python Tutorial
  • Exec with Python Tutorial