March 2019
Beginner
490 pages
12h 40m
English
Now, we are going to create the same script but, instead of using urllib or requests, we are going to use the socket module for working at a low level. For this, create a text file called RFC_download_socket.py:
#!/usr/bin/env python3import sys, sockettry: rfc_number = int(sys.argv[1])except (IndexError, ValueError): print('Must supply an RFC number as first argument') sys.exit(2)host = 'www.rfc-editor.org'port = 80sock = 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')) ...Read now
Unlock full access