[Twisted-Python] how to get the user's input
hello ,everybody: i am a new to use the twisted. i woul'd like to complete a WEBGAME with it.but i have a problem for the twisted i have complete the server ,and the simple client.but ,i want to the client have more function. one is let the usr input some command to the server but when i write the code ,i found it's hard ,if i want to get the user's input with raw_input() in a while loop,but it will block to wait the user' input what can i do for this .use thread? of twisted have some more power feature for the problem,,thank you every body ps:the problem like a MUD game ,user can send the command to the server and reveive the message from server .and the user can send command everytime
On Tuesday 02 December 2008, lei ren wrote:
if i want to get the user's input with raw_input() in a while loop,but it will block to wait the user' input what can i do for this .use thread? of twisted have some more power feature for the problem,,thank you every body
Maybe you can put sys.stdin in non-blocking mode? I never tried it though. Bye, Maarten
On Tue, 2 Dec 2008 21:38:18 +0800, lei ren
hello ,everybody: i am a new to use the twisted. i woul'd like to complete a WEBGAME with it.but i have a problem for the twisted i have complete the server ,and the simple client.but ,i want to the client have more function. one is let the usr input some command to the server but when i write the code ,i found it's hard ,if i want to get the user's input with raw_input() in a while loop,but it will block to wait the user' input what can i do for this .use thread? of twisted have some more power feature for the problem,,thank you every body
ps:the problem like a MUD game ,user can send the command to the server and reveive the message from server .and the user can send command everytime
The way to read from stdin without blocking the reactor is to use twisted.internet.stdio.StandardIO, a transport which lets a protocol interact with standard in and standard out. Jean-Paul
thanks jean-Pul.i can use commandline to intercat with server by the
StandardIO.
it's so easy with it
and there is a sample in Twisted NetWork Programming Essentials
wow
from twisted.internet import stdio, reactor, protocol
from twisted.protocols import basic
import re
class DataForwardingProtocol(protocol.Protocol):
def _ _init_ _(self):
self.output = None
self.normalizeNewlines = False
def dataReceived(self, data):
if self.normalizeNewlines:
data = re.sub(r"(\r\n|\n)", "\r\n", data)
if self.output:
self.output.write(data)
class StdioProxyProtocol(DataForwardingProtocol):
def connectionMade(self):
inputForwarder = DataForwardingProtocol( )
inputForwarder.output = self.transport
inputForwarder.normalizeNewlines = True
stdioWrapper = stdio.StandardIO(inputForwarder)
self.output = stdioWrapper
print "Connected to server. Press ctrl-C to close connection."
class StdioProxyFactory(protocol.ClientFactory):
protocol = StdioProxyProtocol
def clientConnectionLost(self, transport, reason):
reactor.stop( )
def clientConnectionFailed(self, transport, reason):
print reason.getErrorMessage( )
reactor.stop( )
if __name__ == '_ _main_ _':
import sys
if not len(sys.argv) == 3:
print "Usage: %s host port" % _ _file_ _
sys.exit(1)
reactor.connectTCP(sys.argv[1], int(sys.argv[2]), StdioProxyFactory( ))
reactor.run( )
On Tue, Dec 2, 2008 at 10:11 PM, Jean-Paul Calderone
On Tue, 2 Dec 2008 21:38:18 +0800, lei ren
wrote: hello ,everybody: i am a new to use the twisted. i woul'd like to complete a WEBGAME with it.but i have a problem for the twisted i have complete the server ,and the simple client.but ,i want to the client have more function. one is let the usr input some command to the server but when i write the code ,i found it's hard ,if i want to get the user's input with raw_input() in a while loop,but it will block to wait the user' input what can i do for this .use thread? of twisted have some more power feature for the problem,,thank you every body
ps:the problem like a MUD game ,user can send the command to the server and reveive the message from server .and the user can send command everytime
The way to read from stdin without blocking the reactor is to use twisted.internet.stdio.StandardIO, a transport which lets a protocol interact with standard in and standard out.
Jean-Paul
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (3)
-
Jean-Paul Calderone
-
lei ren
-
Maarten ter Huurne