如何修复httplib.BadStatusLine异常? [英] How to fix httplib.BadStatusLine exception?

查看:114
本文介绍了如何修复httplib.BadStatusLine异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

URL = "MY HTTP REQUEST URL"
XML = "<port>0</port>"

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


IOError: ('http protocol error', 0, 'got a bad status line', None)

我正在尝试将XML发送到服务器并取回XML.有什么办法可以解决/忽略此异常?

I am trying to send XML to a server and get back XML. Is there any way to fix / ignore this exception?

我知道状态行为空,这会引发此错误.

I know that the status line is empty which is raising this error.

推荐答案

尝试看看您的服务器实际上返回了什么!它可能不是有效的HTTP响应.您可以使用类似的方法将原始的HTTP请求发送到服务器:

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()

响应应类似于:

HTTP/1.1 200 OK
<response headers>

<response body>

这篇关于如何修复httplib.BadStatusLine异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆