Receiving commandline input while listening for connections in Python

2024/11/17 12:44:41

I am trying to write a program that has clients connect to it while the server is still able to send commands to all of the clients. I am using the "Twisted" solution. How can I go about this? Here is the code I have so far (I understand that Twisted already uses non-blocking sockets):

import threading
print 'threading.'def dock():try:from twisted.internet.protocol import Factory, Protocolfrom twisted.internet import reactorimport currentTimeprint '[*]Imports succesful.'except:print '[/]Imports failed.'#Define the class for the protocolclass Master(Protocol):command = raw_input('> ')def connectionMade(self):print 'Slave connected.'print currentTime.getTime() #Print current time#self.transport.write("Hello")def connectionLost(self, reason):print 'Lost.'#Assemble it in a "factory"class MasterFactory(Factory):protocol = Masterreactor.listenTCP(8800, MasterFactory())#Run it allreactor.run()def commandline():raw_input('>')threading.Thread(target=dock()).start()
threading.Thread(target=commandline()).start()
Answer

Since you're already using twisted, you should also use it for the console part, instead of using raw_input in a thread.

Twisted's event loop can monitor any file descriptor for changes, including the standard input, so you can get event-based callbacks on a new line entered -- It works asynchronously without need for threads.

I've found this example of a interactive console in a twisted application, maybe you can use it.

https://en.xdnf.cn/q/71552.html

Related Q&A

Passing a parameter through AJAX URL with Django

Below is my code. n logs correctly in the console, and everything works perfectly if I manually enter the value for n into url: {% url "delete_photo" iddy=2%}. Alas, when I try to use n as a …

WARNING: toctree contains reference to nonexisting document error with Sphinx

I used the sphinx-quickstart to set everything up. I used doc/ for the documentation root location. The folder containing my package is setup as: myfolder/doc/mypackage/__init__.pymoprob.py...After the…

Removing nan from list - Python

I am trying to remove nan from a list, but it is refusing to go. I have tried both np.nan and nan.This is my code:ztt = [] for i in z:if i != nan:ztt.append(i) zttor:ztt = [] for i in z:if i != np.nan…

Safely unpacking results of str.split [duplicate]

This question already has answers here:How do I reliably split a string in Python, when it may not contain the pattern, or all n elements?(5 answers)Closed 6 years ago.Ive often been frustrated by the…

Get a structure of HTML code

Im using BeautifulSoup4 and Im curious whether is there a function which returns a structure (ordered tags) of the HTML code. Here is an example:<html> <body> <h1>Simple example</h…

max_help_position is not works in python argparse library

Hi colleagues I have the code (max_help_position is 2000):formatter_class=lambda prog: argparse.HelpFormatter(prog, max_help_position=2000) parser = argparse.ArgumentParser(formatter_class=formatter_cl…

Python - How to parse argv on the command line using stdin/stdout?

Im new to programming. I looked at tutorials for this, but Im just getting more confused. But what Im trying to do is use stdin and stdout to take in data, pass it through arguments and print out outpu…

Bad Request from Yelp API

Inspired by this Yelp tutorial, I created a script to search for all gyms in a given city. I tweaked the script with these updates in order to return ALL gyms, not just the first 20. You can find the g…

Python: test empty set intersection without creation of new set

I often find myself wanting to test the intersection of two sets without using the result of the intersections.set1 = set([1,2]) set2 = set([2,3]) if(set1 & set2):print("Non-empty intersection…

object has no attribute show

I have installed wxpython successfully which i verified by import wxBut when I write a code import wx class gui(wx.Frame):def __init__(self,parent,id):wx.Frame.__init__(self, parent,id,Visualisation fo…