Python cgi and stdin

回眸只為那壹抹淺笑 提交于 2021-02-10 23:17:04

问题


I'm using pycurl to upload a file via put and python cgi script to receive the file on the server side. Essentially, the code on the server side is:

while True:
   next = sys.stdin.read(4096)
   if not next:
       break
   #.... write the buffer

This seems to work with text, but not binary files (I'm on windows). With binary files, the loop doing stdin.read breaks after receiving anything around 10kb to 100kb. Any ideas?


回答1:


You need to run Python in binary mode. Change your CGI script from:

#!C:/Python25/python.exe

or whatever it says to:

#!C:/Python25/python.exe -u

Or you can do it programmatically like this:

msvcrt.setmode(sys.stdin.fileno(), os.O_BINARY)

before starting to read from stdin.




回答2:


Use mod_wsgi instead of cgi. It will provide you an input file for the upload that's correctly opened.



来源:https://stackoverflow.com/questions/838991/python-cgi-and-stdin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!