python httplib2 超时以及一些问题

1、python连接一个网页

 h = httplib2.Http()
 res,con = h.request('http://www.xxx.com','GET')

但是发现,如果这个网站宕机了,那么这个就会停留很久很久,那么我如何快速知道这个问题呢,这个就是超时;

可以这么写:

 h = httplib2.Http(timeout=0.1)
 res,con = h.request('http://www.xxx.com','GET')

那么,0.1秒过去了,还没有反应就会知道是不是远程宕机了。Http里面的参数可不止这么一点点啊。

httplib2.Http(self, cache=None, timeout=None, proxy_info=None)
用help(httplib2.Http)就能看到

>>> help(httplib2.Http)
Help on class Http in module httplib2:


class Http(__builtin__.object)
 |  An HTTP client that handles:
 |
 |  - all methods
 |  - caching
 |  - ETags
 |  - compression,
 |  - HTTPS
 |  - Basic
 |  - Digest
 |  - WSSE
 |
 |  and more.
 |
 |  Methods defined here:
 |
 |  __getstate__(self)
 |
 |  __init__(self, cache=None, timeout=None, proxy_info=<function proxy_info_fro
m_environment>, ca_certs=None, disable_ssl_certificate_validation=False)
 |      If 'cache' is a string then it is used as a directory name for
 |      a disk cache. Otherwise it must be an object that supports the
 |      same interface as FileCache.
 |
 |      All timeouts are in seconds. If None is passed for timeout
 |      then Python's default timeout for sockets will be used. See
 |      for example the docs of socket.setdefaulttimeout():
 |      http://docs.python.org/library/socket.html#socket.setdefaulttimeout
 |
 |      `proxy_info` may be:
 |        - a callable that takes the http scheme ('http' or 'https') and
 |          returns a ProxyInfo instance per request. By default, uses
 |          proxy_nfo_from_environment.
 |        - a ProxyInfo instance (static proxy config).
 |        - None (proxy disabled).
 |
 |      ca_certs is the path of a file containing root CA certificates for SSL
 |      server certificate validation.  By default, a CA cert file bundled with
 |      httplib2 is used.
 |
 |      If disable_ssl_certificate_validation is true, SSL cert validation will
 |      not be performed.
 |
 |  __setstate__(self, state)
 |
 |  add_certificate(self, key, cert, domain)
 |      Add a key and cert that will be used
 |      any time a request requires authentication.
 |
 |  add_credentials(self, name, password, domain='')
 |      Add a name and password that will be used
 |      any time a request requires authentication.
 |
 |  clear_credentials(self)
 |      Remove all the names and passwords
 |      that are used for authentication
 |
 |  request(self, uri, method='GET', body=None, headers=None, redirections=5, co
nnection_type=None)
 |      Performs a single HTTP request.
 |
 |      The 'uri' is the URI of the HTTP resource and can begin with either
 |      'http' or 'https'. The value of 'uri' must be an absolute URI.
 |
 |      The 'method' is the HTTP method to perform, such as GET, POST, DELETE,
 |      etc. There is no restriction on the methods allowed.
 |
 |      The 'body' is the entity body to be sent with the request. It is a
 |      string object.
 |
 |      Any extra headers that are to be sent with the request should be
 |      provided in the 'headers' dictionary.
 |
 |      The maximum number of redirect to follow before raising an
 |      exception is 'redirections. The default is 5.
 |
 |      The return value is a tuple of (response, content), the first
 |      being and instance of the 'Response' class, the second being
 |      a string that contains the response entity body.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables (if defined)
 |
 |  __weakref__
 |      list of weak references to the object (if defined)


2、对于python中处理IP地址这样的数据,是非常好处理的。

记得两个函数,一个是join,一个是split,那么这个问题非常容易解决。

int-->string,有一个函数str,这个我之前有讲过。

string-->int,直接强制转换就行了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值