为什么我的Python请求没有被服务器注册?

2024-09-28 15:35:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在用Python编写一个脚本,它允许监听连接设备的串行端口,以便猜测一个人通过门时的身高。 最后的想法是通过http请求将这些信息发送给Piwik,一个web分析包。 代码如下:

import serial, httplib, uuid
arduino = serial.Serial('/dev/ttyACM0', 9600)
while True:
    data = arduino.readline()
    print data
    conn = httplib.HTTPConnection("my-domain.com")
    conn.request("HEAD","/piwik/piwik.php?idsite=1&rec=1&action_name=Entree-magasin&uid="+str(uuid.uuid4())+"&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+"")
    print conn.request

当我要求打印以下行时:

"/piwik/piwik.php?idsite=1&rec=1&action_name=Entree-magasin&uid="+str(uuid.uuid4())+"&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+""

很好用。但是如果我查看托管我的网站的服务器的日志,请求就不会被发送。 如果我删除了下面的部分"&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+",那么它可以正常工作,请求也会被发送。 如果我保留下面的部分&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+",并用一个数字硬编码下面的值+str(data)+,那么请求也会被发送。你知道吗

我真的不知道问题出在哪里。如果有人能帮忙那就太好了。你知道吗

在阅读了您的答案并不断地研究之后,我找到了一种通过使用requests函数来优化代码的方法,但是结果仍然是一样的,我无法在请求中获得str(data)值:

import serial, requests, uuid arduino = serial.Serial('/dev/ttyACM0', 9600) while True: data = arduino.readline() print data r = requests.get('http://my-domain.com/piwik/piwik.php?idsite=1&rec=1&action_name=Entree-magasin&uid='+str(uuid.uuid4())+'&e_c=entree-magasin&e_a=passage&e_n=taille&e_v='+str(data)+'') print r


Tags: datauuidserialconnarduinopassagephpprint
2条回答

我想我知道了。我试着做同样的事情,但是用谷歌分析代替了Piwik。它与谷歌分析(googleanalytics)配合使用,str(数据)在系统内正常返回,但由于某些原因,它与Piwik(Piwik)不起作用

试试下面的代码,参数不是header的一部分

resp, content = h.request("http://my-domain.com/piwik/piwik.php?idsite=1&rec=1&action_name=Entree-magasin&uid="+str(uuid.uuid4())+"&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data), "GET")

相关问题 更多 >