httplib - http不接受内容长度

时间:2016-05-26 17:44:43

标签: python rest http http-headers httplib

问题

当我切换Macbook时,我突然得到一个HTTP 411:需要长度(我没有使用不同的Mac获得此功能)尝试使用POST请求httplib }。我似乎无法为此找到解决办法。

代码部分1 :来自支持类;检索数据和其他东西,

class Data(object):
    def __init__(self, value):
        self.company_id = None
        self.host = settings.CONSUMER_URL
        self.body = None
        self.headers = {"clienttype": "Cloud-web", "Content-Type": "application/json", "ErrorLogging": value}

    def login(self):
        '''Login and store auth token'''
        path = "/Security/Login"
        body = self.get_login_info()
        status_code, resp = self.submit_request("POST", path, json.dumps(body))
        self.info = json.loads(resp)
        company_id = self.get_company_id(self.info)
        self.set_token(self.info["token"])
        return company_id

    def submit_request(self, method, path, body=None, header=None):
        '''Submit requests for API tests'''
        conn = httplib.HTTPSConnection(self.host)
        conn.set_debuglevel(1)
        conn.request(method, path, body, self.headers)
        resp = conn.getresponse()
        return resp.status, resp.read()

代码部分2 :我的单元测试,

# logging in
cls.api = data.Data(False) # initializing the data class from Code Portion 1
cls.company_id = cls.api.login()

...

    # POST Client/Register
    def test_client_null_body(self):
        '''Null body object - 501'''
        status, resp = self.api.submit_request('POST', '/Client/register')
        if status != 500:
        log.log_warning('POST /Client/register: %s, %s' % (str(status), str(resp)))
        self.assertEqual(status, 500)

代码部分3 :我从设置文件发送的数据示例,

API_ACCOUNT = {
    "userName": "account@account.com",
    "password": "password",
    "companyId": 107
}

来自记录

WARNING:root: POST /Client/register: 411, <!DOCTYPE HTML PUBLIC "-//W3C//DTD 

HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>

其他信息:我正在使用2008 Macbook Pro而没有问题。切换到2013 Macbook Pro,这种情况不断发生。

我看了一下这篇文章: Python httplib and POST似乎当时httplib没有自动生成内容长度。

现在https://docs.python.org/2/library/httplib.html

  

如果标题中未提供,则Content-Length标题会自动添加 所有方法如果可以确定正文的长度,则来自str表示的长度,或者报告的磁盘上文件大小。

使用conn.set_debuglevel(1)时,我们看到httplib 发送标头

reply: 'HTTP/1.1 411 Length Required\r\n'
header: Content-Type: text/html; charset=us-ascii
header: Server: Microsoft-HTTPAPI/2.0
header: Date: Thu, 26 May 2016 17:08:46 GMT
header: Connection: close
header: Content-Length: 344

修改

单位测试失败:

======================================================================
FAIL: test_client_null_body (__main__.NegApi)
Null body object - 501
----------------------------------------------------------------------
Traceback (most recent call last):
  File "API_neg.py", line 52, in test_client_null_body
    self.assertEqual(status, 500)
AssertionError: 411 != 500

.send: 'POST /Client/register HTTP/1.1\r\nHost: my.host\r\nAccept-Encoding: identity\r\nAuthorizationToken: uhkGGpJ4aQxm8BKOCH5dt3bMcwsHGCHs1p+OJvtf9mHKa/8pTEnKyYeJr+boBr8oUuvWvZLr1Fd+Og2xJP3xVw==\r\nErrorLogging: False\r\nContent-Type: application/json\r\nclienttype: Cloud-web\r\n\r\n'
reply: 'HTTP/1.1 411 Length Required\r\n'
header: Content-Type: text/html; charset=us-ascii
header: Server: Microsoft-HTTPAPI/2.0
header: Date: Thu, 26 May 2016 17:08:27 GMT
header: Connection: close
header: Content-Length: 344

有关为何在以前的Mac上运行并且目前无法在此处运行的任何想法?它是相同的代码,相同的操作系统。如果我能提供更多信息,请告诉我。

编辑2 问题似乎与OSX 10.10.4有关,升级到10.10.5之后一切都很好。我仍然希望了解我遇到此问题的原因。

从10.10.4到10.10.5的唯一变化,似乎很接近,将是从2.7.6到2.7.10的python更新,其中包括此错误修复:http://bugs.python.org/issue22417

0 个答案:

没有答案