5

我有一个 j2me 客户端,可以将一些分块的编码数据发布到网络服务器。我想用python处理数据。该脚本作为 CGI 脚本运行,但显然 apache 将拒绝对 CGI 脚本的分块编码后的请求。据我所知,mod_python、WSGI 和 FastCGI 也不行。

我想知道是否有办法让python脚本处理这种输入。我对任何建议持开放态度(例如,apache2 中的配置设置会组装块,独立的 python 服务器会做同样的事情等)我做了很多谷歌搜索,但没有找到任何可用的东西,这是很奇怪。

我知道在服务器端使用 java 是一种解决方案,但我无法想象这不能用 apache + python 来解决。

4

5 回答 5

6

I had the exact same problem a year ago with a J2ME client talking to a Python/Ruby backend. The only solution I found which doesn't require application or infrastructure level changes was to use a relatively unknown feature of mod_proxy.

Mod_proxy has the ability to buffer incoming (chunked) requests, and then rewrite them as a single request with a Content-Length header before passing them on to a proxy backend. The neat trick is that you can create a tiny proxy configuration which passes the request back to the same Apache server. i.e. Take an incoming chunked request on port 80, "dechunk" it, and then pass it on to your non-HTTP 1.1 compliant server on port 81.

I used this configuration in production for a little over a year with no problems. It looks a little something like this:

ProxyRequests Off

<Proxy http://example.com:81>
  Order deny,allow
  Allow from all
</Proxy>

<VirtualHost *:80>
  SetEnv proxy-sendcl 1
  ProxyPass / http://example.com:81/
  ProxyPassReverse / http://example.com:81/
  ProxyPreserveHost On
  ProxyVia Full

  <Directory proxy:*>
    Order deny,allow
    Allow from all
  </Directory>

</VirtualHost>

Listen 81

<VirtualHost *:81>
  ServerName example.com
  # Your Python application configuration goes here
</VirtualHost>

I've also got a full writeup of the problem and my solution detailed on my blog.

于 2009-07-27T11:51:03.947 回答
2

我会说使用扭曲的框架来构建您的 http 侦听器。Twisted 支持分块编码。

http://python.net/crew/mwh/apidocs/twisted.web.http._ChunkedTransferEncoding.html

希望这可以帮助。

于 2008-11-12T17:56:15.650 回答
2

Apache 2.2 mod_cgi 对我来说工作得很好,当请求被传递给 CGI 应用程序时,Apache 会透明地取消分块。

WSGI 当前不允许分块请求,而 mod_wsgi 确实会通过 411 响应阻止它们。它在 WSGI 2.0 的绘图板上。但是恭喜你找到了可以执行块请求的东西,我以前从未见过!

于 2008-11-12T18:32:21.363 回答
2

你不能用 mod_python 做你想做的事。如果您使用的是 3.0 版,则可以使用 mod_wsgi 来完成。但是,您必须跳出 WSGI 1.0 规范,因为 WSGI 有效地禁止了分块的请求内容。

在http://code.google.com/p/modwsgi/wiki/ChangesInVersion0300中搜索 WSGIChunkedRequest 以了解所需内容。

于 2009-07-29T00:13:45.113 回答
1

也许是配置问题?Django 可以通过 mod_python、WSGI 和 FastCGI 与 Apache 连接,并且可以接受文件上传。

于 2008-11-12T18:29:05.957 回答