Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8523219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:20:50+00:00 2026-06-11T07:20:50+00:00

Good evening, This is my 1st time on this site, I have been programming

  • 0

Good evening, This is my 1st time on this site, I have been programming a python based user monitoring system for my work for the past 3 months and I am almost done with my 1st release. However I have run into a problem controlling what computer I want to connect to.

If i run the two sample code I put in this post I can receive the client and send commands to client with the server, but only one client at a time, and the server is dictating which client I can send to and which one is next. I am certain the problem is “server side but I am not sure how to fix the problem and a Google search does not turn up anyone having tried this.

I have attached both client and server base networking code in this post.

client:

import asyncore
import socket
import sys
do_restart = False
class client(asyncore.dispatcher):
    def __init__(self, host, port=8000):
        serv = open("srv.conf","r")
        host = serv.read()
        serv.close()
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))
    def writable(self):
        return 0
    def handle_connect(self):
        pass           
    def handle_read(self):
        data = self.recv(4096)
        #Rest of code goes here 
serv = open("srv.conf","r")
host = serv.read()
serv.close()
request = client(host)
asyncore.loop()

server:

import asyncore
import socket
import sys
class  soc(asyncore.dispatcher):
    def __init__(self, port=8000):
        asyncore.dispatcher.__init__(self)   
        self.port = port
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(('', port))
        self.listen(5)
    def handle_accept(self):
        channel, addr = self.accept()
        while 1:
            j = raw_input(addr)
            #Rest of my code is here
server = soc(8000)
asyncore.loop()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:20:52+00:00Added an answer on June 11, 2026 at 7:20 am

    Here is a fast and dirty idea that I threw together.

    The use of raw_input has been replaced with another dispatcher that is asyncore compatable, referencing this other question here

    And I am expanding on the answer given by @user1320237 to defer each new connection to a new dispatcher.

    You wanted to have a single command line interface that can send control commands to any of the connected clients. That means you need a way to switch between them. What I have done is created a dict to keep track of the connected clients. Then we also create a set of available commands that map to callbacks for your command line.

    This example has the following:

    • list: list current clients
    • set <client>: set current client
    • send <msg>: send a msg to the current client

    server.py

    import asyncore
    import socket
    import sys
    from weakref import WeakValueDictionary
    
    
    class Soc(asyncore.dispatcher):
    
        CMDS = {
            'list': 'cmd_list',
            'set': 'cmd_set_addr',
            'send': 'cmd_send',
        }
    
        def __init__(self, port=8000):
            asyncore.dispatcher.__init__(self)  
    
            self._conns = WeakValueDictionary()
            self._current = tuple()
    
            self.port = port
            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
            self.set_reuse_addr()
            self.bind(('', port))
            self.listen(5)
    
            self.cmdline = Cmdline(self.handle_input, sys.stdin)
            self.cmdline.prompt()
    
    
        def writable(self):
            return False
    
        def handle_input(self, i):
            tokens = i.strip().split(None, 1)
            cmd = tokens[0]
            arg = ""
            if len(tokens) > 1:
                arg = tokens[1]
    
            cbk = self.CMDS.get(cmd)
            if cbk:
                getattr(self, cbk)(arg)
    
            self.cmdline.prompt(self._addr_to_key(self._current))
    
        def handle_accept(self):
            channel, addr = self.accept()
            c = Conn(channel)
            self._conns[self._addr_to_key(addr)] = c
    
        def _addr_to_key(self, addr):
            return ':'.join(str(i) for i in addr)
    
        def cmd_list(self, *args):
            avail = '\n'.join(self._conns.iterkeys())
            print "\n%s\n" % avail
    
        def cmd_set_addr(self, addr_str):
            conn = self._conns.get(addr_str)
            if conn:
                self._current = conn.addr
    
        def cmd_send(self, msg):
            if self._current:
                addr_str = self._addr_to_key(self._current)
                conn = self._conns.get(addr_str)
                if conn:
                    conn.buffer += msg         
    
    
    class Cmdline(asyncore.file_dispatcher):
        def __init__(self, cbk, f):
            asyncore.file_dispatcher.__init__(self, f)
            self.cbk = cbk
    
        def prompt(self, msg=''):
            sys.stdout.write('%s > ' % msg)
            sys.stdout.flush()        
    
        def handle_read(self):
            self.cbk(self.recv(1024))
    
    
    class Conn(asyncore.dispatcher):
    
        def __init__(self, *args, **kwargs):
            asyncore.dispatcher.__init__(self, *args, **kwargs)  
            self.buffer = ""
    
        def writable(self):
            return len(self.buffer) > 0
    
        def handle_write(self):
            self.send(self.buffer)
            self.buffer = ''
    
        def handle_read(self):
            data = self.recv(4096)
            print self.addr, '-', data
    
    
    server = Soc(8000)
    asyncore.loop()
    

    Your main server is now never blocking on stdin, and always accepting new connections. The only work it does is the command handling which should either be a fast operation, or signals the connection objects to handle the message.

    Usage:

    # start the server
    # start 2 clients
    > 
    > list
    
    127.0.0.1:51738
    127.0.0.1:51736
    
    > set 127.0.0.1:51736
    127.0.0.1:51736 >
    127.0.0.1:51736 > send foo
    
    # client 127.0.0.1:51736 receives "foo"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good evening, I have been trying to wrap my head about this, below, code
Good evening. For a project, I have to create a system. In this system,
Good evening, I am tired and have been fighting with this for hours. I
Good evening StackOverflow This time I'm fighting with a ListView Containing TextViews. I add
Good evening. I have a problem. i am using has_secure_password and cause of this
Good evening :-)! I have this code to use Drag & Drop method for
Good Evening, I have been trying to implement a simple Android(tm) application that utilizes
Good evening, I'm having an issue with Masonry. This is all my relevant code:
Good evening, I would like to have a navigation bar which is centralised to
Good evening, In my app that I'm currently developing, I have a class that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.