【问题标题】:gunicorn gevent error sending certain HTML stringsgunicorn gevent 错误发送某些 HTML 字符串
【发布时间】:2013-10-17 20:19:19
【问题描述】:

Gunicorn 在 Ubuntu 12.04 上使用 nginx 代理。

我在一个简单的 gunicorn 服务器中有以下函数用于发送 html 字符串:

def send_html(start_response, replystr):
    try:
        status = '200 OK'
        response_headers = [('Content-type', 'text/html; charset=UTF-8'),
                            ('Content-Length', str(len(replystr)))]
        start_response(status, response_headers)
        return iter([replystr])
    except:
        log.info(traceback.print_exc())
        send_error(start_response, '501')
        return []

如果我执行send_html(start_response, 'Hello world'),它就可以正常工作。但是,当我尝试发送包含新闻文章内容的相当大的字符串时,似乎每次都会收到以下错误:

Traceback (most recent call last):
  File "build/bdist.linux-x86_64/egg/gevent/pywsgi.py", line 504, in handle_one_response
    self.run_application()
  File "build/bdist.linux-x86_64/egg/gevent/pywsgi.py", line 491, in run_application
    self.process_result()
  File "build/bdist.linux-x86_64/egg/gevent/pywsgi.py", line 482, in process_result
    self.write(data)
  File "build/bdist.linux-x86_64/egg/gevent/pywsgi.py", line 375, in write
    self._write_with_headers(data)
  File "build/bdist.linux-x86_64/egg/gevent/pywsgi.py", line 394, in _write_with_headers
    towrite.extend(data)
TypeError: an integer or string of size 1 is required

gevent pywsgi 源码告诉我towrite 是一个字节数组:

    def _write_with_headers(self, data):
        towrite = bytearray()
        self.headers_sent = True
        self.finalize_headers()

        towrite.extend('%s %s\r\n' % (self.request_version, self.status))
        for header in self.response_headers:
            towrite.extend('%s: %s\r\n' % header)

        towrite.extend('\r\n')
        if data:
            if self.response_use_chunked:
                ## Write the chunked encoding
                towrite.extend("%x\r\n%s\r\n" % (len(data), data))
            else:
                towrite.extend(data)
        self._sendall(towrite)

文章内容是不是用字节数组什么的不好玩?

【问题讨论】:

  • 我的第一个猜测是replystr 没有正确编码——你能告诉我们触发错误的字符串吗?尝试将字符串缩小到仍然会导致问题的小字符串。
  • 这就是我最终所做的。我不记得究竟是什么字符串导致了错误,但正如您在我的回答中看到的,将其编码为 utf-8 字节数组解决了我的特定问题。

标签: python-2.7 gevent gunicorn


【解决方案1】:

我修改了 send_html 函数以适应我的目的,在发送之前将 replystr 转换为字节数组。不确定这个解决方案的可持续性如何,但它已经工作了一段时间。

def send_html(start_response, replystr):
    try:
        status = '200 OK'
        #coerce the string to a bytearray
        replystr = bytearray(replystr, 'UTF-8') 
        response_headers = [('Content-type', 'text/html; charset=UTF-8'),
                            ('Content-Length', str(len(replystr)))]
        start_response(status, response_headers)
        return iter([replystr])
    except:
        log.info(traceback.print_exc())
        send_error(start_response, '501')
        return []

【讨论】:

    【解决方案2】:

    doing towrite.extend(str("%x\r\n%s\r\n" % (len(data), data))) 会解决问题的。

    “扩展”的原因将不接受 unicode。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 2012-11-13
      • 2016-03-11
      • 2015-01-21
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多