Python3瓶请求代理更改主机挂起并在wsgiref模块下引发BrokenPipeError

时间:2018-11-12 13:06:34

标签: python http python-requests bottle

我正在python中使用Bottle创建一个简单的应用程序代理。

我只想更改主机,代码如下:

def get_proxy_response():
    print('>>>> START')
    resp = requests.request('get', 'http://127.0.0.1:18080/', headers=dict(Host='www.mysite.com'))
    print('<<<< FINISH')
    return resp

该代码可在我的开发笔记本电脑上使用,但是当我将代码投入生产环境(带有Python3.4.8的CentOS6)时,它挂在了请求行中,并保存了很长时间,最终引发了以下错误,然后导致504响应:

>>>> START
127.0.0.1 - - [12/Nov/2018 20:59:57] "GET /index.html HTTP/1.0" 504 1229
Traceback (most recent call last):
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 332, in send_headers
    self.send_preamble()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 453, in _write
    self.stdout.write(data)
  File "/usr/lib64/python3.4/socket.py", line 398, in write
    return self._sock.send(b)
BrokenPipeError: [Errno 32] Broken pipe
127.0.0.1 - - [12/Nov/2018 20:59:57] "GET /index.html HTTP/1.0" 500 59
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 43046)
Traceback (most recent call last):
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 138, in run
    self.finish_response()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 332, in send_headers
    self.send_preamble()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 255, in send_preamble
    ('Date: %s\r\n' % format_date_time(time.time())).encode('iso-8859-1')
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 453, in _write
    self.stdout.write(data)
  File "/usr/lib64/python3.4/socket.py", line 398, in write
    return self._sock.send(b)
BrokenPipeError: [Errno 32] Broken pipe

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 141, in run
    self.handle_error()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 368, in handle_error
    self.finish_response()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 180, in finish_response
    self.write(data)
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 274, in write
    self.send_headers()
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 331, in send_headers
    if not self.origin_server or self.client_is_modern():
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 344, in client_is_modern
    return self.environ['SERVER_PROTOCOL'].upper() != 'HTTP/0.9'
TypeError: 'NoneType' object is not subscriptable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.4/socketserver.py", line 305, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib64/python3.4/socketserver.py", line 331, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib64/python3.4/socketserver.py", line 344, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib64/python3.4/socketserver.py", line 673, in __init__
    self.handle()
  File "/usr/lib64/python3.4/wsgiref/simple_server.py", line 133, in handle
    handler.run(self.server.get_app())
  File "/usr/lib64/python3.4/wsgiref/handlers.py", line 144, in run
    self.close()
  File "/usr/lib64/python3.4/wsgiref/simple_server.py", line 35, in close
    self.status.split(' ',1)[0], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------

更令人困惑的是,情况取决于我设置的主机名,当我使用Host: www.mysite.com时,后端应用程序将返回正确的内容,而其他应用程序将返回简单的Site not found内容

因此,如果我将请求标头设置为headers=dict(Host='www.yoursite.com'),它按预期完成了,说明了正确的Site not found内容。

0 个答案:

没有答案