从 Python 脚本中调用 Python 交互式解释器

发布于 2024-09-06 14:35:57 字数 961 浏览 4 评论 0原文

有没有什么方法可以从脚本中启动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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少情深 2024-09-13 14:35:58

好吧,我终于开始工作了。

基本上,我将整个 try/except/else 子句放在 while True: 循环中,并使用 < code>else 套件是一个 break 语句,而 except 套件的结尾是一个 continue 语句。结果是,如果用户输入的地址没有完全兼容的 XML-RPC2 服务器侦听,它现在会不断循环。结果是这样的:

#!/usr/bin/python -i
# -*- coding: utf-8 -*-

import xmlrpclib, socket
from sys import exit

# Create an object to represent our server.

#server = xmlrpclib.Server(server_url) and print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
server_url = str(raw_input("Server: "))
server = xmlrpclib.ServerProxy(server_url)
while True:
    try:
        server.system.listMethods()
    except xmlrpclib.ProtocolError, socket.error:
        print 'Unable to connect to server. Please try again'
        server_url = str(raw_input("Server: "))
        server = xmlrpclib.ServerProxy(server_url)
        continue
    except EOFError:
        exit(1)
    else:
        break

print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"

# Python interpreter starts...

非常感谢!

...我必须再等一天才能接受这一点...

Well, I finally got it to work.

Basically, I put the entire try/except/else clause in a while True: loop, with the else suite being a break statement and the end of the except suite being a continue 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:

#!/usr/bin/python -i
# -*- coding: utf-8 -*-

import xmlrpclib, socket
from sys import exit

# Create an object to represent our server.

#server = xmlrpclib.Server(server_url) and print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"
server_url = str(raw_input("Server: "))
server = xmlrpclib.ServerProxy(server_url)
while True:
    try:
        server.system.listMethods()
    except xmlrpclib.ProtocolError, socket.error:
        print 'Unable to connect to server. Please try again'
        server_url = str(raw_input("Server: "))
        server = xmlrpclib.ServerProxy(server_url)
        continue
    except EOFError:
        exit(1)
    else:
        break

print 'Xmlrpclib.Server object `__main__.server\' of URL `', server_url, "' created"

# Python interpreter starts...

Thank you very much!

...and I have to wait another day to accept this...

橙幽之幻 2024-09-13 14:35:57

您是否尝试过阅读错误消息? :)

= 是赋值,您需要使用比较运算符 == 来代替。

Have you tried reading the error message? :)

= is assignment, you want the comparison operator == instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文