Skip to content

Instantly share code, notes, and snippets.

@bityob
Created July 28, 2017 16:10
Show Gist options
  • Save bityob/c9dd8c7c9cd923acb8eab121f4b2d560 to your computer and use it in GitHub Desktop.
Save bityob/c9dd8c7c9cd923acb8eab121f4b2d560 to your computer and use it in GitHub Desktop.
HTTP Request using telnetlib (Python 3)
from telnetlib import Telnet
with Telnet('www.example.com', '80') as tn:
req = "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n".encode("ascii")
tn.write(req)
while True:
row = tn.read_until('\n'.encode('ascii'))
print(row)
b'HTTP/1.1 200 OK\r\n'
b'Cache-Control: max-age=604800\r\n'
b'Content-Type: text/html\r\n'
b'Date: Fri, 28 Jul 2017 16:09:34 GMT\r\n'
b'Etag: "359670651+gzip+ident"\r\n'
b'Expires: Fri, 04 Aug 2017 16:09:34 GMT\r\n'
b'Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT\r\n'
b'Server: ECS (dca/24D3)\r\n'
b'Vary: Accept-Encoding\r\n'
b'X-Cache: HIT\r\n'
b'Content-Length: 1270\r\n'
b'\r\n'
b'<!doctype html>\n'
b'<html>\n'
b'<head>\n'
b' <title>Example Domain</title>\n'
b'\n'
b' <meta charset="utf-8" />\n'
b' <meta http-equiv="Content-type" content="text/html; charset=utf-8" />\n'
b' <meta name="viewport" content="width=device-width, initial-scale=1" />\n'
b' <style type="text/css">\n'
b' body {\n'
b' background-color: #f0f0f2;\n'
b' margin: 0;\n'
b' padding: 0;\n'
b' font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;\n'
b' \n'
b' }\n'
b' div {\n'
b' width: 600px;\n'
b' margin: 5em auto;\n'
b' padding: 50px;\n'
b' background-color: #fff;\n'
b' border-radius: 1em;\n'
b' }\n'
b' a:link, a:visited {\n'
b' color: #38488f;\n'
b' text-decoration: none;\n'
b' }\n'
b' @media (max-width: 700px) {\n'
b' body {\n'
b' background-color: #fff;\n'
b' }\n'
b' div {\n'
b' width: auto;\n'
b' margin: 0 auto;\n'
b' border-radius: 0;\n'
b' padding: 1em;\n'
b' }\n'
b' }\n'
b' </style> \n'
b'</head>\n'
b'\n'
b'<body>\n'
b'<div>\n'
b' <h1>Example Domain</h1>\n'
b' <p>This domain is established to be used for illustrative examples in documents. You may use this\n'
b' domain in examples without prior coordination or asking for permission.</p>\n'
b' <p><a href="http://www.iana.org/domains/example">More information...</a></p>\n'
b'</div>\n'
b'</body>\n'
b'</html>\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment