Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. The server forms the listener socket while the client reaches out to the server.
Note: For more information, refer Socket Programming in Python
Binding and Listening with Sockets
A server has a bind() method which binds it to a specific IP and port so that it can listen to incoming requests on that IP and port. A server has a
listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And last a server has an
close() method. The accept method initiates a connection with the client and the close method closes the connection with the client.
- First of all we import socket which is necessary.
- Then we made a socket object and reserved a port on our pc.
- After that we bound our server to the specified port. Passing an empty string means that the server can listen to incoming connections from other computers as well. If we would have passed 127.0.0.1 then it would have listened to only those calls made within the local computer.
- After that we put the server into listen mode. 9 here means that 9 connections are kept waiting if the server is busy and if a 10th socket tries to connect then the connection is refused.
Now we need something with which a server can interact. We could tenet to the server like this just to know that our server is working. Type these commands in the terminal:
# start the server $ python server.py
Keep the above terminal open now open another terminal and type:
$ telnet localhost 12345
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Sockets | Python
- Python - Simple Port Scanner with Sockets
- Simple Port Scanner using Sockets in Python
- Threaded Port Scanner using Sockets in Python
- Python | Binding function in Tkinter
- Binding Function with double click with Tkinter ListBox
- Important differences between Python 2.x and Python 3.x with examples
- Creating and updating PowerPoint Presentations in Python using python - pptx
- Loops and Control Statements (continue, break and pass) in Python
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Python | Using variable outside and inside the class and method
- Releasing GIL and mixing threads from C and Python
- Python | Boolean List AND and OR operations
- Difference between 'and' and '&' in Python
- Replace the column contains the values 'yes' and 'no' with True and False In Python-Pandas
- Ceil and floor of the dataframe in Pandas Python – Round up and Truncate
- Login Application and Validating info using Kivy GUI and Pandas in Python
- Get the city, state, and country names from latitude and longitude using Python
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.