Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

uwsgi returns blank output

Tags:

python

uwsgi

I have setup nginx and uwsgi on a CentOS 6 server. When I POST some data to the webserver it is processed correctly but no output is returned. If I print the generated html just before the application just before the application function returns it, the HTML shows up correct in the console however the next line on the console is:

[pid: 31650|app: 0|req: 2/2] <server ip> () {48 vars in 873 bytes} [Mon Sep 15 18:19:45 2014] POST / => generated 0 bytes in 10583 msecs (HTTP/1.1 200) 1 headers in 44 bytes (418 switches on core 0)

I have increased the socket timeout but doesn't make a difference.

EDIT: I put in a strange workaround for this. My html was being stored in the variable 'html'. I changed my code from:

return [html] #This would not return any output even though 'print html' was fine

To:

open('/tmp/ot.txt', 'w').write(html)
d = open('/tmp/ot.txt').read()
return [d] #This works!

I would prefer not using my workaround. Any ideas why this works and the original doesn't. Python version is 2.6.6

like image 204
Ravi Avatar asked Sep 15 '14 18:09

Ravi


1 Answers

your problem is likely that uwsgi silently drops any unicode objects, and only shows byte strings. that's why when you write to a file and read it back, it works, because write() silently converts unicode to ascii encoding (raising an exception if there's anything with ord(x) > 127), and read() returns bytes.

the solution in that case would be to return [html.encode('utf8')] or whatever encoding you are using.

I only found this out because I was calling markdown.markdown(text) on any .md files, and appending .html files directly to my output. only the HTML was showing, not the markdown, which module returns Unicode.

like image 196
jcomeau_ictx Avatar answered Sep 21 '22 12:09

jcomeau_ictx