socket and os.system

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mfaujour

    #1

    socket and os.system


    I HAVE THIS PYTHON PROGRAMM:


    [test]$ cat socpb.py
    import BaseHTTPServer, SocketServer, os, socket, threading


    # the server initialisation
    server = SocketServer.TC PServer((socket .gethostname(),
    8088),BaseHTTPS erver.BaseHTTPR equestHandler)
    t=threading.Thr ead(target=serv er.serve_foreve r)
    t.setDaemon(1)
    t.start()


    # the command
    os.system("slee p 3600")
    [test]$






    I LAUNCH IT:


    [test]$ python socpb.py


    [1]+ Stopped python socpb.py
    [test]$ bg
    [1]+ python socpb.py &






    I LOOK THE PORT IT USE --> OK:


    [test]$ netstat -taupe | grep 8088
    tcp 0 0 test:8088 *:* LISTEN mik 32481 14092/python






    I KILL THE PYTHON PROGRAMM (OR IT STOP BY ANOTHER WAY):


    [test]$ kill 14092
    [test]$
    [1]+ Terminated python socpb.py






    I LOOK THE PORT --> NOK, I EXPECTED THAT THE PORT WAS FREED BUT SLEEP
    IS LISTENING AND IF I TRY TO LAUNCH MY PROGRAMM, IT FAIL:


    [test]$ netstat -taupe | grep 8088
    tcp 0 0 test:8088 *:* LISTEN mik 32481 14094/sleep
    [test]$ python socpb.py
    Traceback (most recent call last):
    File "socpb.py", line 4, in ?
    server = SocketServer.TC PServer((socket .gethostname(),
    8088),BaseHTTPS erver.BaseHTTPR equestHandler)
    File "/usr/local/lib/python2.3/SocketServer.py ", line 330, in __init__
    self.server_bin d()
    File "/usr/local/lib/python2.3/SocketServer.py ", line 341, in
    server_bind
    self.socket.bin d(self.server_a ddress)
    File "<string>", line 1, in bind




    socket.error: (98, 'Address already in use')




    IF I KILL THE SLEEP, ALL COME BACK FINE:


    [test]$ kill 14094
    [test]$ netstat -taupe | grep 8088
    [test]$ python socpb.py








    I HAVE SEARCHED ON THE WEB HOW TO LAUNCH THE SLEEP WHITHOUT IT GET THE
    PORT IN THE ENVIRONNEMENT BUT NOTHING...


    I HAVE A WORKAROUND BY USING rsh BUT I AM NOT VERY PLEASED WITH THAT.


    DOES SOMEONE HAS AN IDEA ?











  • Mandus

    #2
    Re: socket and os.system

    Wed, 03 Aug 2005 15:46:50 -0000 skrev mfaujour:[color=blue]
    >
    > I HAVE THIS PYTHON PROGRAMM:
    >[/color]
    [snip]

    welcome to usenet!

    Maybe you get an answer if you doesn't shout that much. Or maybe you
    just have a problem with you Caps Lock?


    --
    Mandus - the only mandus around.

    Comment

    • Peter Hansen

      #3
      Re: socket and os.system

      mfaujour wrote:[color=blue]
      > I HAVE THIS PYTHON PROGRAMM:[/color]
      [snip]
      [color=blue]
      > socket.error: (98, 'Address already in use')
      >
      > DOES SOMEONE HAS AN IDEA ?[/color]

      PLEASE learn to format your questions more appropriately! Your post is
      simply _awful_ to read. At the very least, ALL CAPS is considered to be
      "shouting", though I can see why you had to use them since it would have
      been impossible to see the questions amongst all the code.

      In any case, assuming I've been able to guess at the specific problem
      based on the above lines, which isn't certain, you need to use a line
      something like this in your code to allow your server socket to bind to
      an address that was previously in use:

      server.setsocko pt(socket.SOL_S OCKET, socket.SO_REUSE ADDR, 1)

      For more background, I suggest a Google search on "python so_reuseaddr".

      -Peter

      Comment

      • Donn Cave

        #4
        Re: socket and os.system

        In article <JbqdncA4KumVhG zfRVn-ow@powergate.ca >,
        Peter Hansen <peter@engcorp. com> wrote:
        [color=blue]
        > mfaujour wrote:[color=green]
        > > I HAVE THIS PYTHON PROGRAMM:[/color]
        > [snip]
        >[color=green]
        > > socket.error: (98, 'Address already in use')
        > >
        > > DOES SOMEONE HAS AN IDEA ?[/color]
        >
        > PLEASE learn to format your questions more appropriately! Your post is
        > simply _awful_ to read. At the very least, ALL CAPS is considered to be
        > "shouting", though I can see why you had to use them since it would have
        > been impossible to see the questions amongst all the code.
        >
        > In any case, assuming I've been able to guess at the specific problem
        > based on the above lines, which isn't certain, you need to use a line
        > something like this in your code to allow your server socket to bind to
        > an address that was previously in use:
        >
        > server.setsocko pt(socket.SOL_S OCKET, socket.SO_REUSE ADDR, 1)
        >
        > For more background, I suggest a Google search on "python so_reuseaddr".[/color]

        For heaven's sake, it wasn't that hard to read. Of course
        the upper case text was an unpardonable violation of people's
        tender sensibilities, but in this case it does have the virtue
        of a strong visible distinction between his code and his comments.
        The good thing is that he did provide example that clearly
        illustrates the problem.

        Which is not really that he can't reuse the socket address.
        I mean, it's usually good to take care of that, but ordinarily
        for reasons having to do with shutdown latency. In the present
        case, his application is holding the socket open from a fork
        that inherited it by accident.

        I think the current stock answer is "use the subprocess module."
        If that's not helpful, either because it doesn't provide any
        feature that allows you to close a descriptor in a fork (I seem
        to recall it does), or it isn't supported in your version of
        Python (< 2.4), then you have your choice of two slightly awkward
        solutions:

        1. fcntl F_SETFD FD_CLOEXEC (see man 2 fcntl)
        2. implement your own spawn command (see os.py's
        spawnv()) and close the socket FD in the fork.

        Donn Cave, donn@u.washingt on.edu

        Comment

        • Peter Hansen

          #5
          Re: socket and os.system

          Donn Cave wrote:[color=blue]
          > Peter Hansen <peter@engcorp. com> wrote:[color=green]
          >>PLEASE learn to format your questions more appropriately! Your post is
          >>simply _awful_ to read. ...[/color]
          >
          > For heaven's sake, it wasn't that hard to read.[/color]

          My apologies to the original poster. I did find it very hard to read
          with the very brief time I put into reading posts which don't fit Usenet
          conventions. Upon rereading it now a third time I can parse it a little
          better, and my outburst does seem harsh. (Normally I quickly hit "k" on
          such posts and carry on, but the apparent SO_REUSEADDR symptom caught my
          eye milliseconds before I did so.)
          [color=blue]
          > The good thing is that he did provide example that clearly
          > illustrates the problem.
          >
          > Which is not really that he can't reuse the socket address.[/color]

          So I'm doubly wrong and apologize for not having tried hard enough to
          read it to avoid giving an unhelpful answer.

          -putting-ice-on-his-slapped-wrists-ly y'rs,
          Peter

          Comment

          Working...