从 Python 脚本中调用 Python 交互式解释器
有没有什么方法可以从脚本中启动Python解释器,其方式类似于仅使用python -i,以便保留当前脚本中的对象/命名空间等?不使用 python -i 的原因是该脚本初始化了与 XML-RPC 服务器的连接,并且如果有一个连接,我需要能够停止整个程序错误。在有有效输入之前我无法循环,因为显然,我不能做这样的事情:
#!/usr/bin/python -i
# -*- coding: utf-8 -*-
import xmlrpclib
# Create an object to represent our server.
server_url = str(raw_input("Server: "))
while not server = xmlrpclib.Server(server_url):
print 'Unable to connect to server. Please try again'
else:
print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
break
# Python interpreter starts...
因为:
% chmod u+x ./rpcclient.py
% ./rpclient.py
Traceback (most recent call last):
File "./rpcclient.py", line 8
while not server = xmlrpclib.Server(server_url):
^
SyntaxError: invalid syntax
>>>
不幸的是,python -i
在打印出回溯后立即启动解释器,所以我以某种方式调用交互式解释器 - 替换脚本的执行,以便它保留服务器连接 - 从脚本内部
Is there any way to start up the Python interpreter from within a script , in a manner similar to just using python -i
so that the objects/namespace, etc. from the current script are retained? The reason for not using python -i
is that the script initializes a connection to an XML-RPC server, and I need to be able to stop the entire program if there's an error. I can't loop until there's valid input because apparently, I can't do something like this:
#!/usr/bin/python -i
# -*- coding: utf-8 -*-
import xmlrpclib
# Create an object to represent our server.
server_url = str(raw_input("Server: "))
while not server = xmlrpclib.Server(server_url):
print 'Unable to connect to server. Please try again'
else:
print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
break
# Python interpreter starts...
because:
% chmod u+x ./rpcclient.py
% ./rpclient.py
Traceback (most recent call last):
File "./rpcclient.py", line 8
while not server = xmlrpclib.Server(server_url):
^
SyntaxError: invalid syntax
>>>
Unfortunately, python -i
starts the interpreter just after it prints out the traceback, so I somehow have to call the interactive interpreter - replacing the execution of the script so it retains the server connection - from within the script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我终于开始工作了。
基本上,我将整个
try
/except
/else
子句放在while True:
循环中,并使用 < code>else 套件是一个break
语句,而except
套件的结尾是一个continue
语句。结果是,如果用户输入的地址没有完全兼容的 XML-RPC2 服务器侦听,它现在会不断循环。结果是这样的:非常感谢!
...我必须再等一天才能接受这一点...
Well, I finally got it to work.
Basically, I put the entire
try
/except
/else
clause in awhile True:
loop, with theelse
suite being abreak
statement and the end of theexcept
suite being acontinue
statement. The result is that it now continually loops if the user puts in an address that doesn't have a fully compliant XML-RPC2 server listening. Here's how it turned out:Thank you very much!
...and I have to wait another day to accept this...
您是否尝试过阅读错误消息? :)
=
是赋值,您需要使用比较运算符==
来代替。Have you tried reading the error message? :)
=
is assignment, you want the comparison operator==
instead.