注意
僅當(dāng)您在控制臺(tái)中測(cè)試 HTTPX 或發(fā)出少量請(qǐng)求時(shí),才使用這些函數(shù)。使用 ?Client
?將啟用 HTTP/2 和連接池,以實(shí)現(xiàn)更高效、更持久的連接。
httpx.request(method, url, *, params=None, content=None, data=None, files=None, json=None, headers=None, cookies=None, auth=None, proxies=None, timeout=Timeout(timeout=5.0), follow_redirects=False, verify=True, cert=None, trust_env=True)
發(fā)送 HTTP 請(qǐng)求。
參數(shù):
Request
?對(duì)象的 HTTP 方法:?GET
?、?OPTIONS
?、?HEAD
?、?POST
?、?PUT
?、?PATCH
?或?DELETE
?。Request
?對(duì)象的 URL。True
?(默認(rèn) CA 捆綁包)、SSL 證書(shū)文件的路徑、?ssl.SSLContext
?或 ?False
?(將禁用驗(yàn)證)。返回: Response
用法:
>>> import httpx
>>> response = httpx.request('GET', 'https://httpbin.org/get')
>>> response
<Response [200 OK]>
httpx.get(url, *, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?GET
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
請(qǐng)注意 data
, files
, json
和 content
參數(shù)在此函數(shù)上不可用,因?yàn)?GET
?請(qǐng)求不應(yīng)包含請(qǐng)求正文。
httpx.options(url, *, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?OPTIONS
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
請(qǐng)注意 data
, files
, json
和 content
參數(shù)在此函數(shù)上不可用,因?yàn)?OPTIONS
?請(qǐng)求不應(yīng)包含請(qǐng)求正文。
httpx.head(url, *, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?HEAD
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
請(qǐng)注意 data
, files
, json
和 content
參數(shù)在此函數(shù)上不可用,因?yàn)?HEAD
?請(qǐng)求不應(yīng)包含請(qǐng)求正文。
httpx.post(url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?POST
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
httpx.put(url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?PUT
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
httpx.patch(url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?PATCH
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
httpx.delete(url, *, params=None, headers=None, cookies=None, auth=None, proxies=None, follow_redirects=False, cert=None, verify=True, timeout=Timeout(timeout=5.0), trust_env=True)
發(fā)送?DELETE
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
請(qǐng)注意 data
, files
, json
和 content
參數(shù)在此函數(shù)上不可用,因?yàn)?DELETE
?請(qǐng)求不應(yīng)包含請(qǐng)求正文。
httpx.stream(method, url, *, params=None, content=None, data=None, files=None, json=None, headers=None, cookies=None, auth=None, proxies=None, timeout=Timeout(timeout=5.0), follow_redirects=False, verify=True, cert=None, trust_env=True)
?httpx.request()
?的替代方案是流式傳輸響應(yīng)體,而不是立即將其加載到內(nèi)存中。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
另請(qǐng)參見(jiàn):流式處理響應(yīng)
class httpx.Client(*, auth=None, params=None, headers=None, cookies=None, verify=True, cert=None, http1=True, http2=False, proxies=None, mounts=None, timeout=Timeout(timeout=5.0), follow_redirects=False, limits=Limits(max_connections=100, max_keepalive_connections=20, keepalive_expiry=5.0), max_redirects=20, event_hooks=None, base_url='', transport=None, app=None, trust_env=True, default_encoding='utf-8')
HTTP 客戶端,具有連接池、HTTP/2、重定向、Cookie 持久性等功能。
用法:
>>> client = httpx.Client()
>>> response = client.get('https://example.org')
參數(shù):
True
?(默認(rèn) CA 捆綁包)、SSL 證書(shū)文件的路徑、?ssl.SSLContext
?或 ?False
?(將禁用驗(yàn)證)。utf-8
?”。發(fā)送請(qǐng)求時(shí)要包含的 HTTP 標(biāo)頭。
發(fā)送請(qǐng)求時(shí)要包含的 Cookie 值。
發(fā)送請(qǐng)求時(shí)要包含在 URL 中的查詢參數(shù)。
在請(qǐng)求級(jí)別傳遞任何身份驗(yàn)證類時(shí)使用。
另請(qǐng)參閱身份驗(yàn)證。
request(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
生成并發(fā)送請(qǐng)求。
等效于:
request = client.build_request(...)
response = client.send(request, ...)
請(qǐng)參閱?Client.build_request()
?、?Client.send()
?和 配置的合并,了解如何將各種參數(shù)與client-level
?配置合并。
get(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?GET
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
head(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?HEAD
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
options(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?OPTIONS
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
post(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?POST
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
put(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?PUT
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
patch(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?PATCH
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
delete(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?DELETE
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
stream(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
??httpx.request()?
?的替代方案是流式傳輸響應(yīng)體,而不是立即將其加載到內(nèi)存中。
參數(shù):請(qǐng)參見(jiàn)??httpx.request
??。
另請(qǐng)參見(jiàn):流式處理響應(yīng)
build_request(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, timeout=, extensions=None)
生成并返回請(qǐng)求實(shí)例。
params
?、?headers
?和?cookies
?參數(shù)將與?client
?上設(shè)置的任何值合并。urlbase_url
?參數(shù)將與?client
?上的任何集合并。另請(qǐng)參閱:請(qǐng)求實(shí)例
send(self, request, *, stream=False, auth=, follow_redirects=)
發(fā)送請(qǐng)求。
請(qǐng)求按原樣發(fā)送,未經(jīng)修改。
通常,您需要構(gòu)建一個(gè)?Client.build_request()
?,以便將任何客戶端級(jí)配置合并到請(qǐng)求中,但?httpx.Request()也
?支持傳遞顯式配置。
另請(qǐng)參閱:請(qǐng)求實(shí)例
close(self)
關(guān)閉?transport
?和代理。
class httpx.AsyncClient(*, auth=None, params=None, headers=None, cookies=None, verify=True, cert=None, http1=True, http2=False, proxies=None, mounts=None, timeout=Timeout(timeout=5.0), follow_redirects=False, limits=Limits(max_connections=100, max_keepalive_connections=20, keepalive_expiry=5.0), max_redirects=20, event_hooks=None, base_url='', transport=None, app=None, trust_env=True, default_encoding='utf-8')
異步 HTTP 客戶端,具有連接池、HTTP/2、重定向、Cookie 持久性等功能。
用法:
>>> async with httpx.AsyncClient() as client: >>> response = await client.get('https://example.org')
參數(shù):
True
?(默認(rèn) CA 捆綁包)、SSL 證書(shū)文件的路徑或?False
?(禁用驗(yàn)證)。utf-8
?”。發(fā)送請(qǐng)求時(shí)要包含的 HTTP 標(biāo)頭。
發(fā)送請(qǐng)求時(shí)要包含的 Cookie 值。
發(fā)送請(qǐng)求時(shí)要包含在 URL 中的查詢參數(shù)。
在請(qǐng)求級(jí)別傳遞任何身份驗(yàn)證類時(shí)使用。
另請(qǐng)參閱身份驗(yàn)證。
request(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
生成并發(fā)送請(qǐng)求。
等效于:
request = client.build_request(...) response = await client.send(request, ...)
請(qǐng)參閱?AsyncClient.build_request()
?和?AsyncClient.send()
? 配置的合并,了解如何將各種參數(shù)與客戶端級(jí)配置合并。
get(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?GET
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
head(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?HEAD
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
options(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?OPTIONS
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
post(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?POST
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
put(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?PUT
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
patch(self, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?PATCH
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
delete(self, url, *, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
發(fā)送?DELETE
?請(qǐng)求。
參數(shù):請(qǐng)參見(jiàn)?httpx.request
?。
stream(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, auth=, follow_redirects=, timeout=, extensions=None)
???httpx.request()??
?的替代方案是流式傳輸響應(yīng)體,而不是立即將其加載到內(nèi)存中。
參數(shù):請(qǐng)參見(jiàn)???httpx.request??
?。
另請(qǐng)參見(jiàn):流式處理響應(yīng)
build_request(self, method, url, *, content=None, data=None, files=None, json=None, params=None, headers=None, cookies=None, timeout=, extensions=None)
生成并返回請(qǐng)求實(shí)例。
params
??、??headers
??和??cookies
??參數(shù)將與??client
??上設(shè)置的任何值合并。urlbase_url
??參數(shù)將與?client?上的任何集合并。另請(qǐng)參閱:請(qǐng)求實(shí)例
send(self, request, *, stream=False, auth=, follow_redirects=)
發(fā)送請(qǐng)求。
請(qǐng)求按原樣發(fā)送,未經(jīng)修改。
通常,您需要構(gòu)建一個(gè)??Client.build_request()?
?,以便將任何客戶端級(jí)配置合并到請(qǐng)求中,但??httpx.Request()
?也?支持傳遞顯式配置。
另請(qǐng)參閱:請(qǐng)求實(shí)例
aclose(self)
關(guān)閉?transport
?和代理。
HTTP 響應(yīng)。
def __init__(...)
?.status_code
? - int.reason_phrase
? - str.http_version
?- "?HTTP/2
?"或"?HTTP/1.1
?".url
? - URL.headers
? - headers.content
? - bytes.text
? - str.encoding
?- str.is_redirect
? - bool.request
? - Request.next_request
? - 可選[Request].cookies
? - cookies.history
? - 列表[Response].elapsed
? - timedeltadef .raise_for_status()
? - Nonedef .json()
? - Anydef .read()
? - bytesdef .iter_raw()
?- 字節(jié)迭代器def .iter_bytes()
? - 字節(jié)迭代器def .iter_text()
? - 文本迭代器def .iter_lines()
? - 文本迭代器def .close()
? - Nonedef .next()
? - Responsedef .aread()
? - bytesdef .aiter_raw()
? - 異步字節(jié)迭代器def .aiter_bytes()
? - 異步字節(jié)迭代器def .aiter_text()
? - 異步文本迭代器def .aiter_lines()
?- 異步文本迭代器def .aclose()
? - Nonedef .anext()
?- 響應(yīng)一個(gè) HTTP 請(qǐng)求。可以顯式構(gòu)造,以便更好地控制通過(guò)網(wǎng)絡(luò)發(fā)送的確切內(nèi)容。
>>> request = httpx.Request("GET", "https://example.org", headers={'host': 'example.org'})
>>> response = client.send(request)
def __init__(method, url, [params], [headers], [cookies], [content], [data], [files], [json], [stream])
?.method
?- str.url
? - 網(wǎng)址.content
?- 字節(jié)、字節(jié)迭代器或字節(jié)異步迭代器.headers
? - headers.cookies
? - cookie一個(gè)規(guī)范化的、支持IDNA的URL。
>>> url = URL("https://example.org/") >>> url.host 'example.org'
def __init__(url, allow_relative=False, params=None)
?.scheme
?- str.authority
? - str.host
? - str.port
? - int.path
? - str.query
? - str.raw_path
?- str.fragment
? - str.is_ssl
? - bool.is_absolute_url
?- bool.is_relative_url
? - booldef .copy_with([scheme], [authority], [path], [query], [fragment])
? - URL不區(qū)分大小寫(xiě)的多維字典。
>>> headers = Headers({'Content-Type': 'application/json'})
>>> headers['content-type']
'application/json'
def __init__(self, headers, encoding=None)
?def copy()
? - headers一個(gè)類似字典的cookies 存儲(chǔ)庫(kù)。
>>> cookies = Cookies()
>>> cookies.set("name", "value", domain="example.org")
def __init__(cookies: [dict, Cookies, CookieJar])
?.jar
?- CookieJardef extract_cookies(response)
?def set_cookie_header(request)
?def set(name, value, [domain], [path])
?def get(name, [domain], [path])
?def delete(name, [domain], [path])
?def clear([domain], [path])
?
更多建議: