Playing Around with the telnetlib Library


In [23]:
import telnetlib

Using telnetlib to connect to a HTTP server


In [36]:
with telnetlib.Telnet('now.httpbin.org', '80') as conn:
    conn.write(b'GET / HTTP/1.1\r\nHOST: now.httpbin.org\r\n\r\n')
    resp = conn.read_until(b']}').decode('utf-8')

print(resp)


HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.7.1
Date: Thu, 30 Nov 2017 16:45:24 GMT
Content-Length: 285
Content-Type: application/json
Via: 1.1 vegur

{"now": {"epoch": 1512060324.3595078, "slang_date": "today", "slang_time": "now", "iso8601": "2017-11-30T16:45:24.359508Z", "rfc2822": "Thu, 30 Nov 2017 16:45:24 GMT", "rfc3339": "2017-11-30T16:45:24.35Z"}, "urls": ["/", "/docs", "/when/:human-timestamp", "/parse/:machine-timestamp"]}