-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhead_http.py
74 lines (54 loc) · 1.4 KB
/
head_http.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python
"""
Description
head_http.py - print URL http response header
Usage
python head_http.py url
Output
#$># python head_http.py http://github.com
http://github.com:
Server: GitHub.com
Date: Wed, 03 Jun 2015 21:03:18 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 200 OK
...
Note
- works with python 2.7 and 3.6
Author
David Laperriere <[email protected]>
"""
import re
import sys
import time
try:
import urllib.request as urllib2
except ImportError:
import urllib2
__version_info__ = (1, 0)
__version__ = '.'.join(map(str, __version_info__))
__author__ = "David Laperriere [email protected]"
def main():
""" Main check parameters & get http response header """
timeout = 3
# check parameters
if len(sys.argv) == 2:
url = sys.argv[1]
else:
print("Usage: python head_http.py url")
sys.exit(0)
if not re.match('(?:http|https)://', url):
url = "http://" + url
try:
request = urllib2.Request(url)
response = urllib2.urlopen(request)
time.sleep(timeout)
except urllib2.URLError as e:
print(url + " error : \n" + str(e.reason))
sys.exit(-1)
print(url + ":\n")
# print ("Status: " + str(response.getcode()))
print(response.info())
if __name__ == "__main__":
main()