]> git.phdru.name Git - dotfiles.git/blob - bin/HEAD.py
Initial import
[dotfiles.git] / bin / HEAD.py
1 #! /usr/bin/env python
2
3 import sys
4 url = sys.argv[1]
5
6 import socket
7 socket.setdefaulttimeout(30)
8
9 from cgi import parse_qsl
10 import urllib
11 from m_lib.defenc import default_encoding
12
13 protocol, request = urllib.splittype(url)
14 user, password, port = None, None, None
15 host, path = urllib.splithost(request)
16 if host:
17    user, host = urllib.splituser(host)
18    if user:
19       user, password = urllib.splitpasswd(user)
20    host, port = urllib.splitport(host)
21    if port: port = int(port)
22 path, tag = urllib.splittag(path)
23 path, query = urllib.splitquery(path)
24 path = urllib.unquote(path)
25 if tag: tag = urllib.unquote_plus(tag)
26
27 if query:
28    qlist = []
29    for name, value in parse_qsl(query):
30        qlist.append((name, value))
31
32 url = ''
33 host = host.decode(default_encoding).encode('idna')
34 if path:
35    url += urllib.quote(path)
36 if query:
37    url += '?' + urllib.urlencode(qlist)
38 if tag:
39    url += '#' + urllib.quote_plus(tag)
40
41 import httplib
42 server = httplib.HTTP(host, port)
43 server.set_debuglevel(1)
44
45 server.putrequest("HEAD", path)
46 if port:
47     server.putheader("Host", '%s:%d' % (host, port))
48 else:
49    server.putheader("Host", host)
50
51 # I remember seeing some sites that return broken HTML or even HTTP response
52 # without "compatible" user agent; I don't know if such sites are still around,
53 # but this header doesn't cause any harm so I'd better continue to use it.
54 # UPDATE: I saw a number of sites that forbid "Mozilla compatible"
55 client_version = "Python-urllib/%s" % urllib.__version__
56 server.putheader('User-agent', client_version)
57
58 server.putheader('Accept-Charset', "koi8-r;q=1.0")
59 server.endheaders()
60
61 server.getreply()