Python CGI和STDIN [英] Python cgi and stdin

查看:83
本文介绍了Python CGI和STDIN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用pycurl通过put和python cgi脚本上传文件,以在服务器端接收文件.本质上,服务器端的代码是:

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

这似乎适用于文本,但不适用于二进制文件(我在Windows上).对于二进制文件,执行stdin.read的循环在接收到大约10kb至100kb的内容后中断.有什么想法吗?

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?

推荐答案

您需要以二进制模式运行Python.从以下位置更改CGI脚本:

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

#!C:/Python25/python.exe

或说什么:

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

或者您可以通过编程方式做到这一点:

Or you can do it programmatically like this:

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

开始从 stdin 读取之前.

这篇关于Python CGI和STDIN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆