3

我当前的测试 python 脚本执行以下操作:

#!/usr/bin/env python
import sys
data = sys.stdin.read()
myjson = json.loads(data)

问题是即使这在某些情况下有效,但在其他情况下它似乎会阻塞,可能在 read() 处。

由于其他一些原因,我被迫在 cgi 脚本中使用 tomcat,但不确定这是否重要。

4

1 回答 1

2

You'll need to check the content length before reading and limit the number of byte read by sys.stdin.read(). See cgi.parse_header().

Update:

Your incoming data comes through the environment which is populated by the web server. It is accessible in os.environ.

import os
from cgi import parse_header
os.environ['Content-Type'] = 'text/html; charset=utf-8'
parse_header(os.environ['Content-Type'])
# returns ('text/html', {'charset': 'utf-8'})

So in your CGI script you need (roughly):

import os, cgi, sys
cl, _ = parse_header(os.environ['Content-Length'])
data = sys.stdin.read(int(cl))
于 2013-02-20T10:49:56.350 回答