Python - 从标准输入进行HTTP POST
我每隔一秒钟就会从一个叫做'ibeacon_scan'的bash命令中获取数据。
ibeacon scan -b | stdin.py
输出结果是:
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
我需要通过查询字符串发送这些信息。这是我的代码。
#!/usr/bin/python
import fileinput
import httplib
import urllib
for line in fileinput.input():
string = line
string2 = string.split(" ")
parmas = string2
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()
但是我遇到了这个错误:
Traceback (most recent call last):
File "./stdin.py", line 14, in <module>
conn.request("POST", "post.php", params, headers)
NameError: name 'params' is not defined
参数好像有问题?我该怎么格式化才能正确接受'ibeacon scan'命令,并通过HTTP POST发送呢?
1 个回答
0
for line in fileinput.input():
string = line
string2 = string.split(" ")
parmas = string2 # you call the variable parmas here
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) # and params here
你似乎犯了一个打字错误。