X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=bin%2FHEAD.py;fp=bin%2FHEAD.py;h=c5d06a646e669ee657021565c438296852947560;hb=3fdf2f7baaa3a67880de95948d6ee76bc698fbc9;hp=c7b376403f5ded4aff11fedcb2d4916fad29d270;hpb=0477080a03713846f689faf58a3d0df4028e32fc;p=dotfiles.git diff --git a/bin/HEAD.py b/bin/HEAD.py index c7b3764..c5d06a6 100755 --- a/bin/HEAD.py +++ b/bin/HEAD.py @@ -1,61 +1,92 @@ -#! /usr/bin/env python +#! /usr/bin/env python3 -import sys -url = sys.argv[1] +try: + PY2 = False + from http.client import HTTPConnection + from urllib.parse import parse_qsl, urlencode, \ + quote, quote_plus, unquote, unquote_plus, \ + splittype, splithost, splituser, splitpasswd, \ + splitport, splittag, splitquery + import urllib.request +except ImportError: + PY2 = True + from cgi import parse_qsl + from urllib import urlencode, quote, quote_plus, unquote, unquote_plus, \ + splittype, splithost, splituser, splitpasswd, \ + splitport, splittag, splitquery + from httplib import HTTPConnection -import socket -socket.setdefaulttimeout(30) - -from cgi import parse_qsl +import sys import urllib from m_lib.defenc import default_encoding -protocol, request = urllib.splittype(url) +url = sys.argv[1] + +protocol, request = splittype(url) user, password, port = None, None, None -host, path = urllib.splithost(request) +host, path = splithost(request) if host: - user, host = urllib.splituser(host) - if user: - user, password = urllib.splitpasswd(user) - host, port = urllib.splitport(host) - if port: port = int(port) -path, tag = urllib.splittag(path) -path, query = urllib.splitquery(path) -path = urllib.unquote(path) -if tag: tag = urllib.unquote_plus(tag) + user, host = splituser(host) + if user: + user, password = splitpasswd(user) + host, port = splitport(host) + if port: port = int(port) +path, tag = splittag(path) +path, query = splitquery(path) +path = unquote(path) +if tag: tag = unquote_plus(tag) if query: - qlist = [] - for name, value in parse_qsl(query): - qlist.append((name, value)) + qlist = [] + for name, value in parse_qsl(query): + qlist.append((name, value)) -url = '' -host = host.decode(default_encoding).encode('idna') +url = protocol + "://" +if user: + url += quote(user) + if password: + url += ':' + quote(password) + url += '@' +if host: + if PY2: + host = host.decode(default_encoding) + host = host.encode('idna') + if not PY2: + host = host.decode('ascii') + url += host + if port: + url += ':%d' % port if path: - url += urllib.quote(path) + if protocol == "file": + url += quote(path) + else: + url += quote(path) if query: - url += '?' + urllib.urlencode(qlist) + url += '?' + urlencode(qlist) if tag: - url += '#' + urllib.quote_plus(tag) + url += '#' + quote_plus(tag) -import httplib -server = httplib.HTTP(host, port) +server = HTTPConnection(host, port) server.set_debuglevel(1) server.putrequest("HEAD", path) if port: server.putheader("Host", '%s:%d' % (host, port)) else: - server.putheader("Host", host) + server.putheader("Host", host) # I remember seeing some sites that return broken HTML or even HTTP response # without "compatible" user agent; I don't know if such sites are still around, # but this header doesn't cause any harm so I'd better continue to use it. # UPDATE: I saw a number of sites that forbid "Mozilla compatible" -client_version = "Python-urllib/%s" % urllib.__version__ +if PY2: + urllib_version = urllib.__version__ +else: + urllib_version = urllib.request.__version__ +client_version = "Python-urllib/%s" % urllib_version server.putheader('User-agent', client_version) server.putheader('Accept-Charset', "koi8-r;q=1.0") server.endheaders() -server.getreply() +server.getresponse()