我需要將用 Python 創建的帶有 POST 請求的字典發送到用 Laravel 制作的網頁。我在互聯網上看到我需要匯入一個請求庫,但我不明白它是如何作業的。
我的python腳本是這樣的:
mac_dict = {}
def readFile():
with open("/home/pi/Desktop/Progetti SIoTD/device.txt", "r") as file:
for i in file:
line, *lines = i.split()
if line in mac_dict:
mac_dict[line] = lines
else:
mac_dict[line] = lines
print(mac_dict)
print("\n")
return mac_dict
'''
def get_all_values(nested_dictionary):
for key, value in nested_dictionary.items():
if type(value) is dict:
get_all_values(value)
else:
print(key, ":", value)
print("\n")
'''
def getValues(dict, mac):
s = 0
rssi_val = []
for key in dict:
if key == mac:
k = dict.get(mac)
for i in range(len(k)):
if i % 2 == 0:
rssi = k[i]
rssi = int(rssi)
print(rssi)
rssi_val.append(rssi)
else:
k_v = k[i]
print(k_v)
for i in range(len(rssi_val)):
s = rssi_val[i]
average = s / len(rssi_val)
return average
readFile()
#get_all_values(mac_dict)
getValues(mac_dict, 'C4:A5:DF:24:05:7E')
我怎樣才能做到?提前致謝
更新
我需要發送我從檔案中讀取的字典(函式 readFile)...這個檔案是一個 TXT 檔案,包含 MAC 地址、RSSI 和一個帶時間的字串;該檔案也在 Raspberry Pi4 型號 B 中,我用于掃描藍牙信號并創建 TXT 檔案
uj5u.com熱心網友回復:
如果您的網站是用 2 種不同語言撰寫的,那么在它們之間建立關系的最可靠方法是 API 方法。你可以通過在 Laravel 撰寫的系統中輸入 api 來接受 post 請求。然后,您可以輕松地將這個 post 請求中的文本或檔案通過 python 發送到 laravel api url。
uj5u.com熱心網友回復:
發送 API 并在 Laravel 中使用
`public function __construct(){
$data = "//API Here//";
$this->data = $data;
}`
`public function store(){
// rest of the code here you can find any where
}
`
uj5u.com熱心網友回復:
你沒有解釋你想發送什么資料 - 所以我只能展示基本的語法。
import requests
url = 'https://...'
my_dict = {...}
response = requests.post(url, data=my_dict)
print(response.text)
但一切都可能取決于您沒有顯示的細節。
使用門戶httpbin.org進行測驗的最少作業代碼。它從 POST 發回所有標頭
import requests
url = 'https://httpbin.org/post'
my_dict = {'message': 'hello world'}
response = requests.post(url, data=my_dict)
print(response.text)
結果:
{
"args": {},
"data": "",
"files": {},
"form": {
"message": "hello world"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate, br",
"Content-Length": "19",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.26.0",
"X-Amzn-Trace-Id": "Root=1-6176a644-76c6320f5f4ae9b05a118b2f"
},
"json": null,
"origin": "79.163.228.53",
"url": "https://httpbin.org/post"
}
它顯示"form": {"message": "hello world"}了我發送到服務器的內容。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/336387.html
下一篇:陣列檢查物件鍵是否具有相同的值
