Python - 使用分隔符正确格式化HTTP post

时间:2014-08-06 22:15:26

标签: python bash http ibeacon file-import

我有一个python脚本接受bash命令的输出' ibeacon_scan'。运行脚本时出现此错误。

Traceback (most recent call last):
   file "./stdin.py", line 12, in <module>
      conn.request("POST", "post.php", params, headers)
NameError: name 'params is not defined

这是python的代码

#!/usr/bin/python

import fileinput
import httplib
import urllib

for line in fileinput.input():

   parmas = urllib.urlencode({"UUID": {"Major":{"Minor":"RSSI"}}})
   headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
   conn = httplib.HTTPConnection("67.205.14.22")
   conn.request("POST", "post.php", params, headers)
   response = conn.getresponse()
   print response.status, response.reason
   data = response.read()
   print data
   conn.close()

&#39; ibeacon_san&#39;的输出命令是:

ibeacon scan...
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 1 4 -71 -69
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 6 2 -71 -63
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 1 4 -71 -69
3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6 5 7 -71 -64

我的问题是,如何更改我的脚本发送查询字符串,如:

http://67.205.14.22/post.php/ProcessRawData?data={"UUID":"3F234454-CFD-4A0FF-ADF2-F4911BA9FFA6","timestamp":"2010-04-12 10:54:24","Major": "1","Minor": "4","RSSI":"-69"}

我不知道如何将fileinput格式化为查询字符串。我的格式看起来有点正确吗?分隔符是进入python脚本的数据的空间。我在Raspberry Pi上使用Raspbian。

1 个答案:

答案 0 :(得分:0)

您在定义params的行上有拼写错误 - 您将其称为parmas

通常,如果您阅读错误消息,那将非常有帮助。这清楚地告诉您,您没有定义名为params的变量 - 所以去检查一下您的代码部分!