Postman的Python脚本不起作用

时间:2017-04-13 12:40:32

标签: python postman

我买了一个小的wifi中继模块 - 虽然它是中文的,我没看过,我已经研究了如何从嵌入式Web服务器主页上的按钮打开和关闭中继。

然后我使用邮差拦截器来捕捉'开放'和'关闭'动作,现在我可以点击'发布'按钮让动作发生。

然而'生成代码'python脚本不起作用,并且从我有限的理解中没有正确的信息。

import requests

url = "http://192.168.4.1/"

payload = ""
headers = {
    'origin': "http://192.168.4.1",
    'upgrade-insecure-requests': "1",
    'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
    'content-type': "application/x-www-form-urlencoded",
    'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
    'dnt': "1",
    'referer': "http://192.168.4.1/",
    'accept-encoding': "gzip, deflate",
    'accept-language': "en-US,en;q=0.8",
    'cache-control': "no-cache",
    'postman-token': "bece04e7-ee50-3764-ca50-e86d07ebc0f3"
    }

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)

当我选择HTTP而不是Python请求时的输出是

POST / HTTP/1.1
Host: 192.168.4.1
Origin: http://192.168.4.1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
DNT: 1
Referer: http://192.168.4.1/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8
Cache-Control: no-cache
Postman-Token: 0bd42b4f-067d-b5be-dd1c-b7e689196043

open_relay=%EF%BF%BD%F2%BF%AA%BC%CC%B5%EF%BF%BD%EF%BF%BD%EF%BF%BD

有人可以建议如何修改Python以正确发送与Postman本身一起正常工作的POST吗?

1 个答案:

答案 0 :(得分:1)

您的python代码缺少POST数据,其中包含设备命令,该命令列在http请求的底部。

open_relay=%EF%BF%BD%F2%BF%AA%BC%CC%B5%EF%BF%BD%EF%BF%BD%EF%BF%BD放入python代码中的payload变量:

import requests

url = "http://192.168.4.1/"

payload = "open_relay=%EF%BF%BD%F2%BF%AA%BC%CC%B5%EF%BF%BD%EF%BF%BD%EF%BF%BD"
headers = {
  'origin': "http://192.168.4.1",
  'upgrade-insecure-requests': "1",
  'user-agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36",
  'content-type': "application/x-www-form-urlencoded",
  'accept': "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
  'dnt': "1",
  'referer': "http://192.168.4.1/",
  'accept-encoding': "gzip, deflate",
  'accept-language': "en-US,en;q=0.8",
  'cache-control': "no-cache",
  'postman-token': "bece04e7-ee50-3764-ca50-e86d07ebc0f3"
}

response = requests.request("POST", url, data=payload, headers=headers)

print(response.text)