how to make a SimpleXMLRPCServer abort at CTRL-C under windows

Discussion in 'Python' started by News123, Feb 5, 2010.

  1. News123

    News123 Guest

    Hi,



    I'm using an XMLRPC server under Windows.

    What I wonder is how I could create a server, that can be killed with CTRL-C

    The server aborts easily with CTRL-BREAK but not with CTRL-C (under Windows)

    If I press CTRL-C it will only abort when the next RPC call occurs.
    It seems it is blocking in the select() call in the handle_request()
    function.

    Is there any trick, which I overlook?

    thanks in advance for ideas and bye


    N
    News123, Feb 5, 2010
    #1
    1. Advertising

  2. En Fri, 05 Feb 2010 20:03:51 -0300, News123 <> escribió:

    > I'm using an XMLRPC server under Windows.
    >
    > What I wonder is how I could create a server, that can be killed with
    > CTRL-C
    >
    > The server aborts easily with CTRL-BREAK but not with CTRL-C (under
    > Windows)
    >
    > If I press CTRL-C it will only abort when the next RPC call occurs.
    > It seems it is blocking in the select() call in the handle_request()
    > function.


    Python 2.6 and up behaves exactly as you want.
    On previous versions you may use this:

    class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):

    ... your methods ...

    if not hasattr(SimpleXMLRPCServer.SimpleXMLRPCServer, 'shutdown'):

    # pre 2.6
    quit = False

    def serve_forever(self):
    while not self.quit:
    self.handle_request()

    def shutdown(self):
    self.quit = True

    def server_bind(self):
    self.socket.settimeout(1.0)
    SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 6, 2010
    #2
    1. Advertising

  3. News123

    News123 Guest

    Hi Gabriel,



    Gabriel Genellina wrote:
    > En Fri, 05 Feb 2010 20:03:51 -0300, News123 <> escribió:
    >
    >> I'm using an XMLRPC server under Windows.
    >>
    >> What I wonder is how I could create a server, that can be killed with
    >> CTRL-C
    >>
    >> The server aborts easily with CTRL-BREAK but not with CTRL-C (under
    >> Windows)
    >>
    >> If I press CTRL-C it will only abort when the next RPC call occurs.
    >> It seems it is blocking in the select() call in the handle_request()
    >> function.

    >
    > Python 2.6 and up behaves exactly as you want.
    > On previous versions you may use this:]




    I', using python 2.6.4 nd neither on Win-XP nor on Win-7 I'm capable to
    abort with CTR-C ?


    On Linux however I can use CTRL-C .


    >
    > class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
    >
    > ... your methods ...
    >
    > if not hasattr(SimpleXMLRPCServer.SimpleXMLRPCServer, 'shutdown'):
    >
    > # pre 2.6
    > quit = False
    >
    > def serve_forever(self):
    > while not self.quit:
    > self.handle_request()
    >
    > def shutdown(self):
    > self.quit = True
    >
    > def server_bind(self):
    > self.socket.settimeout(1.0)
    > SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)
    >



    I tried something similiar, but without setting the socket timeout.
    I'll try it now with.


    N
    News123, Feb 6, 2010
    #3
  4. News123

    News123 Guest

    Hi Gabriel,

    Gabriel Genellina wrote:
    > En Fri, 05 Feb 2010 20:03:51 -0300, News123 <> escribió:
    >
    >> I'm using an XMLRPC server under Windows.
    >>
    >> What I wonder is how I could create a server, that can be killed with
    >> CTRL-C
    >>
    >> The server aborts easily with CTRL-BREAK but not with CTRL-C (under
    >> Windows)
    >>
    >> If I press CTRL-C it will only abort when the next RPC call occurs.
    >> It seems it is blocking in the select() call in the handle_request()
    >> function.

    >
    > Python 2.6 and up behaves exactly as you want.
    > On previous versions you may use this:
    >
    > class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
    >
    > ... your methods ...
    >
    > if not hasattr(SimpleXMLRPCServer.SimpleXMLRPCServer, 'shutdown'):
    >
    > # pre 2.6
    > quit = False
    >
    > def serve_forever(self):
    > while not self.quit:
    > self.handle_request()
    >
    > def shutdown(self):
    > self.quit = True
    >
    > def server_bind(self):
    > self.socket.settimeout(1.0)
    > SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)
    >


    Overloading server_bind() with your version solved my problem.

    thanks again

    N
    News123, Feb 7, 2010
    #4
  5. En Sat, 06 Feb 2010 21:24:33 -0300, News123 <> escribió:
    > Gabriel Genellina wrote:
    >> En Fri, 05 Feb 2010 20:03:51 -0300, News123 <> escribió:
    >>
    >>> I'm using an XMLRPC server under Windows.
    >>> What I wonder is how I could create a server, that can be killed with
    >>> CTRL-C

    >>
    >> Python 2.6 and up behaves exactly as you want.
    >> On previous versions you may use this:
    >>
    >> def server_bind(self):
    >> self.socket.settimeout(1.0)
    >> SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)
    >>

    >
    > Overloading server_bind() with your version solved my problem.


    Strange. With Python 2.6.4 I don't need to do that; I'd say the difference
    is in the OS or antivirus (some AV are known to break the TCP stack).

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 7, 2010
    #5
  6. News123

    Aahz Guest

    In article <>,
    Gabriel Genellina <> wrote:
    >
    >Strange. With Python 2.6.4 I don't need to do that; I'd say the difference
    >is in the OS or antivirus (some AV are known to break the TCP stack).


    Perhaps, but I've also found that ctrl-C doesn't work on Windows.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
    Aahz, Feb 12, 2010
    #6
  7. "Aahz" <> wrote in message
    news:hl2ob2$3ib$...
    > In article <>,
    > Gabriel Genellina <> wrote:
    >>
    >>Strange. With Python 2.6.4 I don't need to do that; I'd say the difference
    >>is in the OS or antivirus (some AV are known to break the TCP stack).

    >
    > Perhaps, but I've also found that ctrl-C doesn't work on Windows.


    I don't know if this is exactly the same, but FWIW ...

    I had the following, in a test program -

    from wsgiref.simple_server import make_server

    myserver = MyServer()
    httpd = make_server('', 7789, myserver)
    httpd.serve_forever()

    try:
    while True:
    sleep(1)
    except KeyboardInterrupt:
    httpd.shutdown()

    I could not get it to respond to Ctrl-C.

    Then I read somewhere that it must run in a separate thread, so I changed it
    like this -

    - httpd.serve_forever()
    + threading.Thread(target=httpd.serve_forever).start()

    Now it does respond to Ctrl-C.

    HTH

    Frank Millman
    Frank Millman, Feb 12, 2010
    #7
  8. On 11 Feb 2010 21:18:26 -0800, (Aahz) declaimed the
    following in gmane.comp.python.general:

    > In article <>,
    > Gabriel Genellina <> wrote:
    > >
    > >Strange. With Python 2.6.4 I don't need to do that; I'd say the difference
    > >is in the OS or antivirus (some AV are known to break the TCP stack).

    >
    > Perhaps, but I've also found that ctrl-C doesn't work on Windows.


    Unless the running program makes an I/O call to the console, I don't
    think <ctrl-c> gets past the device driver... <G>
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Feb 12, 2010
    #8
  9. News123

    Aahz Guest

    In article <>,
    Dennis Lee Bieber <> wrote:
    >On 11 Feb 2010 21:18:26 -0800, (Aahz) declaimed the
    >following in gmane.comp.python.general:
    >> In article <>,
    >> Gabriel Genellina <> wrote:
    >>>
    >>>Strange. With Python 2.6.4 I don't need to do that; I'd say the difference
    >>>is in the OS or antivirus (some AV are known to break the TCP stack).

    >>
    >> Perhaps, but I've also found that ctrl-C doesn't work on Windows.

    >
    > Unless the running program makes an I/O call to the console, I don't
    >think <ctrl-c> gets past the device driver... <G>


    That's probably it. It's more annoying for me because I run Windows with
    a VM on a Mac, which doesn't have ctrl-break.
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
    Aahz, Feb 12, 2010
    #9
  10. En Fri, 12 Feb 2010 11:22:40 -0300, Aahz <> escribió:

    > In article <>,
    > Dennis Lee Bieber <> wrote:
    >> On 11 Feb 2010 21:18:26 -0800, (Aahz) declaimed the
    >> following in gmane.comp.python.general:
    >>> In article <>,
    >>> Gabriel Genellina <> wrote:
    >>>>
    >>>> Strange. With Python 2.6.4 I don't need to do that; I'd say the
    >>>> difference
    >>>> is in the OS or antivirus (some AV are known to break the TCP stack).
    >>>
    >>> Perhaps, but I've also found that ctrl-C doesn't work on Windows.

    >>
    >> Unless the running program makes an I/O call to the console, I don't
    >> think <ctrl-c> gets past the device driver... <G>

    >
    > That's probably it. It's more annoying for me because I run Windows with
    > a VM on a Mac, which doesn't have ctrl-break.


    On a "real" PC with Windows XP SP3 and Python 2.6.4, with an idle server,
    just hitting Ctrl-C was enough:

    D:\temp>python -m SimpleXMLRPCServer
    Running XML-RPC server on port 8000
    Traceback (most recent call last):
    File "d:\apps\python26\lib\runpy.py", line 122, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
    File "d:\apps\python26\lib\runpy.py", line 34, in _run_code
    exec code in run_globals
    File "d:\apps\python26\lib\SimpleXMLRPCServer.py", line 615, in <module>
    server.serve_forever()
    File "d:\apps\python26\lib\SocketServer.py", line 224, in serve_forever
    r, w, e = select.select([self], [], [], poll_interval)
    KeyboardInterrupt

    A slightly more realistic example: a busy, single threaded server. I had
    to hit Ctrl-C twice: the first time, KeyboardInterrupt was sent as a Fault
    response to the client.


    import sys

    def busy():
    x = 50000
    y = x**x
    return "Ok"

    if sys.argv[1]=='server':
    import SimpleXMLRPCServer
    server = SimpleXMLRPCServer.SimpleXMLRPCServer(("localhost",
    8000),logRequests=False)
    server.register_function(busy)
    server.serve_forever()
    else:
    import xmlrpclib
    proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
    while True:
    print "client", proxy.busy()


    Maybe the OP's code containes a bare 'except:' clause that swallows
    KeyboardInterrupt, or the server is so busy that is always executing a
    function handler (and all KeyboardInterrupt become Fault and are sent as a
    function response).

    Mmm, perhaps that's a bug, a KeyboardInterrupt should bubble up to the
    server code, not being treated as an error in computing the function
    result.

    --
    Gabriel Genellina
    Gabriel Genellina, Feb 13, 2010
    #10
  11. On Fri, 12 Feb 2010 22:36:42 -0300, "Gabriel Genellina"
    <> declaimed the following in
    gmane.comp.python.general:

    >
    > On a "real" PC with Windows XP SP3 and Python 2.6.4, with an idle server,
    > just hitting Ctrl-C was enough:
    >
    > D:\temp>python -m SimpleXMLRPCServer
    > Running XML-RPC server on port 8000
    > Traceback (most recent call last):
    > File "d:\apps\python26\lib\runpy.py", line 122, in _run_module_as_main
    > "__main__", fname, loader, pkg_name)
    > File "d:\apps\python26\lib\runpy.py", line 34, in _run_code
    > exec code in run_globals
    > File "d:\apps\python26\lib\SimpleXMLRPCServer.py", line 615, in <module>
    > server.serve_forever()
    > File "d:\apps\python26\lib\SocketServer.py", line 224, in serve_forever
    > r, w, e = select.select([self], [], [], poll_interval)
    > KeyboardInterrupt
    >

    Well... Okay, maybe not a full console I/O request but I suspect
    code that didn't use select() might also be non-responsive -- while
    select() only works with sockets on Windows, it may include a check for
    <ctrl-c>... code that doesn't invoke such an event-based wait, though?
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Feb 13, 2010
    #11
  12. News123

    News123 Guest

    Hi Gabriel,




    News123 wrote:
    > Hi Gabriel,
    >
    > Gabriel Genellina wrote:
    >> En Fri, 05 Feb 2010 20:03:51 -0300, News123 <> escribió:
    >>
    >>> I'm using an XMLRPC server under Windows.
    >>>
    >>> What I wonder is how I could create a server, that can be killed with
    >>> CTRL-C
    >>>
    >>> The server aborts easily with CTRL-BREAK but not with CTRL-C (under
    >>> Windows)
    >>>
    >>> If I press CTRL-C it will only abort when the next RPC call occurs.
    >>> It seems it is blocking in the select() call in the handle_request()
    >>> function.

    >> Python 2.6 and up behaves exactly as you want.
    >> On previous versions you may use this:
    >>
    >> class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
    >>
    >> ... your methods ...
    >>
    >> if not hasattr(SimpleXMLRPCServer.SimpleXMLRPCServer, 'shutdown'):
    >>
    >> # pre 2.6
    >> quit = False
    >>
    >> def serve_forever(self):
    >> while not self.quit:
    >> self.handle_request()
    >>
    >> def shutdown(self):
    >> self.quit = True
    >>
    >> def server_bind(self):
    >> self.socket.settimeout(1.0)
    >> SimpleXMLRPCServer.SimpleXMLRPCServer.server_bind(self)
    >>

    >
    > Overloading server_bind() with your version solved my problem.
    >


    Overloading server_bind() makes the server killable with ctrl-C.

    I noticed however, that I can't use this as solution.
    If the bind function sets a time out, then it seems,

    that there are regular small time windows, when connecting clients fail.
    I don't have the error message any more, but will post it when I capture
    the event next time.


    bye

    N
    News123, Feb 13, 2010
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C++
    Replies:
    5
    Views:
    647
    CBFalconer
    Aug 15, 2003
  2. Danny

    Implement Ctrl-C, Ctrl-V

    Danny, Aug 14, 2003, in forum: C Programming
    Replies:
    5
    Views:
    659
    CBFalconer
    Aug 15, 2003
  3. Angus
    Replies:
    5
    Views:
    2,097
    Michael Rauscher
    Nov 18, 2006
  4. Joseph Turian
    Replies:
    0
    Views:
    284
    Joseph Turian
    Jun 29, 2009
  5. Replies:
    1
    Views:
    100
    Roland Koebler
    Feb 22, 2013
Loading...

Share This Page