【问题标题】:Send POST request from python to laravel从 python 发送 POST 请求到 laravel
【发布时间】:2021-10-25 07:20:30
【问题描述】:

我需要将带有 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 和一个带时间的字符串;该文件也位于我用于扫描蓝牙信号并创建 TXT 文件的 Raspberry Pi4 模型 B 中

【问题讨论】:

  • requests.post(url, data={..dictionary...})
  • 你有误导性的变量名称 - 它应该是for line in file:
  • 不清楚要发送什么数据。您展示了如何从文件中读取一些数据,但更好地展示了您想要发布到服务器的示例数据。

标签: python laravel post request


【解决方案1】:

如果您有使用 2 种不同语言编写的网站,那么在它们之间建立关系的最可靠的方法是 API 方法。您可以通过在用 Laravel 编写的系统中输入 api 来接受 post 请求。然后你可以很容易地通过python在这个post请求中将文本或文件发送到laravel api url。

【讨论】:

    【解决方案2】:

    发送 API 并在 Laravel 中使用

    `public function __construct(){
     $data = "//API Here//";
     $this->data = $data;
    }`
    
    `public function store(){
          // rest of the code here you can find any where
     }
      `
    

    【讨论】:

      【解决方案3】:

      你没有解释你想发送什么数据——所以我只能展示基本的语法。

      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"}

      【讨论】:

        猜你喜欢
        • 2020-12-05
        • 2017-08-22
        • 2020-09-04
        • 2020-07-14
        • 2015-07-25
        • 2020-05-11
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        相关资源
        最近更新 更多