]> git.phdru.name Git - dotfiles.git/blob - bin/HEAD.py
Feat(bin): Port scripts to Python 3
[dotfiles.git] / bin / HEAD.py
1 #! /usr/bin/env python3
2
3 try:
4     PY2 = False
5     from http.client import HTTPConnection
6     from urllib.parse import parse_qsl, urlencode, \
7         quote, quote_plus, unquote, unquote_plus, \
8         splittype, splithost, splituser, splitpasswd, \
9         splitport, splittag, splitquery
10     import urllib.request
11 except ImportError:
12     PY2 = True
13     from cgi import parse_qsl
14     from urllib import urlencode, quote, quote_plus, unquote, unquote_plus, \
15         splittype, splithost, splituser, splitpasswd, \
16         splitport, splittag, splitquery
17     from httplib import HTTPConnection
18
19 import sys
20 import urllib
21 from m_lib.defenc import default_encoding
22
23 url = sys.argv[1]
24
25 protocol, request = splittype(url)
26 user, password, port = None, None, None
27 host, path = splithost(request)
28 if host:
29     user, host = splituser(host)
30     if user:
31         user, password = splitpasswd(user)
32     host, port = splitport(host)
33     if port: port = int(port)
34 path, tag = splittag(path)
35 path, query = splitquery(path)
36 path = unquote(path)
37 if tag: tag = unquote_plus(tag)
38
39 if query:
40     qlist = []
41     for name, value in parse_qsl(query):
42         qlist.append((name, value))
43
44 url = protocol + "://"
45 if user:
46     url += quote(user)
47     if password:
48         url += ':' + quote(password)
49     url += '@'
50 if host:
51     if PY2:
52         host = host.decode(default_encoding)
53     host = host.encode('idna')
54     if not PY2:
55         host = host.decode('ascii')
56     url += host
57     if port:
58         url += ':%d' % port
59 if path:
60     if protocol == "file":
61         url += quote(path)
62     else:
63         url += quote(path)
64 if query:
65     url += '?' + urlencode(qlist)
66 if tag:
67     url += '#' + quote_plus(tag)
68
69 server = HTTPConnection(host, port)
70 server.set_debuglevel(1)
71
72 server.putrequest("HEAD", path)
73 if port:
74     server.putheader("Host", '%s:%d' % (host, port))
75 else:
76     server.putheader("Host", host)
77
78 # I remember seeing some sites that return broken HTML or even HTTP response
79 # without "compatible" user agent; I don't know if such sites are still around,
80 # but this header doesn't cause any harm so I'd better continue to use it.
81 # UPDATE: I saw a number of sites that forbid "Mozilla compatible"
82 if PY2:
83     urllib_version = urllib.__version__
84 else:
85     urllib_version = urllib.request.__version__
86 client_version = "Python-urllib/%s" % urllib_version
87 server.putheader('User-agent', client_version)
88
89 server.putheader('Accept-Charset', "koi8-r;q=1.0")
90 server.endheaders()
91
92 server.getresponse()