Python的 - 从标准输入HTTP发布 [英] Python - HTTP post from stdin

查看:109
本文介绍了Python的 - 从标准输入HTTP发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个格式获取数据每秒左右从bash命令'ibeacon_scan

  iBeacon显示扫描-b | stdin.py

输出:

  iBeacon显示扫描...
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

我需要通过查询字符串来发送信息。这里是我的code。

 #!的/ usr / bin中/蟒蛇进口的FileInput
给出import httplib
进口的urllib在fileinput.input线():
   字符串=行
   字符串2 = string.split()
   parmas =字符串2
   parmas = urllib.urlen code({UUID:大,小:RSSI})
   标题= {内容类型:应用程序/ x-WWW的形式urlen codeD,接受:text / plain的}
   康恩= httplib.HTTPConnection(67.205.14.22)
   conn.request(POST,post.php中,参数,头)
   响应= conn.getresponse()
   打印response.status,response.reason
   数据= response.read()
   打印数据
   conn.close()

我得到这个错误:

 回溯(最后最近一次调用):
  文件./stdin.py,线路14,上述<模块>
    conn.request(POST,post.php中,参数,头)
NameError:名称'参数'没有定义

有些事情不对使用参数?我如何格式化这个正确接受iBeacon显示扫描命令并通过HTTP邮寄呢?


解决方案

 在fileinput.input线():
   字符串=行
   字符串2 = string.split()
   parmas =字符串2#调用这里的变量parmas
   parmas = urllib.urlen code({UUID:大,小:RSSI})
   标题= {内容类型:应用程序/ x-WWW的形式urlen codeD,接受:text / plain的}
   康恩= httplib.HTTPConnection(67.205.14.22)
   conn.request(POST,post.php中,参数,头)#这里PARAMS

您已经取得了什么看起来是一个错字。

I am getting data in this format every second or so from a bash command 'ibeacon_scan"

ibeacon scan -b | stdin.py

Output:

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

I need to send that information by query string. Here is my code.

#!/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()

I'm getting this error:

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

Something is wrong with params? How do I format this to correctly accept the 'ibeacon scan' command and send it by HTTP post?

解决方案

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

You have made what looks to be a typo.

这篇关于Python的 - 从标准输入HTTP发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆