How to fix httplib.BadStatusLine exception?

前端 未结 1 2002
野性不改
野性不改 2020-12-06 13:45
URL = \"MY HTTP REQUEST URL\"
XML = \"0\"

parameter = urllib.urlencode({\'XML\': XML})
response = urllib.urlopen(URL, parameter)
print resp         


        
相关标签:
1条回答
  • 2020-12-06 14:23

    Try to have a look what your server actually returns! It probably isn't a valid HTTP response. You could use something like this to send a raw http request to the server:

    from socket import socket
    
    host = 'localhost'
    port = 80
    path = "/your/url"
    xmlmessage = "<port>0</port>"
    
    s = socket()
    s.connect((host, port))
    s.send("POST %s HTTP/1.1\r\n" % path)
    s.send("Host: %s\r\n" % host)
    s.send("Content-Type: text/xml\r\n")
    s.send("Content-Length: %d\r\n\r\n" % len(xmlmessage))
    s.send(xmlmessage)
    for line in s.makefile():
        print line,
    s.close()
    

    The response should look something like:

    HTTP/1.1 200 OK
    <response headers>
    
    <response body>
    
    0 讨论(0)
提交回复
热议问题