client server question

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

    #1

    client server question


    I have a simple script that runs a server where one client can connect.
    I would like to make it so that many clients can connect to one server
    on the same port. Where can I find how to do this?

    Thanks,
    --j

  • Peter Hansen

    #2
    Re: client server question

    John wrote:[color=blue]
    > I have a simple script that runs a server where one client can connect.
    > I would like to make it so that many clients can connect to one server
    > on the same port. Where can I find how to do this?[/color]

    Start by reading http://www.catb.org/~esr/faqs/smart-questions.html to
    learn how to ask better questions.

    Then consider that "script that runs a server" could mean either you've
    written a Python "script" that runs *as* a server, but has the
    limitation of only one client connection at a time, or it could mean
    that your script simply runs (via, for example, a call to os.system())
    an external server program which has this limitation.

    Given the ambiguity of your request, it's impossible to give a useful
    answer without lots of guessing. Please clear up the above question and
    provide much more detail and you'll likely get a more directly useful
    response.

    -Peter

    Comment

    • Chris Curvey

      #3
      Re: client server question

      import threading
      import logging

      ############### ############### ############### ############### ##########
      class Reader(threadin g.Thread):
      def __init__(self, clientsock):
      threading.Threa d.__init__(self )
      self.logger = logging.getLogg er("Reader")

      #-----------------------------------------------------------------
      def run(self):
      self.logger.inf o("New child %s" %
      (threading.curr entThread().get Name()))
      self.logger.inf o("Got connection from %s" %
      (clientsock.get peername()))

      ############### ############### ############### ############### ########
      # set up a socket to listen for incoming connections from our clients
      s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
      s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
      s.bind((host, port))
      s.listen(1)

      while True:
      try:
      clientsock, clientaddr = s.accept()
      except KeyboardInterru pt:
      raise
      except:
      traceback.print _exc()
      continue

      client = Reader(clientso ck)
      client.setDaemo n(1)
      client.start()

      Comment

      • John

        #4
        Re: client server question

        Thanks a lot,
        I think I could modify this to get my work done.
        --j

        Chris Curvey wrote:[color=blue]
        > import threading
        > import logging
        >
        > ############### ############### ############### ############### ##########
        > class Reader(threadin g.Thread):
        > def __init__(self, clientsock):
        > threading.Threa d.__init__(self )
        > self.logger = logging.getLogg er("Reader")
        >
        > #-----------------------------------------------------------------
        > def run(self):
        > self.logger.inf o("New child %s" %
        > (threading.curr entThread().get Name()))
        > self.logger.inf o("Got connection from %s" %
        > (clientsock.get peername()))
        >
        > ############### ############### ############### ############### ########
        > # set up a socket to listen for incoming connections from our clients
        > s = socket.socket(s ocket.AF_INET, socket.SOCK_STR EAM)
        > s.setsockopt(so cket.SOL_SOCKET , socket.SO_REUSE ADDR, 1)
        > s.bind((host, port))
        > s.listen(1)
        >
        > while True:
        > try:
        > clientsock, clientaddr = s.accept()
        > except KeyboardInterru pt:
        > raise
        > except:
        > traceback.print _exc()
        > continue
        >
        > client = Reader(clientso ck)
        > client.setDaemo n(1)
        > client.start()[/color]

        Comment

        • Robert Wierschke

          #5
          Re: client server question

          John schrieb:[color=blue]
          > I have a simple script that runs a server where one client can connect.
          > I would like to make it so that many clients can connect to one server
          > on the same port. Where can I find how to do this?
          >
          > Thanks,
          > --j
          >[/color]

          use sockets.

          the socket accept mehtoh returns a new socket (so now you hava one
          "server socket" and a connection between to "client sockets" one used by
          the requesting client and one created at the server side (retrund from
          accept) ). you should start a new thread/process with this new socket
          to handle the client request and use the "old" server socket again with
          the accept method to wait for the next client to connect.

          Comment

          • Irmen de Jong

            #6
            Re: client server question

            Robert Wierschke wrote:[color=blue]
            > John schrieb:
            >[color=green]
            >> I have a simple script that runs a server where one client can connect.
            >> I would like to make it so that many clients can connect to one server
            >> on the same port. Where can I find how to do this?
            >>
            >> Thanks,
            >> --j
            >>[/color]
            >
            > use sockets.[/color]

            Or, if you have no interest at all in the gory details and problems
            of socket programming, have a look at Pyro (http://pyro.sourceforge.net).
            Pyro lets you invoke remote python objects as if they were just regular
            python objects.

            --Irmen

            Comment

            Working...