Gunicorn behinx an nginx proxy on an Ubuntu 12.04.
I have the following function in a bare bones gunicorn server for sending an html string:
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 []
If I execute send_html(start_response, 'Hello world')
it works just fine. However when I try to send a sizable string containing a news article's content, I seem to get the following error every time:
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
The gevent pywsgi source code tells me towrite
is a byte array:
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)
Does the article content not play nice with a byte array or something?
I have modified the send_html function to suit my purposes by turning the replystr into a byte array before sending it. Not sure how sustainable this solution is, but it has been working for a while now.
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 []