[python]用requests请求接口报LineTooLong的错误

今天用requests库来请求接口合成公众号文章的音频时,一直合成失败,查看日志,报的是同样的错误:

Traceback (most recent call last):
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "", line 2, in raise_from
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 321, in begin
    self.headers = self.msg = parse_headers(self.fp)
  File "/usr/lib/python3.6/http/client.py", line 208, in parse_headers
    raise LineTooLong("header line")
http.client.LineTooLong: got more than 65536 bytes when reading header line
​
During handling of the above exception, another exception occurred:
​
Traceback (most recent call last):
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "", line 2, in raise_from
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/urllib3/connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse
    response.begin()
  File "/usr/lib/python3.6/http/client.py", line 321, in begin
    self.headers = self.msg = parse_headers(self.fp)
  File "/usr/lib/python3.6/http/client.py", line 208, in parse_headers
    raise LineTooLong("header line")
urllib3.exceptions.ProtocolError: ('Connection aborted.', LineTooLong('got more than 65536 bytes when reading header line',))
​
During handling of the above exception, another exception occurred:
​
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "article_tts.py", line 153, in update_tts
    result = self.text2audio(text, artid, speaker)
  File "article_tts.py", line 257, in text2audio
    r = requests.post(self.api, headers=headers, data=body.encode('utf-8'))
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/api.py", line 112, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/adapters.py", line 495, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', LineTooLong('got more than 65536 bytes when reading header line',))

从报错的信息得知,是程序读取返回数据时引起的错误,返回头的数据超过了65536bytes,也就是请求已经发送出去的了,接口服务器那边也接受到了请求并且返回了数据,问题就在读取返回数据那里。既然知道问题在哪了,但是不知道怎么解决呀?

上网问社区,好像遇到这种错误的很少,甚至找不到。也不奇怪,因为一般正常来说返回头的数据哪有超过65536bytes的,很少,但是内部自己使用的接口也说不准会出现,那没办法了,只有自己捣鼓摸索了。

这种最大值应该是可以修改的,requests库不可能那么死板,那是在哪里修改呢?

首先想到的是看源码,根据错误信息,找到对应错误位置

File "/home/rhys/.virtualenvs/tools-backend/lib/python3.6/site-packages/requests/adapters.py", line 495, in send    raise ConnectionError(err, request=request)​requests.exceptions.ConnectionError: ('Connection aborted.', LineTooLong('got more than 65536 bytes when reading header line',))

找到源码报错的位置,在其上下文也没看出什么东东,全文搜索65536也没有找到配置的位置。那好吧,用linux命令中的grep文本查找命令来查吧。

grep 65536 -rl ./

切换到requests包目录下,执行以上命令,以为可以查到配置位置在哪,但是,竟然没有结果。what?

然后又回去看报错信息,看到了http.py这个文件

File "/usr/lib/python3.6/http/client.py", line 1331, in getresponse    response.begin()  File "/usr/lib/python3.6/http/client.py", line 321, in begin    self.headers = self.msg = parse_headers(self.fp)  File "/usr/lib/python3.6/http/client.py", line 208, in parse_headers    raise LineTooLong("header line")http.client.LineTooLong: got more than 65536 bytes when reading header line

会不会配置在这个文件下,那就查查看,进入到client.py文件,Ctrl+f搜索65536,果然,配置在这里。

[python]用requests请求接口报LineTooLong的错误_第1张图片

OK,那好办了,在事先导入的时候就修改其最大值

最后,重新运行,成功解决。

 

关注公众号:日常bug,每天至少一篇技术文章,适合技术点滴积累,利用琐碎时间学习技术的人。

你可能感兴趣的:(Linux,python高级编程)