views:

147

answers:

2

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

+2  A: 

Have you tried reading the error message? :)

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

badp
Looks like a port of a C idiom?
rq
Isn't the `=` what creates (or tries to create) the new instance of `xmlrpclib.Server`, and returns True on success and False on failure?
Bushman
@Bushman: no, that's not how Python works. Assignments do not return anything in Python.
Felix
Wow, it seems I got the original version of that from a *very* old source. The main xmlrpclib object apparently is Server*Proxy*. I'l see if that works.
Bushman
So, how would I find out if the object actually was created successfully, without actually trying to connect? `while not dir(server): ...` does not work.
Bushman
Also, it seems that a connection error does not return a normal exception. I get "Traceback (most recent call last): ... socket.error: [Errno 111] Connection refused"
Bushman
@Bushman Python has exceptions -- if the function returns, it executed correctly. A traceback is indeed what Python prints when an unchecked exception occur. You would use a `try`/`catch socket.error:` block to handle it.
badp
Of course, it also could that be that the code for the connection method in the xmlrpclib isn't in a try..catch... expression, so it's returning the error raised by the socket module that the xmlrpclib imported...I wonder how to access that...and no, it returns an error saying that the name `socket` is not defined. EDIT: trying to catch `xmlrpclib.socket.error` also does not work.
Bushman
Have you tried adding `import socket` to the top? PS: if there is an exception the function does not return anything at all :)
badp
I imported `socket` and put the address input/testing code in a `try`/`except socket.error, socket.gaierror, xmlrpclib.ProtocolError' block, and it claims that there is a `SyntaxError: invalid syntax`, and the caret points to the comma after `socket.gaierror` in the `except` statement.
Bushman
A: 

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...

Bushman