Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.
Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.
This chapter gives you understanding on most famous concept in Networking - Socket Programming.
What is Socket?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate within a process, between processes on the same machine, or between processes on different continents.
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest.
Socket Module
To create a socket, you must use the socket.socket() function available in socket module, which has the general syntax:
s = socket.socket (socket_family, socket_type, protocol=0)
Server Socket Method
| Method | Explanation |
|---|---|
| s.bind() | This method binds address (hostname, port number pair) to socket. |
| s.listen() | This method sets up and start TCP listener. |
| s.accept() | This passively accept TCP client connection, waiting until connection arrives (blocking). |
Client Socket Method
| Method | Explanation |
|---|---|
| s.connect() | This method actively initiates TCP server connection. |
General Socket Methods
| Method | Explanation |
|---|---|
| s.recv() | This method receives TCP message |
| s.send() | This method transmits TCP message |
| s.recvfrom() | This method receives UDP message |
| s.sendto() | This method transmits UDP message |
| s.close() | This method closes socket |
| socket.gethostname() | Returns the hostname. |
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, 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)
c.send('Thank you for connecting')
c.close() # Close the connection
Simple Server
To write an Internet server, we use the socket function available in socket module to create a socket object. The socket object is then used to call other functions to setup a socket server.
Now call bind(hostname,port) function to specify a port for your service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the port you specified, and then returns a connection object that represents the connection to that client.
Simple Client
Let us write a very simple client program which opens a connection to a given port 12345 and a given host. This is very simple to create a socket client using the Python's socket module function.
The socket.connect(hostname, port) opens a TCP connection to hostname on the port. Once you have a socket open, you can read from it like any IO object. When done, do not forget to close it, as you would close a file.
The following code is a very simple client that connects to a given host and port, reads any available data from the socket, and then exits:
#!/usr/bin/python # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print (s.recv(1024))
s.close # Close the socket when done
Now run this server.py in background and then run above client.py to see the result.
Run server:
python server.py &
After server runs, continue
Run client:
python client.py
The result will be like this :
Got connection from ('127.0.0.1', 48437)
Thank you for connecting
Python Internet Modules
Here is a list of some important modules in Python Network / Internet programming.
| Protocol | Common function | Port No | Python module |
|---|---|---|---|
| HTTP | Web pages | 80 | httplib, urllib, xmlrpclib |
| NNTP | Usenet news | 119 | nntplib |
| FTP | File transfer | 20 | ftplib, urllib |
| SMTP | Sending email | 25 | smtplib |
| POP3 | Fetching email | 110 | poplib |
| IMAP4 | Fetching email | 143 | imaplib |
| Telnet | Command lines | 23 | telnetlib |
| Gopher | Document transfers | 70 | gopherlib, urllib |
Gabung Komunitas Developer & Kreator Digital
Dapatkan teman coding, sharing project, networking dengan expert, dan update teknologi terbaru.
Selamat! Anda telah sukses mendaftar di newsletter.