BaseHTTPServer怪异 [英] BaseHTTPServer weirdness

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

问题描述

我正在试图弄清楚如何使用BaseHTTPServer。这是我的小

测试应用程序:


===================== ============

#!/ usr / bin / python
来自BaseHTTPServer导入的
* br />

导入cgi


class myHandler(BaseHTTPRequestHandler):


def do_GET(r):

s =''''

尝试:

s = cgi.parse_qs(r.rfile.read(int(r.headers.get

(Content-length))),1)

除外:

通过


r .send_response(200)

r.send_header(" Content-type"," text / html")

r.end_headers()

r.wfile.write("""

< form method = post action = foo>

< input type = text name = text1 value ="">

< input type = text name = text2 value ="">

< input type = submit>

< / form%s

"""%s)


def do_POST(r):

r.do_GET()

d = HTTPServer(('''',1024),myHandler)

d.serve_forever()


======= ============================

两个问题:

1.行:


s = cgi.parse_qs(r.rfile.read(int(r.headers.get(" Conte nt-length")) ),1)


感觉就像一个可怕的黑客。看起来这将是一个更好的

替代方案:


s = cgi.parse(r.rfile)


但这实际上并没有奏效。为什么?在BaseHTTPServer中解析

表单数据的正确方法是什么?


2.尽管我正在为keep_blank_values传递1 br />
cgi.parse_qs的参数,它实际上并没有保留空值。

这是一个错误,还是我做错了什么?


谢谢,

rg

解决方案

Ron Garret写道:


我正在试图弄清楚如何使用BaseHTTPServer。这是我的小

测试应用程序:


===================== ============

#!/ usr / bin / python
来自BaseHTTPServer导入的
* br />

导入cgi


class myHandler(BaseHTTPRequestHandler):


def do_GET(r):

s =''''

尝试:

s = cgi.parse_qs(r.rfile.read(int(r.headers.get

(Content-length))),1)

除外:

通过


r .send_response(200)

r.send_header(" Content-type"," text / html")

r.end_headers()

r.wfile.write("""

< form method = post action = foo>

< input type = text name = text1 value ="">

< input type = text name = text2 value ="">

< input type = submit>

< / form%s

"""%s)


def do_POST(r):

r.do_GET()


d = HTTPServer(('''', 1024),myHandler)

d.serve_forever()


==================== ===============

两个问题:


1.行:


s = cgi.parse_qs(r.rfile.read(int(r.headers.get(" Conte nt-length"))),1)


感觉像是一个可怕的黑客。看起来这将是一个更好的

替代方案:


s = cgi.parse(r.rfile)


但这实际上并没有奏效。为什么?什么是在BaseHTTPServer中解析

表单数据的正确方法?



正常方式是


s = cgi.parse()


因为CGI脚本看到客户端网络套接字(在消耗了HTTP标头的
之后)作为其标准输入。但是我不确定它当前在处理像gzip

压缩数据这样的奇怪输入方面做了多少。


2.尽管我将一个1为keep_blank_values

参数传递给cgi.parse_qs,但它实际上并没有保留空白值。

这是一个错误,还是我做错了什么?



听起来像一个bug,但是因为你的解析看起来有点儿我'很惊讶

你得到任何东西。尝试使用关键字参数

keep_blank_values = 1以防万一订单已更改或某事物

daft。但是首先修复你的解析。


另外需要注意的是,因为你要将字典'

字符串表示直接输入到你的HTML中这里有奇怪的

字符,这可能会在浏览器中给你带来奇怪的输出,所以你要查看页面来源以确保不是这种情况。哪一个

可能不是......


问候

Steve

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/steve.holden


文章< ma ************************************* @ python.or g> ;,

Steve Holden< st *** @ holdenweb.comwrote:


正常的方式是


s = cgi.parse()


因为CGI脚本看到客户端网络套接字(在消耗了HTTP标头的
之后)作为其标准输入。



不起作用。 (我甚至尝试过sys.stdin = r.rfile; s = cgi.parse())不要忘记,这不是CGI脚本,它是BaseHTTPServer的处理程序。


2.尽管我正在为keep_blank_values传递一个
$ b cgi.parse_qs的$ b参数,它实际上并没有保留空值。

这是一个错误,还是我做错了什么?



听起来像一个bug,但是因为你的解析看起来有点儿我很惊讶

你得到任何东西。尝试使用关键字参数

keep_blank_values = 1以防万一订单已更改或某事物

daft。但是首先修复你的解析。


另外需要注意的是,因为你要将字典'

字符串表示直接输入到你的HTML中这里有奇怪的

字符,这可能会在浏览器中给你带来奇怪的输出,所以你要查看页面来源以确保不是这种情况。这可能是
可能不是......



我知道这不是问题,因为它在我使用时确实有效parse_qs。

(我知道逃避HTML和所有这些,但这只是一个小测试

程序。)


rg


Ron Garret写道:


文章< ma ********* ****************************@python.or g> ;,

Steve Holden< st ** *@holdenweb.com写道:


>>正常的方式是

s = cgi.parse()

因为CGI脚本将客户端网络套接字(在消费HTTP标头之后)视为其标准输入。




不行。 (我甚至尝试过sys.stdin = r.rfile; s = cgi.parse())不要忘记,这不是CGI脚本,它是BaseHTTPServer的处理程序。



对。我的错。然而,显然有些事情发生了变化,

因为否则你会期望在输出中至少看到一个空的字典。


>


>>> 2。尽管我将keep_blank_values
参数的1传递给cgi.parse_qs,但它实际上并没有保留空值。这是一个错误,还是我做错了什么?


听起来像一个bug,但是因为你的解析看起来有点儿我很惊讶
你得到任何东西。尝试使用关键字参数
keep_blank_values = 1,以防万一订单发生变化或者说是尴尬。但是先修复你的解析。



阅读2.4.3库的来源显示有人添加了

environ = os.environ参数,这将是

位置调用的第二个参数,因此可以清除这个神秘感。 doicumentation

应该真正显示这些作为关键字参数,而不是暗示他们

是定位。如果您能将此报告为

文档错误,那就太好了 - 虽然我相信现在2.5rc2版本将冻结


< blockquote class =post_quotes>


>>另外需要注意的是,因为你要将字典'
字符串表示直接输入HTML如果其中有奇怪的
字符,这可能会在浏览器中给你奇怪的输出,所以你应该查看页面来源以确保不是这种情况。它可能不是......




我知道这不是问题,因为它在我使用时确实有效parse_qs。

(我知道逃避HTML和所有这些,但这只是一个小测试

程序。)



我怀疑你的剩余问题(cgi_parse似乎是

返回一个* string *,该死)是由于这个过程你

正在运行HTTP服务器没有环境变量

设置服务器如果确实在CGI

上下文中调用它们将设置,并且CGI库预计将被设置。你可以试试

将它们作为一个明确的环境参数传递,看看是否有用。


但基本上,你没有提供CGI环境,这就是为什么

cgi.parse()不起作用。


问候

史蒂夫

-

Steve Holden +44 150 684 7255 +1 800 494 3119

Holden Web LLC / Ltd http://www.holdenweb.com

Skype:holdenweb http://holdenweb.blogspot.com

最近的Ramblings http://del.icio.us/steve.holden

I''m trying to figure out how to use BaseHTTPServer. Here''s my little
test app:

=================================

#!/usr/bin/python

from BaseHTTPServer import *

import cgi

class myHandler(BaseHTTPRequestHandler):

def do_GET(r):
s = ''''
try:
s = cgi.parse_qs(r.rfile.read(int(r.headers.get
("Content-length"))), 1)
except:
pass

r.send_response(200)
r.send_header("Content-type", "text/html")
r.end_headers()
r.wfile.write("""
<form method=post action=foo>
<input type=text name=text1 value="">
<input type=text name=text2 value="">
<input type=submit>
</form%s
""" % s)

def do_POST(r):
r.do_GET()
d = HTTPServer(('''', 1024), myHandler)
d.serve_forever()

===================================

Two questions:

1. The line:

s = cgi.parse_qs(r.rfile.read(int(r.headers.get("Conte nt-length"))), 1)

feels like a horrible hack. It seems like this would be a better
alternative:

s = cgi.parse(r.rfile)

but that doesn''t actually work. Why? What is the Right Way to parse
form data in a BaseHTTPServer?

2. Despite the fact that I''m passing a 1 for the keep_blank_values
argument to cgi.parse_qs, it doesn''t actually keep blank values. Is
this a bug, or am I doing something wrong?

Thanks,
rg

解决方案

Ron Garret wrote:

I''m trying to figure out how to use BaseHTTPServer. Here''s my little
test app:

=================================

#!/usr/bin/python

from BaseHTTPServer import *

import cgi

class myHandler(BaseHTTPRequestHandler):

def do_GET(r):
s = ''''
try:
s = cgi.parse_qs(r.rfile.read(int(r.headers.get
("Content-length"))), 1)
except:
pass

r.send_response(200)
r.send_header("Content-type", "text/html")
r.end_headers()
r.wfile.write("""
<form method=post action=foo>
<input type=text name=text1 value="">
<input type=text name=text2 value="">
<input type=submit>
</form%s
""" % s)

def do_POST(r):
r.do_GET()
d = HTTPServer(('''', 1024), myHandler)
d.serve_forever()

===================================

Two questions:

1. The line:

s = cgi.parse_qs(r.rfile.read(int(r.headers.get("Conte nt-length"))), 1)

feels like a horrible hack. It seems like this would be a better
alternative:

s = cgi.parse(r.rfile)

but that doesn''t actually work. Why? What is the Right Way to parse
form data in a BaseHTTPServer?

The normal way is

s = cgi.parse()

since the CGI script sees the client network socket (after consumption
of HTTP headers) as its standard input. However I''m not sure how much it
currently does in the way on handling strange inputs like gzip
compressed data.

2. Despite the fact that I''m passing a 1 for the keep_blank_values
argument to cgi.parse_qs, it doesn''t actually keep blank values. Is
this a bug, or am I doing something wrong?

Sounds like a bug, but then since your parsing looks buggy I''m surprised
you get anything at all. Try using a keyword argument
keep_blank_values=1 just in case the order has changed or something
daft. But fix your parsing first.

The other thing to note is that since you are putting a dictionary''s
string representation out straight into your HTML if there are odd
characters in it this may give you strange output in the browser, so you
should view the page source to ensure that''s not the case. Which it
probably isn''t ...

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


In article <ma*************************************@python.or g>,
Steve Holden <st***@holdenweb.comwrote:

The normal way is

s = cgi.parse()

since the CGI script sees the client network socket (after consumption
of HTTP headers) as its standard input.

Doesn''t work. (I even tried sys.stdin=r.rfile; s=cgi.parse()) Don''t
forget, this is not a CGI script, it''s a handler for a BaseHTTPServer.

2. Despite the fact that I''m passing a 1 for the keep_blank_values
argument to cgi.parse_qs, it doesn''t actually keep blank values. Is
this a bug, or am I doing something wrong?

Sounds like a bug, but then since your parsing looks buggy I''m surprised
you get anything at all. Try using a keyword argument
keep_blank_values=1 just in case the order has changed or something
daft. But fix your parsing first.

The other thing to note is that since you are putting a dictionary''s
string representation out straight into your HTML if there are odd
characters in it this may give you strange output in the browser, so you
should view the page source to ensure that''s not the case. Which it
probably isn''t ...

I know that''s not a problem because it does work when I use parse_qs.
(I know about escaping HTML and all that, but this is just a little test
program.)

rg


Ron Garret wrote:

In article <ma*************************************@python.or g>,
Steve Holden <st***@holdenweb.comwrote:

>>The normal way is

s = cgi.parse()

since the CGI script sees the client network socket (after consumption
of HTTP headers) as its standard input.



Doesn''t work. (I even tried sys.stdin=r.rfile; s=cgi.parse()) Don''t
forget, this is not a CGI script, it''s a handler for a BaseHTTPServer.

Right. My bad. However there''s clearly something screwy going on,
because otherwise you''d expect to see at least an empty dictionary in
the output.

>

>>>2. Despite the fact that I''m passing a 1 for the keep_blank_values
argument to cgi.parse_qs, it doesn''t actually keep blank values. Is
this a bug, or am I doing something wrong?


Sounds like a bug, but then since your parsing looks buggy I''m surprised
you get anything at all. Try using a keyword argument
keep_blank_values=1 just in case the order has changed or something
daft. But fix your parsing first.

Reading the source of the 2.4.3 library shows that someone added an
environ=os.environ argument, which will be the second argument on a
positional call, so that clears that mystery up. The doicumentation
should really show these as keyword arguments rather than implying they
are positionals. It''d be nice if you could report this as a
documentation bug - though I believe by now the 2.5rc2 release will be
frozen.

>>The other thing to note is that since you are putting a dictionary''s
string representation out straight into your HTML if there are odd
characters in it this may give you strange output in the browser, so you
should view the page source to ensure that''s not the case. Which it
probably isn''t ...



I know that''s not a problem because it does work when I use parse_qs.
(I know about escaping HTML and all that, but this is just a little test
program.)

I suspect that the remainder of your problems (cgi_parse appears to be
returning a *string*, dammit) are due to the fact that the process you
are running the HTTP server in doesn''t have the environment variables
set that a server would set if it really were being called in a CGI
context, and which the CGI library expects to be set. You could try
passing them as an explicit environ argument and see if that worked.

But basically, you aren''t providing a CGI environment, and that''s why
cgi.parse() isn''t working.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden


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

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