[英]Python - urllib.request and flask
我有這個P2P
python代碼,並且我試圖從中將POST
請求發送到flask
:
在我的P2P
方面,我有:
...
for reply in con:
jsonData = json.loads(reply)
print(jsonData)
print(type(jsonData) is dict, tuple)
data = urlencode(jsonData)
print(data + " : urlencode")
data = data.encode()
print(data.__class__)
req = urllib.request.Request("http://0.0.0.0:5000/validate", data)
response = urllib.request.urlopen(req)
#res = response.read()
print(response.read().decode('utf8') + " : response in alice")
對於我的flask
代碼,我有:
@app.route('/validate', methods=['POST'])
def validate():
print(request.args)
request args始終輸出為:
ImmutableMultiDict([])
P2P
端的輸出為:
{'index': 2043, 'message': 'New Block Forged', 'previous_hash': 'fa4a49cd092869db788490e79a933e7a45107ce513523500e5cd9c85e25426de', 'proof': 168158, 'transactions': [{'amount': 1, 'recipient': '6760d061731c493e94897164c2362476', 'sender': '0'}]}
True <class 'tuple'>
index=2043&message=New+Block+Forged&previous_hash=fa4a49cd092869db788490e79a933e7a45107ce513523500e5cd9c85e25426de&proof=168158&transactions=%5B%7B%27amount%27%3A+1%2C+%27recipient%27%3A+%276760d061731c493e94897164c2362476%27%2C+%27sender%27%3A+%270%27%7D%5D : urlencode
<class 'bytes'>
{
"add": true
}
: response in alice
如您所見, urllib.request.urlopen
的data
看起來正確。 為什么不通過燒瓶側?
您正在發出POST
請求以validate
端點。 並且request.args僅返回url查詢字符串。 因此,實際數據將在request.form
可用。
回答
請發出GET
請求...,以便可以在request.args
獲得data
data = urllib.parse.urlencode(json_data)
url = 'http://localhost:5000?{}'.format(data)
with urllib.request.urlopen(url) as response:
pass
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.