当前位置: 首页>>代码示例>>Python>>正文


Python web.head函数代码示例

本文整理汇总了Python中web.head函数的典型用法代码示例。如果您正苦于以下问题:Python head函数的具体用法?Python head怎么用?Python head使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了head函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: short

def short(text):
    """
    This function creates a bitly url for each url in the provided "text" string.
    The return type is a list.
    """

    if not bitly_loaded: return [ ]
    bitlys = [ ]
    try:
        a = re.findall(url_finder, text)
        k = len(a)
        i = 0
        while i < k:
            b = str(a[i][0])
            if not b.startswith("http://bit.ly") or not b.startswith("http://j.mp/"):
                # check to see if the url is valid
                try: c = web.head(b)
                except: return [[None, None]]

                url = "http://api.j.mp/v3/shorten?login=%s&apiKey=%s&longUrl=%s&format=txt" % (bitly_user, bitly_api_key, urllib2.quote(b))
                shorter = web.get(url)
                shorter.strip()
                bitlys.append([b, shorter])
            i += 1
        return bitlys
    except:
        return
开发者ID:vaicine,项目名称:chedder,代码行数:27,代码来源:url.py

示例2: val

def val(phenny, input): 
   """Check a webpage using the W3C Markup Validator."""
   if not input.group(2):
      return phenny.reply("Nothing to validate.")
   uri = input.group(2)
   if not uri.startswith('http://'): 
      uri = 'http://' + uri
      
   logging.debug('Getting W3C validation for ' + uri)

   path = '/check?uri=%s;output=xml' % web.urllib.quote(uri)
   info = web.head('http://validator.w3.org' + path)

   result = uri + ' is '

   if isinstance(info, list): 
      return phenny.say('Got HTTP response %s' % info[1])

   if info.has_key('X-W3C-Validator-Status'): 
      result += str(info['X-W3C-Validator-Status'])
      if info['X-W3C-Validator-Status'] != 'Valid': 
         if info.has_key('X-W3C-Validator-Errors'): 
            n = int(info['X-W3C-Validator-Errors'].split(' ')[0])
            if n != 1: 
               result += ' (%s errors)' % n
            else: result += ' (%s error)' % n
   else: result += 'Unvalidatable: no X-W3C-Validator-Status'
   
   logging.debug('Validator: ' + result)

   phenny.reply(result)
开发者ID:TCFMark,项目名称:lackbot,代码行数:31,代码来源:validate.py

示例3: val

def val(jenny, input):
    """Check a webpage using the W3C Markup Validator."""
    uri = input.group(2)
    if not uri.startswith("http://"):
        uri = "http://" + uri

    path = "/check?uri=%s;output=xml" % web.urllib.quote(uri)
    info = web.head("http://validator.w3.org" + path)

    result = uri + " is "

    if isinstance(info, list):
        return jenny.say("Got HTTP response %s" % info[1])

    if info.has_key("X-W3C-Validator-Status"):
        result += str(info["X-W3C-Validator-Status"])
        if info["X-W3C-Validator-Status"] != "Valid":
            if info.has_key("X-W3C-Validator-Errors"):
                n = int(info["X-W3C-Validator-Errors"].split(" ")[0])
                if n != 1:
                    result += " (%s errors)" % n
                else:
                    result += " (%s error)" % n
    else:
        result += "Unvalidatable: no X-W3C-Validator-Status"

    jenny.reply(result)
开发者ID:myano,项目名称:jeden,代码行数:27,代码来源:validate.py

示例4: val

def val(torp, input): 
   """Check a webpage using the W3C Markup Validator."""
   uri = input.group(2)
   if not uri.startswith('http://'): 
      uri = 'http://' + uri

   path = '/check?uri=%s;output=xml' % web.urllib.quote(uri)
   info = web.head('http://validator.w3.org' + path)

   result = uri + ' is '

   if isinstance(info, list): 
      return torp.say('Got HTTP response %s' % info[1])

   if info.has_key('X-W3C-Validator-Status'): 
      result += str(info['X-W3C-Validator-Status'])
      if info['X-W3C-Validator-Status'] != 'Valid': 
         if info.has_key('X-W3C-Validator-Errors'): 
            n = int(info['X-W3C-Validator-Errors'].split(' ')[0])
            if n != 1: 
               result += ' (%s errors)' % n
            else: result += ' (%s error)' % n
   else: result += 'Unvalidatable: no X-W3C-Validator-Status'

   torp.reply(result)
开发者ID:endenizen,项目名称:torp,代码行数:25,代码来源:validate.py

示例5: service

def service(code, input, command, args):
    t = o.services[command]
    template = t.replace('${args}', urllib.quote(args.encode('utf-8'), ''))
    template = template.replace('${nick}', urllib.quote(input.nick, ''))
    uri = template.replace('${sender}', urllib.quote(input.sender, ''))

    info = web.head(uri)
    if isinstance(info, list):
        info = info[0]
    if not 'text/plain' in info.get('content-type', '').lower():
        return code.reply(code.color('red', 'Sorry, the service didn\'t respond in plain text.'))
    bytes = web.get(uri)
    lines = bytes.splitlines()
    if not lines:
        return code.reply(code.color('red', 'Sorry, the service didn\'t respond any output.'))
    try: line = lines[0].encode('utf-8')[:350]
    except: line = lines[0][:250]
    if input.group(1) == 'urban':
        if line.find('ENOTFOUND') > -1:
            line = "I'm sorry, that definition %s found." % code.bold('wasn\'t')
            code.say(line)
        elif line.find('Traceback (most recent call last)') > -1:
            line = code.color('red', 'Failed to search for that definition. Please try again.')
            code.say(line)
        else:
            code.say(line)
开发者ID:Sandfreak1,项目名称:Code,代码行数:26,代码来源:oblique.py

示例6: head

def head(jenni, input):
    """Provide HTTP HEAD information."""
    uri = input.group(2)
    uri = (uri or "").encode("utf-8")
    if " " in uri:
        uri, header = uri.rsplit(" ", 1)
    else:
        uri, header = uri, None

    if not uri and hasattr(jenni, "last_seen_uri"):
        try:
            uri = jenni.last_seen_uri[input.sender]
        except KeyError:
            return jenni.say("?")

    if not uri.startswith("htt"):
        uri = "http://" + uri
    # uri = uri.replace('#!', '?_escaped_fragment_=')

    try:
        info = web.head(uri)
    except IOError:
        return jenni.say("Can't connect to %s" % uri)
    except httplib.InvalidURL:
        return jenni.say("Not a valid URI, sorry.")

    if not isinstance(info, list):
        try:
            info = dict(info)
        except TypeError:
            return jenni.reply("Try .head http://example.org/ [optional header]")
        info["Status"] = "200"
    else:
        newInfo = dict(info[0])
        newInfo["Status"] = str(info[1])
        info = newInfo

    if header is None:
        data = []
        if info.has_key("Status"):
            data.append(info["Status"])
        if info.has_key("content-type"):
            data.append(info["content-type"].replace("; charset=", ", "))
        if info.has_key("last-modified"):
            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 info.has_key("content-length"):
            data.append(info["content-length"] + " bytes")
        jenni.reply(", ".join(data))
    else:
        headerlower = header.lower()
        if info.has_key(headerlower):
            jenni.say(header + ": " + info.get(headerlower))
        else:
            msg = "There was no %s header in the response." % header
            jenni.say(msg)
开发者ID:rnewman,项目名称:jenni,代码行数:57,代码来源:head.py

示例7: head

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 web.HTTPError as e:
        if hasattr(e, 'code'):
            return phenny.say(str(e.code))
        else:
            return phenny.say(str(e.response.status_code))
    except web.ConnectionError:
        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)
开发者ID:Nuruddinjr,项目名称:phenny,代码行数:54,代码来源:head.py

示例8: id_tweet

def id_tweet(tid):
   link = 'https://twitter.com/twitter/status/' + tid
   data = web.head(link)
   message, status = tuple(data)
   if status == 301:
      url = message.get("Location")
      if not url: return "Sorry, couldn't get a tweet from %s" % link
      username = url.split('/')[3]
      tweet = read_tweet(url)
      return format(tweet, username)
   return "Sorry, couldn't get a tweet from %s" % link
开发者ID:zhouqt,项目名称:phenny,代码行数:11,代码来源:twitter.py

示例9: id_tweet

def id_tweet(tid):
    link = "https://twitter.com/twitter/status/" + tid
    headers, status = web.head(link)
    if status == 301:
        if not "Location" in headers:
            return "Sorry, couldn't get a tweet from %s" % link
        url = headers["Location"]
        username = url.split("/")[3]
        tweet = read_tweet(url)
        return format(tweet, username)
    return "Sorry, couldn't get a tweet from %s" % link
开发者ID:asl97,项目名称:minetestbot-modules,代码行数:11,代码来源:twitter.py

示例10: head

def head(phenny, input): 
   """Provide HTTP HEAD information."""
   uri = input.group(2)
   uri = (uri or '').encode('utf-8')
   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_=')
   
   logging.debug('Getting header information for ' + uri)

   try: info = web.head(uri)
   except IOError: return phenny.say("Can't connect to %s" % uri)
   except httplib.InvalidURL: return phenny.say("Not a valid URI, sorry.")

   if not isinstance(info, list): 
      try: info = dict(info)
      except TypeError: 
         return phenny.reply('Try .head http://example.org/ [optional header]')
      info['Status'] = '200'
   else: 
      newInfo = dict(info[0])
      newInfo['Status'] = str(info[1])
      info = newInfo

   if header is None: 
      data = []
      if info.has_key('Status'): 
         data.append(info['Status'])
      if info.has_key('content-type'): 
         data.append(info['content-type'].replace('; charset=', ', '))
      if info.has_key('last-modified'): 
         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 info.has_key('content-length'): 
         data.append(info['content-length'] + ' bytes')
      phenny.reply(', '.join(data))
   else: 
      headerlower = header.lower()
      if info.has_key(headerlower): 
         phenny.say(header + ': ' + info.get(headerlower))
      else: 
         msg = 'There was no %s header in the response.' % header
         phenny.say(msg)
开发者ID:TCFMark,项目名称:lackbot,代码行数:52,代码来源:head.py

示例11: head

def head(phenny, input):
    """Provide HTTP HEAD information."""
    uri = input.group(2)
    uri = (uri or "").encode("utf-8")
    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("?")

    try:
        info = web.head(uri)
    except IOError:
        return phenny.say("Can't connect to %s" % uri)

    if not isinstance(info, list):
        info = dict(info)
        info["Status"] = "200"
    else:
        newInfo = dict(info[0])
        newInfo["Status"] = str(info[1])
        info = newInfo

    if header is None:
        data = []
        if info.has_key("Status"):
            data.append(info["Status"])
        if info.has_key("content-type"):
            data.append(info["content-type"].replace("; charset=", ", "))
        if info.has_key("last-modified"):
            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 info.has_key("content-length"):
            data.append(info["content-length"] + " bytes")
        phenny.reply(", ".join(data))
    else:
        headerlower = header.lower()
        if info.has_key(headerlower):
            phenny.say(header + ": " + info.get(headerlower))
        else:
            msg = "There was no %s header in the response." % header
            phenny.say(msg)
开发者ID:rmccue,项目名称:rmbot,代码行数:48,代码来源:head.py

示例12: service

def service(phenny, input, command, args): 
   t = o.services[command]
   template = t.replace('${args}', urllib.quote(args.encode('utf-8'), ''))
   template = template.replace('${nick}', urllib.quote(input.nick, ''))
   uri = template.replace('${sender}', urllib.quote(input.sender, ''))

   info = web.head(uri)
   if isinstance(info, list): 
      info = info[0]
   if not 'text/plain' in info.get('content-type', '').lower(): 
      return phenny.reply("Sorry, the service didn't respond in plain text.")
   bytes = web.get(uri)
   lines = bytes.splitlines()
   if not lines: 
      return phenny.reply("Sorry, the service didn't respond any output.")
   phenny.say(lines[0][:350])
开发者ID:KB3NZQ,项目名称:phenny,代码行数:16,代码来源:oblique.py

示例13: service

def service(jenni, input, command, args):
    t = o.services[command]
    template = t.replace("${args}", urllib.quote(args.encode("utf-8"), ""))
    template = template.replace("${nick}", urllib.quote(input.nick, ""))
    uri = template.replace("${sender}", urllib.quote(input.sender, ""))

    info = web.head(uri)
    if isinstance(info, list):
        info = info[0]
    if not "text/plain" in info.get("content-type", "").lower():
        return jenni.reply("Sorry, the service didn't respond in plain text.")
    bytes = web.get(uri)
    lines = bytes.splitlines()
    if not lines:
        return jenni.reply("Sorry, the service didn't respond any output.")
    jenni.say(lines[0][:350])
开发者ID:embolalia,项目名称:jenni,代码行数:16,代码来源:oblique.py

示例14: short

def short(text):
    bitlys = [ ]
    try:
        a = re.findall(url_finder, text)
        k = len(a)
        i = 0
        while i < k:
            b = str(a[i][0])
            if not b.startswith("http://bit.ly") or not b.startswith("http://j.mp/"):
                # check to see if the url is valid
                try: c = web.head(b)
                except: return [[None, None]]

                url = "http://api.j.mp/v3/shorten?login=%s&apiKey=%s&longUrl=%s&format=txt" % (bitly_user, bitly_api_key, urllib2.quote(b))
                shorter = web.get(url)
                shorter.strip()
                bitlys.append([b, shorter])
            i += 1
        return bitlys
    except:
        return
开发者ID:Kitsueki,项目名称:jenni,代码行数:21,代码来源:url.py

示例15: tock

def tock(phenny, input): 
    """Shows the time from the USNO's atomic clock."""
    info = web.head('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
    phenny.say('"' + info['Date'] + '" - tycho.usno.navy.mil')
开发者ID:KaiCode2,项目名称:phenny,代码行数:4,代码来源:clock.py


注:本文中的web.head函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。