GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Python Socket - Python network programming with sockets Python Socketlast modified January 29, 2024 Python Socket tutorial shows how to do Python network programming with sockets. Socket programming is low-level. The goal of this tutorial is to introduce network programming including these low-level details. There are higher-level Python APIs such as Twisted that might be better suited. In programming, a socket is an endpoint of a communication between two programs running on a network. Sockets are used to create a connection between a client program and a server program.
Python's
Note: In networking, the term socket has a different meaning.
It is used for the combination of an IP address and a port number.
Network protocolsTCP/IP is a suite of protocols used by devices to communicate over the Internet and most local networks. TCP is more reliable, has extensive error checking, and requires more resources. It is used by services such as HTTP, SMTP, or FTP. UDP is much less reliable, has limited error checking, and requires less resources. It is used by services such as VoIP.
The Address familiesWhen we create a socket, we have to specify its address family. Then we can only use addresses of that type with the socket.
For the Python get IP address
With get_ip.py
#!/usr/bin/python
import socket
ip = socket.gethostbyname('example.com')
print(ip)
The example prints the IP address of $ ./get_ip.py 93.184.216.34 Python UDP socket exampleUDP is a communication protocol that transmits independent packets over the network with no guarantee of arrival and no guarantee of the order of delivery. One service that used UDP is the Quote of the Day (QOTD). qotd_client.py
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
message = b''
addr = ("djxmmx.net", 17)
s.sendto(message, addr)
data, address = s.recvfrom(1024)
print(data.decode())
The example creates a client program that connects to a QOTD service. import socket
We import the with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s: A datagram socket for IPv4 is created. message = b'' We send an empty message; the QOTD service works by sending arbitrary data to the socket; it simply responds with a quote. To communicate over TCP/UDP, we use binary strings. addr = ("djxmmx.net", 17)
We provide the address and the port. s.sendto(message, addr)
We send data with the data, address = s.recvfrom(1024)
UDP sockets use print(data.decode()) We print the decoded data to the terminal. $ ./qotd_client.py
"Oh the nerves, the nerves; the mysteries of this machine called man!
Oh the little that unhinges it, poor creatures that we are!"
Charles Dickens (1812-70)
Python TCP socket exampleThe are servers that provide current time. A client simply connects to the server with no commands, and the server responds with a current time.
Note: Time servers come and go, so we might
need to find a working server on https://www.ntppool.org/en/.
time_client.py
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
host = "time.nist.gov"
port = 13
s.connect((host, port))
s.sendall(b'')
print(str(s.recv(4096), 'utf-8'))
The example determines the current time by connecting to a time server's TCP socket. with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: A TCP socket for IPv4 is created. host = "time.nist.gov" port = 13 This is the host name and the port number of a working time server. s.connect((host, port))
We connect to the remote socket with s.sendall(b'')
The print(str(s.recv(4096), 'utf-8'))
We print the received data. The Python Socket HEAD requestA HEAD request is a GET request without a message body. The header of a request/response contains metadata, such as HTTP protocol version or content type. head_request.py
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("webcode.me" , 80))
s.sendall(b"HEAD / HTTP/1.1\r\nHost: webcode.me\r\nAccept: text/html\r\n\r\n")
print(str(s.recv(1024), 'utf-8'))
In the example, we send a HEAD request to s.sendall(b"HEAD / HTTP/1.1\r\nHost: webcode.me\r\nAccept: text/html\r\n\r\n")
A head request is issued with the $ head_request.py HTTP/1.1 200 OK Server: nginx/1.6.2 Date: Sun, 08 Sep 2019 11:23:25 GMT Content-Type: text/html Content-Length: 348 Last-Modified: Sat, 20 Jul 2019 11:49:25 GMT Connection: keep-alive ETag: "5d32ffc5-15c" Accept-Ranges: bytes Python Socket GET requestThe HTTP GET method requests a representation of the specified resource. Requests using GET should only retrieve data. get_request.py
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect(("webcode.me" , 80))
s.sendall(b"GET / HTTP/1.1\r\nHost: webcode.me\r\nAccept: text/html\r\nConnection: close\r\n\r\n")
while True:
data = s.recv(1024)
if not data:
break
print(data.decode())
The example reads the home page of the s.sendall(b"GET / HTTP/1.1\r\nHost: webcode.me\r\nAccept: text/html\r\nConnection: close\r\n\r\n")
For the HTTP 1.1 protocol, the connections may be persistent by default. This is why we
send the while True:
data = s.recv(1024)
if not data:
break
print(data.decode())
We use a while loop to process the received data. If no error occurs,
$ ./get_request.py
HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Sun, 08 Sep 2019 11:39:34 GMT
Content-Type: text/html
Content-Length: 348
Last-Modified: Sat, 20 Jul 2019 11:49:25 GMT
Connection: keep-alive
ETag: "5d32ffc5-15c"
Access-Control-Allow-Origin: *
Accept-Ranges: bytes
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My html page</title>
</head>
<body>
<p>
Today is a beautiful day. We go swimming and fishing.
</p>
<p>
Hello there. How are you?
</p>
</body>
</html>
Echo client server exampleAn echo server sends the message from the client back. It is a classic example used for testing and learning. echo_server.py
#!/usr/bin/python
import socket
import time
with socket.socket() as s:
host = 'localhost'
port = 8001
s.bind((host, port))
print(f'socket binded to {port}')
s.listen()
con, addr = s.accept()
with con:
while True:
data = con.recv(1024)
if not data:
break
con.sendall(data)
The echo server sends the client message back to the client. host = 'localhost' port = 8001 The server runs on localhost on port 8001. s.bind((host, port))
The s.listen()
The con, addr = s.accept()
With
Note that the echo_client.py
#!/usr/bin/python
import socket
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
host = "localhost"
port = 8001
s.connect((host, port))
s.sendall(b'hello there')
print(str(s.recv(4096), 'utf-8'))
The client sends a message to the echo server. Asynchronous server example
In order to improve the performance of a server, we can use the
async_server.py
#!/usr/bin/python
# from threading import current_thread
import asyncio
async def handle_client(reader, writer):
data = (await reader.read(1024))
writer.write(data)
writer.close()
loop = asyncio.get_event_loop()
loop.create_task(asyncio.start_server(handle_client, 'localhost', 8001))
loop.run_forever()
We can now test the performance of the blocking and non-blocking servers. $ ab -c 50 -n 1000 http://localhost:8001/ For instance, we can test the performance with the Apache benchmarking tool. In our case, the command sends 1000 requests, 50 at a time. SourcePython socket — Low-level networking interface In this article we have showed how to create simple networking programs with sockets in Python. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Python tutorials. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |