7 socket.setdefaulttimeout(30)
9 from cgi import parse_qsl
11 from m_lib.defenc import default_encoding
13 protocol, request = urllib.splittype(url)
14 user, password, port = None, None, None
15 host, path = urllib.splithost(request)
17 user, host = urllib.splituser(host)
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)
29 for name, value in parse_qsl(query):
30 qlist.append((name, value))
33 host = host.decode(default_encoding).encode('idna')
35 url += urllib.quote(path)
37 url += '?' + urllib.urlencode(qlist)
39 url += '#' + urllib.quote_plus(tag)
42 server = httplib.HTTP(host, port)
43 server.set_debuglevel(1)
45 server.putrequest("HEAD", path)
47 server.putheader("Host", '%s:%d' % (host, port))
49 server.putheader("Host", host)
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)
58 server.putheader('Accept-Charset', "koi8-r;q=1.0")