我已经下载并安装了 Python 2.5.4 在我的计算机上(我的操作系统是 Windows XP),下载“Goggle App Engine 软件开发套件” 并用 Python 创建了我的第一个应用程序,该应用程序是一个名为 helloworld 包含一个同名的小 python 文件 (helloworld.py)。以下是该小文件的内容:
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
每当我使用 “Goggle App Engine 软件开发套件”,我的浏览器 (FireFox) 总是向我显示一个白色窗口,上面写着 Hello, world! > 写在里面。
然后我下载了 Twill 并将其解压到 helloworld< /em> 目录。 正确安装 Twill,我能够从 Twill shell 执行一些小命令。例如,我可以通过某个链接转到网页:
然后我想直接从Python(即通过使用 Python 中的 Twill。)以下是 Twill 文档页面的内容描述如下:
twill 的 Python API
使用 TwillBrowser 制作扩展
twill 本质上是 mechanize 包周围的一个薄壳。所有斜纹命令都在commands.py 文件中实现,pyparsing 负责解析输入并将其转换为Python 命令的工作(请参阅parse.py)。交互式 shell 工作和 readline 支持是通过 cmd 模块(来自标准 Python 库)实现的。
在 Python 中使用斜纹
从 Python 中使用斜纹有两种相当简单的方法。 (它们彼此兼容,因此您无需在它们之间进行选择;只需使用合适的即可。)
第一个是简单地导入commands.py 中的所有命令并直接从Python 使用它们。例如,
from twill.commands import *
go("http://www.python.org/")
showforms()
这具有非常简单的优点,并且直接与命令参考中记录的命令集相关联。
所以我决定使用这种方式。我删除了 helloworld.py 之前的内容并为其提供了新内容:
from twill.commands import *
go("http://www.python.org/")
showforms()
但是当我尝试使用 “Goggle App Engine 软件开发工具包”,我的浏览器,而不是描绘www.python.org 网站的内容给我一条错误消息:'module' object has no attribute 'Popen' :
请看一下整个页面此处。
以下是该页面的最后三行:
: 'module' 对象没有属性 'Popen'
args = ("'module' object has no attribute 'Popen'",)
message = "'module' object has no attribute 'Popen'"
请任何人向我解释一下这个 Popen 属性的含义以及我在这里做错了什么?
预先感谢大家。
更新1
(此更新是我对下面leoluk提供的第二个答案的回应)
你好,leoluk!
我尝试这样做:
config use_tidy 0
from twill.commands import *
go("http://www.python.org/")
但没有成功。我收到此错误消息:(
<type 'exceptions.SyntaxError'>: invalid syntax (helloworld.py, line 1)
args = ('invalid syntax', (r'E:\helloworld\helloworld.py', 1, 15, 'config use_tidy 0\n'))
filename = r'E:\helloworld\helloworld.py'
lineno = 1
message = ''
msg = 'invalid syntax'
offset = 15
print_file_and_line = None
text = 'config use_tidy 0\n'
您可以在此处<查看整个页面/strong>)
您知道这意味着什么以及出了什么问题吗?
I have downloaded and installed Python 2.5.4 on my computer (my OS is Windows XP), downloaded “Goggle App Engine Software Development Kit” and created my first application in Python, which was a directory named helloworld that contained a small python file with the same name (helloworld.py). Here are the contents of that small file:
print 'Content-Type: text/plain'
print ''
print 'Hello, world!'
Whenever I ran this application locally on my computer with “Goggle App Engine Software Development Kit”, my browser (FireFox) always showed me a white window with Hello, world! written in it.
Then I downloaded Twill and unpacked it into helloworld directory. Having installed Twill properly, I was able to execute some small commands from Twill shell. For example, I could turn to a web page by some link:
Then I wanted to perform the same operation directly from Python (i.e. by means of using Twill from Python.) Here is what the Twill documentation page says about it:
twill's Python API
Using TwillBrowser Making extensions
twill is essentially a thin shell around the mechanize package. All twill commands are implemented in the commands.py file, and pyparsing does the work of parsing the input and converting it into Python commands (see parse.py). Interactive shell work and readline support is implemented via the cmd module (from the standard Python library).
Using twill from Python
There are two fairly simple ways to use twill from Python. (They are compatible with each other, so you don't need to choose between them; just use whichever is appropriate.)
The first is to simply import all of the commands in commands.py and use them directly from Python. For example,
from twill.commands import *
go("http://www.python.org/")
showforms()
This has the advantage of being very simple, as well as being tied directly to the documented set of commands in the commands reference.
So I decided to use this way. I deleted the previous contents of helloworld.py and gave it the new contents:
from twill.commands import *
go("http://www.python.org/")
showforms()
But when I tried to run that file on my computer with “Goggle App Engine Software Development Kit”, my browser, instead of depicting the contents of www.python.org web site, gives me an error message: 'module' object has no attribute 'Popen' :
Please, take a look at the whole page here.
Here are the last three lines of that page:
: 'module' object has no attribute 'Popen'
args = ("'module' object has no attribute 'Popen'",)
message = "'module' object has no attribute 'Popen'"
Can anybody, please, explain to me what this Popen attribute is all about and what I am doing wrong here?
Thank you all in advance.
Update 1
(this update is my response to the second answer provided below by leoluk)
Hello, leoluk!!!
I tried doing it this way:
config use_tidy 0
from twill.commands import *
go("http://www.python.org/")
but it didn't work. I received this error message:
<type 'exceptions.SyntaxError'>: invalid syntax (helloworld.py, line 1)
args = ('invalid syntax', (r'E:\helloworld\helloworld.py', 1, 15, 'config use_tidy 0\n'))
filename = r'E:\helloworld\helloworld.py'
lineno = 1
message = ''
msg = 'invalid syntax'
offset = 15
print_file_and_line = None
text = 'config use_tidy 0\n'
(You can see the whole page HERE)
Do You have any idea what it means and what went wrong?
发布评论
评论(3)
您无法使用 Google App 引擎中的任何内容。 Twill 使用谷歌应用程序引擎上不可用的东西来工作。因此应用程序引擎不完全支持斜纹。
值得注意的是,该代码尝试调用外部命令
tidy
,并且在应用引擎中调用外部命令不起作用。you can't use anything in the Google App engine. Twill uses stuff not available on google app engine to work. So twill is not fully supported by app engine.
notably, the code is trying to call on an external command,
tidy
, and calling external commands in app engine doesn't work.我认为你应该直接使用
mechanize
。 Twill 以 Google App Engine 不支持的方式与系统通信。I think you should use
mechanize
directly. Twill communicates with the system in a way that's not supported by Google App Engine.