request

build_opener()

urllib.request.build_opener([handler, ...])

Возвращает объект для запросов urllib.request.OpenerDirector()

proxy_handler = urllib.request.ProxyHandler({'http': proxy_http_url})

auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(
    realm='Application',
    uri=uri,
    user=user,
    passwd=passwd
)
opener = urllib.request.build_opener(auth_handler, proxy_handler)
urllib.request.install_opener(opener)
urllib.request.urlopen(login_url)

getproxies()

urllib.request.getproxies()

install_opener()

urllib.request.install_opener(opener)

Устанавливает глобальный объект для запросов.

Объектов может быть любой класс, реализующий методы urllib.request.OpenerDirector()

opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)

pathname2url()

urllib.request.pathname2url(path)

urlopen()

urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=None, context=None)

Выполняет запрос и возвращает результат ответа с сервера в виде объекта http.client.HTTPResponse

conn = urlopen(url)

conn = urlopen(Request(url))

with urlopen(url) as f:
    print(f.read())
# b'html content'

req = Request(url=url, data=b'This data is passed to stdin of the CGI')
with urllib.request.urlopen(req) as f:
    print(f.read().decode('utf-8'))
Got Data: "This data is passed to stdin of the CGI"

url2pathname()

urllib.request.url2pathname(path)

AbstractBasicAuthHandler()

class urllib.request.AbstractBasicAuthHandler
http_error_auth_reqed(authreq, host, req, headers)

AbstractDigestAuthHandler()

class urllib.request.AbstractDigestAuthHandler
http_error_auth_reqed(authreq, host, req, headers)

BaseHandler()

class urllib.request.BaseHandler
add_parent
add_parent(director)
close()
default_open(req)
http_error_default(req, fp, code, msg, hdrs)
http_error_<nnn>>(req, fp, code, msg, hdrs)
unknown_open(req)
<protocol>_open(req)
<protocol>_request(req)
<protocol>_response(req, response)

CacheFTPHandler()

class urllib.request.CacheFTPHandler
setMaxConns(m)
setTimeout(t)

DataHandler()

class urllib.request.DataHandler
data_open(req)

FileHandler()

class urllib.request.FileHandler
file_open(req)

FTPHandler()

class urllib.request.FTPHandler
ftp_open(req)

HTTPBasicAuthHandler()

class urllib.request.HTTPBasicAuthHandler(password_mgr=None)
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(
    realm='Application',
    uri=uri,
    user=user,
    passwd=passwd
)
opener = urllib.request.build_opener(auth_handler)
urllib.request.install_opener(opener)
urllib.request.urlopen(login_url)
http_error_401(req, fp, code, msg, hdrs)

HTTPCookieProcessor()

urllib.request.HTTPCookieProcessor(cookijar=None)
urllib.request.cookiejar

http.cookiejar.Cookiejar()

HTTPDefaultErrorHandler()

class urllib.request.HTTPDefaultErrorHandler

HTTPDigestAuthHandler()

class urllib.request.HTTPDigestAuthHandler(password_mgr=None)
http_error_401(req, fp, code, msg, hdrs)

HTTPErrorProcessor()

class urllib.request.HTTPErrorProcessor
http_response(request, response)
https_response(request, response)

HTTPHandler()

class urllib.request.HTTPHandler
http_open(req)

HTTPPasswordMgr()

class urllib.request.HTTPPasswordMgr
add_password(realm, uri, user, passwd)
find_user_password(realm, aurhuri)

HTTPPasswordMgrWithDefaultRealm()

class urllib.request.HTTPPasswordMgrWithDefaultRealm
add_password(realm, uri, user, passwd)
find_user_password(realm, authuri)

HTTPPasswordMgrWithPriorAuth()

class urllib.request.HTTPPasswordMgrWithPriorAuth
add_password(realm, uri, user, passwd, is_authenticated=False)
find_user_password(realm, authuri)
update_authenticated(self, uri, is_authenticated=False)
is_authenticated(self, authuri)

HTTPRedirectHandler()

class urllib.request.HTTPRedirectHandler
redirect_request(req, fp, code, msg, hdrs, newurl)
http_error_301(req, fp, code, msg, hdrs)
http_error_302(req, fp, code, msg, hdrs)
http_error_303(req, fp, code, msg, hdrs)
http_error_307(req, fp, code, msg, hdrs)

HTTPSHandler()

class urllib.request.HTTPSHandler
https_open(req)

OpenerDirector()

class urllib.request.OpenerDirector
add_handler(handler)
error(proto, *args)
open(url, data=None[, timeout])

ProxyBasicAuthHandler()

class urllib.request.ProxyBasicAuthHandler(password_mgr=None)
http_error_407(req, fp, code, msg, hdrs)

ProxyDigestAuthHandler()

class urllib.request.ProxyDigestAuthHandler(password_mgr=None)
http_error_407(req, fp, code, msg, hdrs)

ProxyHandler()

class urllib.request.ProxyHandler
proxy_handler = urllib.request.ProxyHandler({'http': proxy_http_url})
<protocol>_open(req)

Request()

class urllib.request.Request(url, data=None, headers={}, origin_req_host=None, unverifiable=False, method=None)
  • url - str

  • data - bytes

query_args = {'q': 'text'}

req = Request(url)

req = Request(
    url,
    data=urllib.parse.urlencode(query_args).encode('utf-8')
)
data
full_url
host
origin_req_host
selector
type
unverifiable
add_header(key, value)
req.add_header('User-agent', 'ChromeBrowser')
add_unredirected_header()
get_full_url()
get_header(key, default=None)
get_method()
has_header(header)
header_items()
remove_header(header)
set_proxy(host, type)

UnknownHandler()

class urllib.request.UnknownHandler
unknown_open()