Skip to content

Instantly share code, notes, and snippets.

@geberl
Created September 21, 2018 11:02
Show Gist options
  • Save geberl/19de3340979a535abf6bb8f610a70ca7 to your computer and use it in GitHub Desktop.
Save geberl/19de3340979a535abf6bb8f610a70ca7 to your computer and use it in GitHub Desktop.
Get all HTTP headers sent by a host at some port over http/https
#!/usr/local/bin/python3
import http.client
def get_all_headers(host, port, url, connection='https'):
"""
This can be used to test if your web server leaks any info it should rather keep for itself
Examples:
- Server : nginx
- Server : Apache
- Server : Jetty(9.4.z-SNAPSHOT)
- Server : Jetty(9.4.11.v20180605)
"""
if connection == 'https':
conn = http.client.HTTPSConnection(host, port=port, timeout=30)
else:
conn = http.client.HTTPConnection(host, port=port, timeout=30)
conn.request('HEAD', url=url)
response = conn.getresponse()
headers = response.getheaders()
for header in headers:
if type(header) == tuple:
print(' : '.join(header))
else:
print(header)
# Note: The timestamp of the "Date" header says it is GMT, which is used here interchangeably with UTC. Neither GMT
# nor UTC ever change due to daylight saving time adjustments, however countries that use GMT may switch to
# different time zones during DST periods.
# For example Google reports back ('Date', 'Mon, 02 Jul 2018 09:04:14 GMT') at 11:05 Berlin summer time, which
# translates to 10:04 in Greenwich/London - but is 09:04 GMT/UTC.
# If you want GMT/UTC use Reykjavik/Iceland as location (no summer time there), NOT Greenwich/London!
if __name__ == '__main__':
get_all_headers('google.com', 443, 'https://www.google.com/')
# get_all_headers('192.168.10.75', 80, 'http://192.168.10.75/', connection='http')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment