tags:

views:

274

answers:

3

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

A: 

The traceback doesn't come from the same code you pasted.

On the error traceback there's a line:

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

It is the line 9 of http.py however it is not on the code you pasted.

Please paste the actual code.

nosklo
Thanks for noticing that.
badp
sorry nosklo, actually i'm testing with sample codes, mistakenl i paste from different, i've edited the code,can u help me for the http post for different parameter as above format ,thanks
Apache
+4  A: 

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'
badp
hi bro, i'm having 1 prob, when i use hard-coded method data can send to the server, but when i use not hard-coded method i'm gettind error as Traceback (most recent call last): File "scan.py", line 71, in <module> request.add_data(urllib.urlencode(query_args)) File "/usr/lib/python2.6/urllib.py", line 1254, in urlencode raise TypeErrorTypeError: not a valid non-string sequence or mapping objectcan u help me for this, i change the code for reference as abovethanks
Apache
+2  A: 

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.

ars