How python http request and response works

I am new to python, I have a task, so I need to scan wifi and send data to the server, below is the format I need to send, this work is fine when manually entered in the browser URL field,

http://223.56.124.58:8080/ppod-web/ProcessRawData?data={"userId":"2220081127-14","timestamp":"2010-04-12 10:54:24","wifi":{"ssid":"guest","rssi":"80"}} 

here is my code:

 import httplib import urllib params = urllib.urlencode('{\"userId\":\"20081127-14\",\"timestamp\":\"2010-04-12 10:54:24\",\"wifi\":{\"ssid\":\"guest\",\"rssi\":\"80\"}}') headers = {"Content-type":"application/x-www-form-urlencoded","Accept":"text/plain"} conn = httplib.HTTPConnection("http://223.56.124.58:8080") conn.request("POST","ppod-web/ProcessRawData?data=",params,headers) response = conn.getresponse() print response.status print "-----" print response.reason data = response.read() print data conn.close() 

thanks

+6
python
source share
3 answers

Most likely, the problem with the script that you sent in the question , you cannot do directly:

 conn=httplib.HTTPConnection("http://223.56.124.58:8080/wireless") 

The exception is getaddrinfo() in getaddrinfo() , which calls the getaddrinfo() C function, which returns EAI_NONAME :

node or service is unknown; or both node, and the service is NULL; or AI_NUMERICSERV was specified in hints.ai_flags, and the service was not a numeric string of the port number. "

Obviously, there is a problem with the parameters passed to getaddrinfo , and most likely you are trying to get information for the host "223.56.124.58:8080/wireless" . Email Oh!

Indeed, you cannot directly connect to the URL. Since the documentation is clearly indicated and displayed , you are connecting to the server:

 conn = httplib.HTTPConnection("223.56.124.58", 8080) 

Then you can do:

 conn.request("POST", "wireless", params, headers) 

What about the script you're actually using ?

 conn.request("POST","http://202.45.139.58:8080/ppod-web",params,headers) 

Even if the connection was correctly formed, you will have POSTing to http://202.45.139.58:8080/http://202.45.139.58:8080/ppod-web . You probably really want:

 conn = httplib.HTTPConnection("202.45.139.58", 8080) conn.request("POST", "ppod-web", params, headers) 

An error is shown for this line, since most likely the HTTPConnection is a lazy object and only tries to actually connect to the server when calling request() .


After you finish the installation above, you need to fix params .

 >>> urllib.urlencode({"wifi":{"ssid":"guest","rssi","80"}}) SyntaxError: invalid syntax >>> urllib.urlencode({"wifi":{"ssid":"guest","rssi":"80"}}) 'wifi=%7B%27rssi%27%3A+%2780%27%2C+%27ssid%27%3A+%27guest%27%7D' 

To get what you think you want to receive, you must do:

 >>> urllib.urlencode({"data": {"wifi":{"ssid":"guest","rssi":"80"}}}) 'data=%7B%27wifi%27%3A+%7B%27rssi%27%3A+%2780%27%2C+%27ssid%27%3A+%27guest%27%7D%7D' 
+10
source

Instead:

 conn = httplib.HTTPConnection("http://223.56.124.58:8080/wireless") conn.request("POST", "data", params, headers) 

to try:

 conn = httplib.HTTPConnection("223.56.124.58", port=8080) conn.request("POST", "/wireless", params, headers) 

Not sure if it will solve all your problems, but at least your code will match the method / constructor signatures.

+1
source

Tracking does not come from the same code that you inserted.

There is a line in the error trace:

 conn.request("POST","http://202.45.139.58:8080/ppod-web",params,headers) 

This is line 9 from http.py , however it is not on the code you inserted.

Paste the actual code.

0
source

All Articles