Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def head(phenny, input):
- """Provide HTTP HEAD information."""
- uri = input.group(2)
- uri = (uri or '')
- if ' ' in uri:
- uri, header = uri.rsplit(' ', 1)
- else: uri, header = uri, None
- if not uri and hasattr(phenny, 'last_seen_uri'):
- try: uri = phenny.last_seen_uri[input.sender]
- except KeyError: return phenny.say('?')
- if not uri.startswith('htt'):
- uri = 'http://' + uri
- # uri = uri.replace('#!', '?_escaped_fragment_=')
- start = time.time()
- try:
- info = web.head(uri)
- info['status'] = '200'
- except urllib.error.HTTPError as e:
- return phenny.say(str(e.code))
- except http.client.InvalidURL:
- return phenny.say("Not a valid URI, sorry.")
- except IOError:
- return phenny.say("Can't connect to %s" % uri)
- resptime = time.time() - start
- if header is None:
- data = []
- if 'Status' in info:
- data.append(info['Status'])
- if 'content-type' in info:
- data.append(info['content-type'].replace('; charset=', ', '))
- if 'last-modified' in info:
- modified = info['last-modified']
- modified = time.strptime(modified, '%a, %d %b %Y %H:%M:%S %Z')
- data.append(time.strftime('%Y-%m-%d %H:%M:%S UTC', modified))
- if 'content-length' in info:
- data.append(info['content-length'] + ' bytes')
- data.append('{0:1.2f} s'.format(resptime))
- phenny.reply(', '.join(data))
- else:
- headerlower = header.lower()
- if headerlower in info:
- phenny.say(header + ': ' + info.get(headerlower))
- else:
- msg = 'There was no %s header in the response.' % header
- phenny.say(msg)
- head.commands = ['head']
- head.example = '.head http://www.w3.org/'
- r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
- r_entity = re.compile(r'&[A-Za-z0-9#]+;')
Advertisement
Add Comment
Please, Sign In to add comment