Skip to Content

Open a listening Port with Python

To open a port with Python, you typically create a server socket that listens on a specific port for incoming connections.

Here’s a simple example of a server that listens on a specific port:

import socket

# Create a socket object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Define the port on which you want to connect
port = 12345

# Bind to the port
s.bind(('', port))

# Put the socket into listening mode

print('Server is listening')

# A forever loop until we interrupt it or 
# an error occurs
while True:

    # Establish a connection with the client
    c, addr = s.accept()

    print('Got connection from', addr)

    # Close the connection with the client

In this example, the server is set to listen on port 12345. When a client connects to this port, the server prints a message and then closes the connection.

Please note that you may need to run this script with root privileges if you are opening a port number less than 1024. Also, be aware that opening ports can have security implications, so ensure you have the necessary security measures in place.

This script only opens a port and accepts a connection. If you want to read data from the port, or write data to it, you will need additional code.

understanding socket module in Python

Sockets in Python provide a powerful mechanism for network communication, enabling connections between servers and clients over a network.

In essence, a socket is an endpoint for sending and receiving data. The Python `socket` module, part of the standard library, facilitates creating and managing socket connections for various network protocols.

Python’s socket programming capabilities are extensive and can handle both TCP (Transmission Control Protocol) and UDP (User Datagram Protocol) connections.

TCP is used for connection-oriented communication, ensuring the reliable transmission of data packets in the correct order, making it ideal for applications like web servers.

UDP, on the other hand, is connectionless and suitable for applications that require speed over reliability, such as streaming services.

Key Concepts in Python Socket Programming

  • Socket Creation: Using the socket.socket() function, you can create a new socket, specifying the address family and socket type. For example, socket.AF_INET for IPv4 communications, and socket.SOCK_STREAM for TCP.
  • Binding to an Address: Servers bind their socket to a specific address and port to listen for incoming client connections using the socket.bind() method.
  • Listening for Connections: The socket.listen() method enables a server socket to accept incoming connection requests.
  • Accepting Connections: The socket.accept() method waits for an incoming client connection, establishing a new socket for communication with the client.
  • Connecting to a Server: A client socket initiates a connection to a server using the socket.connect() method with the server’s address and port.
  • Data Transmission: Sockets can send and receive data using the socket.send() and socket.recv() methods, respectively.
  • Closing a Socket: Properly closing a socket using socket.close() is crucial to free up resources.


Python’s socket programming is not limited to internet-based communications but also extends to inter-process communications on the same machine.

Its ease of use and comprehensive standard library makes Python a popular choice for network programming and building a wide range of network applications, from simple data transfer scripts to complex servers and clients.