Client/Server Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • diffuser78@gmail.com

    #1

    Client/Server Question

    My server.py looks like this

    ---------------------------------------------CODE----------------------------------
    #!/usr/bin/env python
    import socket
    import sys
    import os

    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    host = ''
    port = 2000

    s.bind((host,po rt))
    s.listen(1)
    conn, addr = s.accept()
    print 'client is at', addr

    while True:
    data = conn.recv(10000 00)
    if (data == 'MaxSim'):
    print 'MaxiSim'
    os.system('note pad')
    elif (data == 'Driving Sim'):
    print 'Driving Sim'
    os.system('expl orer')
    elif (data == 'SHUTDOWN'):
    print 'Shutting down...'
    os.system('shut down -s')
    conn.close()
    break
    -------------------------------------------CODE
    END-------------------------------------

    I am running this above program on a windows machine. My client is a
    Linux box. What I want to achieve is that server.py should follows
    instructions till I send a 'SHUTDOWN' command upon which it should shut
    down.

    When I run this program and suppose send 'MaxSim' to it, it launches
    notepad.exe fine, but then after that it doesn't accept subsequent
    command. I want is that it should accept subsequent commands like
    Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
    command.

    Any help on this, it will be greatly appreciated.

  • Dennis Benzinger

    #2
    Re: Client/Server Question

    diffuser78@gmai l.com wrote:
    My server.py looks like this
    >
    ---------------------------------------------CODE----------------------------------
    #!/usr/bin/env python
    import socket
    import sys
    import os
    >
    s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
    host = ''
    port = 2000
    >
    s.bind((host,po rt))
    s.listen(1)
    conn, addr = s.accept()
    print 'client is at', addr
    >
    while True:
    data = conn.recv(10000 00)
    if (data == 'MaxSim'):
    print 'MaxiSim'
    os.system('note pad')
    elif (data == 'Driving Sim'):
    print 'Driving Sim'
    os.system('expl orer')
    elif (data == 'SHUTDOWN'):
    print 'Shutting down...'
    os.system('shut down -s')
    conn.close()
    break
    -------------------------------------------CODE
    END-------------------------------------
    >
    I am running this above program on a windows machine. My client is a
    Linux box. What I want to achieve is that server.py should follows
    instructions till I send a 'SHUTDOWN' command upon which it should shut
    down.
    >
    When I run this program and suppose send 'MaxSim' to it, it launches
    notepad.exe fine, but then after that it doesn't accept subsequent
    command. I want is that it should accept subsequent commands like
    Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
    command.
    >
    Any help on this, it will be greatly appreciated.
    >

    os.system() blocks until the called program has finished. Use the
    subprocess module <http://docs.python.org/lib/module-subprocess.html >:

    <untested_cod e>

    import subprocess

    subprocess.Pope n("notepad")

    </untested_code>


    Dennis

    Comment

    • diffuser78@gmail.com

      #3
      Re: Client/Server Question

      Is os.system() going to be deprecated in future ?.....I read somewhere.

      Dennis Benzinger wrote:
      diffuser78@gmai l.com wrote:
      My server.py looks like this

      ---------------------------------------------CODE----------------------------------
      #!/usr/bin/env python
      import socket
      import sys
      import os

      s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      host = ''
      port = 2000

      s.bind((host,po rt))
      s.listen(1)
      conn, addr = s.accept()
      print 'client is at', addr

      while True:
      data = conn.recv(10000 00)
      if (data == 'MaxSim'):
      print 'MaxiSim'
      os.system('note pad')
      elif (data == 'Driving Sim'):
      print 'Driving Sim'
      os.system('expl orer')
      elif (data == 'SHUTDOWN'):
      print 'Shutting down...'
      os.system('shut down -s')
      conn.close()
      break
      -------------------------------------------CODE
      END-------------------------------------

      I am running this above program on a windows machine. My client is a
      Linux box. What I want to achieve is that server.py should follows
      instructions till I send a 'SHUTDOWN' command upon which it should shut
      down.

      When I run this program and suppose send 'MaxSim' to it, it launches
      notepad.exe fine, but then after that it doesn't accept subsequent
      command. I want is that it should accept subsequent commands like
      Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
      command.

      Any help on this, it will be greatly appreciated.
      >
      >
      os.system() blocks until the called program has finished. Use the
      subprocess module <http://docs.python.org/lib/module-subprocess.html >:
      >
      <untested_cod e>
      >
      import subprocess
      >
      subprocess.Pope n("notepad")
      >
      </untested_code>
      >
      >
      Dennis

      Comment

      • faulkner

        #4
        Re: Client/Server Question

        i highly doubt it.



        diffuser78@gmai l.com wrote:
        Is os.system() going to be deprecated in future ?.....I read somewhere.
        >
        Dennis Benzinger wrote:
        diffuser78@gmai l.com wrote:
        My server.py looks like this
        >
        ---------------------------------------------CODE----------------------------------
        #!/usr/bin/env python
        import socket
        import sys
        import os
        >
        s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        host = ''
        port = 2000
        >
        s.bind((host,po rt))
        s.listen(1)
        conn, addr = s.accept()
        print 'client is at', addr
        >
        while True:
        data = conn.recv(10000 00)
        if (data == 'MaxSim'):
        print 'MaxiSim'
        os.system('note pad')
        elif (data == 'Driving Sim'):
        print 'Driving Sim'
        os.system('expl orer')
        elif (data == 'SHUTDOWN'):
        print 'Shutting down...'
        os.system('shut down -s')
        conn.close()
        break
        -------------------------------------------CODE
        END-------------------------------------
        >
        I am running this above program on a windows machine. My client is a
        Linux box. What I want to achieve is that server.py should follows
        instructions till I send a 'SHUTDOWN' command upon which it should shut
        down.
        >
        When I run this program and suppose send 'MaxSim' to it, it launches
        notepad.exe fine, but then after that it doesn't accept subsequent
        command. I want is that it should accept subsequent commands like
        Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
        command.
        >
        Any help on this, it will be greatly appreciated.
        >

        os.system() blocks until the called program has finished. Use the
        subprocess module <http://docs.python.org/lib/module-subprocess.html >:

        <untested_cod e>

        import subprocess

        subprocess.Pope n("notepad")

        </untested_code>


        Dennis

        Comment

        • faulkner

          #5
          Re: Client/Server Question

          you might want to look at sshd. if you're on a windows box, you may
          need cygwin. if you're on linux, you either already have it, or it's in
          your package manager.

          diffuser78@gmai l.com wrote:
          My server.py looks like this
          >
          ---------------------------------------------CODE----------------------------------
          #!/usr/bin/env python
          import socket
          import sys
          import os
          >
          s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
          host = ''
          port = 2000
          >
          s.bind((host,po rt))
          s.listen(1)
          conn, addr = s.accept()
          print 'client is at', addr
          >
          while True:
          data = conn.recv(10000 00)
          if (data == 'MaxSim'):
          print 'MaxiSim'
          os.system('note pad')
          elif (data == 'Driving Sim'):
          print 'Driving Sim'
          os.system('expl orer')
          elif (data == 'SHUTDOWN'):
          print 'Shutting down...'
          os.system('shut down -s')
          conn.close()
          break
          -------------------------------------------CODE
          END-------------------------------------
          >
          I am running this above program on a windows machine. My client is a
          Linux box. What I want to achieve is that server.py should follows
          instructions till I send a 'SHUTDOWN' command upon which it should shut
          down.
          >
          When I run this program and suppose send 'MaxSim' to it, it launches
          notepad.exe fine, but then after that it doesn't accept subsequent
          command. I want is that it should accept subsequent commands like
          Driving Sim and launch windows explorer etc untill I send a 'SHUTDOWN'
          command.
          >
          Any help on this, it will be greatly appreciated.

          Comment

          • Dennis Benzinger

            #6
            Re: Client/Server Question

            diffuser78@gmai l.com wrote:
            Is os.system() going to be deprecated in future ?.....I read somewhere.
            [...]
            Sometime in the future it will. But that won't happen soon. Read the
            second paragraph of "Backwards Compatibility" in the subprocess PEP
            <http://www.python.org/dev/peps/pep-0324/>.


            Dennis

            Comment

            • bryanjugglercryptographer@yahoo.com

              #7
              Re: Client/Server Question


              diffuser78@gmai l.com wrote:
              My server.py looks like this
              >
              ---------------------------------------------CODE----------------------------------
              #!/usr/bin/env python
              import socket
              import sys
              import os
              >
              s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
              host = ''
              port = 2000
              >
              s.bind((host,po rt))
              s.listen(1)
              conn, addr = s.accept()
              print 'client is at', addr
              >
              while True:
              data = conn.recv(10000 00)
              if (data == 'MaxSim'):
              print 'MaxiSim'
              os.system('note pad')
              elif (data == 'Driving Sim'):
              print 'Driving Sim'
              os.system('expl orer')
              elif (data == 'SHUTDOWN'):
              print 'Shutting down...'
              os.system('shut down -s')
              conn.close()
              break
              -------------------------------------------CODE
              END-------------------------------------
              >
              I am running this above program on a windows machine. My client is a
              Linux box. What I want to achieve is that server.py should follows
              instructions till I send a 'SHUTDOWN' command upon which it should shut
              down.
              >
              When I run this program and suppose send 'MaxSim' to it, it launches
              notepad.exe fine, but then after that it doesn't accept subsequent
              command.
              As others noted, that's because os.system() blocks.
              You have more bugs than that.

              The recv() might return "MaxiSimDri ving Sim". It could return
              "MaxiS" on one call, and "im" on the next. If the remote side
              closes the connection, recv() will keep returning the empty
              string, and your program will be stuck in an infinite loop.

              Did you understand Faulkner's suggustion? Anyone who connects to
              TCP port 2000 can invoke "shutdown -s" (which I assume shuts down
              your host).


              --
              --Bryan

              Comment

              • diffuser78@gmail.com

                #8
                Re: Client/Server Question

                The subprocess module didn't work here. I tried using os.popen(), that
                won't help it either.


                What I am trying to achieve is that the server daemon is sitting and
                listening to command from the client. As client send "MaxSim" it
                launches MaxSim.exe and then again waits for subsequent commands.
                Support client sends "DrivingSim " it should launch DrivingSim.exe etc.
                It does till client sends a "SHUTODWN" message upon which it shuts down
                itself.

                Any pointers to achieve this more seamlessly.

                Every help is greatly appreciated.

                Thanks


                Dennis Benzinger wrote:
                os.system() blocks until the called program has finished. Use the
                subprocess module <http://docs.python.org/lib/module-subprocess.html >:
                >
                <untested_cod e>
                >
                import subprocess
                >
                subprocess.Pope n("notepad")
                >
                </untested_code>
                >
                >
                Dennis

                Comment

                • diffuser78@gmail.com

                  #9
                  Re: Client/Server Question

                  The recv() might return "MaxiSimDri ving Sim". It could return
                  "MaxiS" on one call, and "im" on the next. If the remote side
                  closes the connection, recv() will keep returning the empty
                  string, and your program will be stuck in an infinite loop.
                  How to overcome this problem ?
                  Did you understand Faulkner's suggustion? Anyone who connects to
                  TCP port 2000 can invoke "shutdown -s" (which I assume shuts down
                  your host).
                  I did, anyone who connects to port 2000 might be able to send a
                  shutdown and close the computer. I am aware that its not the best way
                  to achieve this but my app runs on a small network without actually
                  connecting to internet.

                  Can you give any tips to overcome the above mentioned problem.
                  --Bryan

                  Comment

                  • diffuser78@gmail.com

                    #10
                    Re: Client/Server Question

                    I apologize.

                    subprocess did work for me like a charm I made a mistake in my code.
                    Appreciate your help.

                    Dennis Benzinger wrote:
                    os.system() blocks until the called program has finished. Use the
                    subprocess module <http://docs.python.org/lib/module-subprocess.html >:
                    >
                    <untested_cod e>
                    >
                    import subprocess
                    >
                    subprocess.Pope n("notepad")
                    >
                    </untested_code>
                    >
                    >
                    Dennis

                    Comment

                    Working...