askcompu

Untitled

Jul 21st, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.88 KB | None | 0 0
  1. def head(phenny, input):
  2.     """Provide HTTP HEAD information."""
  3.     uri = input.group(2)
  4.     uri = (uri or '')
  5.     if ' ' in uri:
  6.         uri, header = uri.rsplit(' ', 1)
  7.     else: uri, header = uri, None
  8.  
  9.     if not uri and hasattr(phenny, 'last_seen_uri'):
  10.         try: uri = phenny.last_seen_uri[input.sender]
  11.         except KeyError: return phenny.say('?')
  12.  
  13.     if not uri.startswith('htt'):
  14.         uri = 'http://' + uri
  15.     # uri = uri.replace('#!', '?_escaped_fragment_=')
  16.    
  17.     start = time.time()
  18.  
  19.     try:
  20.         info = web.head(uri)
  21.         info['status'] = '200'
  22.     except urllib.error.HTTPError as e:
  23.         return phenny.say(str(e.code))
  24.     except http.client.InvalidURL:
  25.         return phenny.say("Not a valid URI, sorry.")
  26.     except IOError:
  27.         return phenny.say("Can't connect to %s" % uri)
  28.  
  29.     resptime = time.time() - start
  30.  
  31.     if header is None:
  32.         data = []
  33.         if 'Status' in info:
  34.             data.append(info['Status'])
  35.         if 'content-type' in info:
  36.             data.append(info['content-type'].replace('; charset=', ', '))
  37.         if 'last-modified' in info:
  38.             modified = info['last-modified']
  39.             modified = time.strptime(modified, '%a, %d %b %Y %H:%M:%S %Z')
  40.             data.append(time.strftime('%Y-%m-%d %H:%M:%S UTC', modified))
  41.         if 'content-length' in info:
  42.             data.append(info['content-length'] + ' bytes')
  43.         data.append('{0:1.2f} s'.format(resptime))
  44.         phenny.reply(', '.join(data))
  45.     else:
  46.         headerlower = header.lower()
  47.         if headerlower in info:
  48.             phenny.say(header + ': ' + info.get(headerlower))
  49.         else:
  50.             msg = 'There was no %s header in the response.' % header
  51.             phenny.say(msg)
  52. head.commands = ['head']
  53. head.example = '.head http://www.w3.org/'
  54.  
  55. r_title = re.compile(r'(?ims)<title[^>]*>(.*?)</title\s*>')
  56. r_entity = re.compile(r'&[A-Za-z0-9#]+;')
Advertisement
Add Comment
Please, Sign In to add comment