zoukankan      html  css  js  c++  java
  • Urllib库

    urllib.request.urlopen(urldata=None[timeout]*cafile=Nonecapath=Nonecadefault=Falsecontext=None)

    Open the URL url, which can be either a string or a Request object.

    data must be an object specifying additional data to be sent to the server, or None if no such data is needed. See Request for details.

    urllib.request module uses HTTP/1.1 and includes Connection:close header in its HTTP requests.

    The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

    If context is specified, it must be a ssl.SSLContext instance describing the various SSL options. See HTTPSConnection for more details.

    The optional cafile and capath parameters specify a set of trusted CA certificates for HTTPS requests. cafile should point to a single file containing a bundle of CA certificates, whereas capathshould point to a directory of hashed certificate files. More information can be found in ssl.SSLContext.load_verify_locations().

    The cadefault parameter is ignored.

    This function always returns an object which can work as a context manager and has methods such as

    • geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
    • info() — return the meta-information of the page, such as headers, in the form of an email.message_from_string() instance (see Quick Reference to HTTP Headers)
    • getcode() – return the HTTP status code of the response.

    For HTTP and HTTPS URLs, this function returns a http.client.HTTPResponse object slightly modified. In addition to the three new methods above, the msg attribute contains the same information as the reason attribute — the reason phrase returned by server — instead of the response headers as it is specified in the documentation for HTTPResponse.

    For FTP, file, and data URLs and requests explicitly handled by legacy URLopener and FancyURLopener classes, this function returns a urllib.response.addinfourl object.

    Raises URLError on protocol errors.

    Note that None may be returned if no handler handles the request (though the default installed global OpenerDirector uses UnknownHandler to ensure this never happens).

    In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy.

    The legacy urllib.urlopen function from Python 2.6 and earlier has been discontinued; urllib.request.urlopen() corresponds to the old urllib2.urlopen. Proxy handling, which was done by passing a dictionary parameter to urllib.urlopen, can be obtained by usingProxyHandler objects.

    Changed in version 3.2: cafile and capath were added.

    Changed in version 3.2: HTTPS virtual hosts are now supported if possible (that is, if ssl.HAS_SNIis true).

    New in version 3.2: data can be an iterable object.

    Changed in version 3.3: cadefault was added.

    Changed in version 3.4.3: context was added.

    Deprecated since version 3.6: cafilecapath and cadefault are deprecated in favor of context. Please use ssl.SSLContext.load_cert_chain() instead, or let ssl.create_default_context()select the system’s trusted CA certificates for you

    -----

    Python标准库中提供了:urllib、urllib2、httplib等模块以供Http请求,但是,它的 API 太渣了。它是为另一个时代、另一个互联网所创建的。它需要巨量的工作,甚至包括各种方法覆盖,来完成最简单的任务。

  • 相关阅读:
    Hibernate+mysql 中文问题解决方案.
    FpSpread表格控件,FpSpread事件介绍(一)
    如何实现打开有宏的EXCEL时不提示
    使用VB.Net写一个简单的数据访问层(不能称ORM):CRUD操作
    Asp.NET 时间Since转换
    64位操作系统上。NET操作MSMQ的问题
    IIS7配置管理Windows2008 64位系统IIS7的问题
    数据库开发批量附加数据库
    IIS7中对静态文件的处理
    techsailor三步曲
  • 原文地址:https://www.cnblogs.com/yizhixuepython/p/9266531.html
Copyright © 2011-2022 走看看