简体   繁体   中英

Why is my request in Python not registered by the server?

I am currently writing a script in Python which allows to listen the serial port from a connected device in order to guess the height of a person when passing through a door. The final idea is to send this information to Piwik, a web analytics package through a http request. The code is as follow:

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

when I just ask to print the following line:

"/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)+""

it works fine. But if I look in the logs of the server hosting my website the request is not sent. If I remove the following part "&e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+" then it works fine and the request is sent. If I leave the following part &e_c=entree-magasin&e_a=passage&e_n=taille&e_v="+str(data)+" and hard code the following value +str(data)+ by a figure, then the request is sent too.

I don't really where the problem can be. If anyone can help that would be great.

After reading your answers and work on and on on it, I find a way of optimizing my code by using the requests function instead, but the result is still the same i cannot get str(data) value within my request:

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

试试下面的代码,参数不是头文件的一部分

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

I think I figured it out. I tried to do the same thing but with Google Analytics instead of Piwik. It works with Google Analytics, the str(data) is going back properly within the system, but for some reason it is not working with Piwik :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM