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=
| 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,直接强制转换就行了。