W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
您可以使用WSGI協(xié)議將?httpx-client
?配置為直接調(diào)用Python web應(yīng)用程序。
這對(duì)于兩個(gè)主要用例特別有用:
httpx
?作客戶端。下面是針對(duì) Flask 應(yīng)用程序進(jìn)行集成的示例:
from flask import Flask
import httpx
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
with httpx.Client(app=app, base_url="http://testserver") as client:
r = client.get("/")
assert r.status_code == 200
assert r.text == "Hello World!"
對(duì)于一些更復(fù)雜的情況,您可能需要自定義 WSGI 傳輸。這使您可以:
raise_app_exceptions=False
?來引發(fā)異常。script_name
?(WSGI) 在子路徑上掛載 WSGI 應(yīng)用程序。remote_addr
?(WSGI) 為請(qǐng)求使用給定的Client地址。例如:
# Instantiate a client that makes WSGI requests with a client IP of "1.2.3.4".
transport = httpx.WSGITransport(app=app, remote_addr="1.2.3.4")
with httpx.Client(transport=transport, base_url="http://testserver") as client:
...
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: