Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how python http request and response works

Tags:

python

http

I'm newbie for python, I'm having task so I need to scan wifi and send the data to the server, the below is the format which i have to send, this work fine when enter manually in browser url text box,

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

like image 898
Apache Avatar asked Apr 12 '10 06:04

Apache


People also ask

What is HTTP request and response in Python?

The request module in Python is used to make HTTP requests to web pages. A web server usually sends an HTTP response back when it receives a request. The HTTP response comprises of status code, content, and encodings.

How does Python requests get work?

The generic process is this: a client (like a browser or Python script using Requests) will send some data to a URL, and then the server located at the URL will read the data, decide what to do with it, and return a response to the client. Finally, the client can decide what to do with the data in the response.

How do you get a response from a Python request?

When one makes a request to a URI, it returns a response. This Response object in terms of python is returned by requests. method(), method being – get, post, put, etc.


2 Answers

Most likely, the issue with the script you posted in the question is you cannot directly do:

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

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

The node or service is not known; or both node and service are NULL; or AI_NUMERICSERV was specified in hints.ai_flags and service was not a numeric port-number string."

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

Indeed, you cannot directly connect to an URL address. As the documentation clearly states and shows, you connect 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 are actually using?

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

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

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

The error is shown for this line because most likely HTTPConnection is a lazy object and only attempts to actually connect to the server when you call request().


After you're done fixing the above, you'll 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 get, you should 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'
like image 85
badp Avatar answered Oct 13 '22 00:10

badp


Instead of:

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

try:

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

Not sure if it will resolve all your problems, but at least your code will conform to the method/constructor signatures.

like image 21
ars Avatar answered Oct 12 '22 23:10

ars