5.7.23. vlcp.utils.webclient

Note

This document is generated from the source file.

View Source on GitHub

Created on 2015/11/24

author:hubo
exception vlcp.utils.webclient.CertificateException
exception vlcp.utils.webclient.ManualRedirectRequired(msg, response, request, kwargs)
__init__(msg, response, request, kwargs)

Initialize self. See help(type(self)) for accurate signature.

class vlcp.utils.webclient.WebClient(allowcookies=False, cookiejar=None)

Convenient HTTP request processing. Proxy is not supported in current version.

__init__(allowcookies=False, cookiejar=None)
Parameters:
  • allowcookies – Accept and store cookies, automatically use them on further requests
  • cookiejar – Provide a customized cookiejar instead of the default CookieJar()
cleanup(host=None)

Cleaning disconnected connections

cleanup_task(container, interval=None)

If this client object is persist for a long time, and you are worrying about memory leak, create a routine with this method: myclient.cleanup_task(mycontainer, 60). But remember that if you have created at lease one task, you must call myclient.endtask() to completely release the webclient object.

manualredirect(container, exc, data, datagen=None)

If data is a stream, it cannot be used again on redirect. Catch the ManualRedirectException and call a manual redirect with a new stream.

open(container, request, ignorewebexception=False, timeout=None, datagen=None, cafile=None, key=None, certificate=None, followredirect=True, autodecompress=False, allowcookies=None)

Open http request with a Request object

Parameters:
  • container – a routine container hosting this routine
  • request – vlcp.utils.webclient.Request object
  • ignorewebexception – Do not raise exception on Web errors (4xx, 5xx), return a response normally
  • timeout – timeout on connection and single http request. When following redirect, new request does not share the old timeout, which means if timeout=2: connect to host: (2s) wait for response: (2s) response is 302, redirect connect to redirected host: (2s) wait for response: (2s) …
  • datagen – if the request use a stream as the data parameter, you may provide a routine to generate data for the stream. If the request failed early, this routine is automatically terminated.
  • cafile – provide a CA file for SSL certification check. If not provided, the SSL connection is NOT verified.
  • key – provide a key file, for client certification (usually not necessary)
  • certificate – provide a certificate file, for client certification (usually not necessary)
  • followredirect – if True (default), automatically follow 3xx redirections
  • autodecompress – if True, automatically detect Content-Encoding header and decode the body
  • allowcookies – override default settings to disable the cookies
shutdown()

Shutdown free connections to release resources

urlgetcontent(container, url, data=None, method=None, headers={}, tostr=False, encoding=None, rawurl=False, *args, **kwargs)

In Python2, bytes = str, so tostr and encoding has no effect. In Python3, bytes are decoded into unicode str with encoding. If encoding is not specified, charset in content-type is used if present, or default to utf-8 if not. See open for available arguments

Parameters:rawurl – if True, assume the url is already url-encoded, do not encode it again.
urlopen(container, url, data=None, method=None, headers={}, rawurl=False, *args, **kwargs)

Similar to urllib2.urlopen, but: 1. is a routine 2. data can be an instance of vlcp.event.stream.BaseStream, or str/bytes 3. can specify method 4. if datagen is not None, it is a routine which writes to <data>. It is automatically terminated if the connection is down. 5. can also specify key and certificate, for client certification 6. certificates are verified with CA if provided. If there are keep-alived connections, they are automatically reused. See open for available arguments

Extra argument:

Parameters:rawurl – if True, assume the url is already url-encoded, do not encode it again.
class vlcp.utils.webclient.WebClientRequestDoneEvent(*args, **kwargs)
exception vlcp.utils.webclient.WebException
vlcp.utils.webclient.match_hostname(cert, hostname)

Verify that cert (in decoded format as returned by SSLSocket.getpeercert()) matches the hostname. RFC 2818 and RFC 6125 rules are followed, but IP addresses are not accepted for hostname.

CertificateError is raised on failure. On success, the function returns nothing.