Learning Python Network Programming

(Sean Pound) #1

Network Programming and Python


print('Must supply an RFC number as first argument')
sys.exit(2)

host = 'www.ietf.org'
port = 80
sock = socket.create_connection((host, port))

req = (
'GET /rfc/rfc{rfcnum}.txt HTTP/1.1\r\n'
'Host: {host}:{port}\r\n'
'User-Agent: Python {version}\r\n'
'Connection: close\r\n'
'\r\n'
)
req = req.format(
rfcnum=rfc_number,
host=host,
port=port,
version=sys.version_info[0]
)
sock.sendall(req.encode('ascii'))
rfc_raw = bytearray()
while True:
buf = sock.recv(4096)
if not len(buf):
break
rfc_raw += buf
rfc = rfc_raw.decode('utf-8')
print(rfc)

The first noticeable change is that we have used socket instead of urllib. Socket
is Python's interface for the operating system's TCP and UDP implementation. The
command-line check remains the same, but then we will see that we now need to
handle some of the things that urllib was doing for us before.


We have to tell socket which transport layer protocol that we want to use.
We do this by using the socket.create_connection() convenience function. This
function will always create a TCP connection. You'll notice that we have to explicitly
supply the TCP port number that socket should use to establish the connection as
well. Why 80? 80 is the standard port number for web services over HTTP. We've
also had to separate the host from the URL, since socket has no understanding
of URLs.

Free download pdf