how python http request and response works

前端 未结 3 1541
逝去的感伤
逝去的感伤 2021-02-09 13:56

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

3条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-09 14:40

    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'
    

提交回复
热议问题