Python - Correctly format HTTP post with delimiter

2019-09-07 00:22发布

This question is an exact duplicate of:

I have a python script accepting the output of a bash command 'ibeacon_scan'. I am getting this error when running the script.

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

Here is the code for 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()

The output of the 'ibeacon_san' command is:

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

My question is, how do change my script send a query string such as:

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

I don't know how to get the fileinput formatted into a query string. Does my formatting look somewhat correct? The delimiter is a space for the data coming into the python script. I'm using Raspbian on a Raspberry Pi.

1条回答
欢心
2楼-- · 2019-09-07 00:56

You have a typo on the line where you define params - you call it parmas.

Generally, if you read the error message it's quite helpful. This clearly tells you that you don't have a variable defined called params - so go check that part of your code!

查看更多
登录 后发表回答