问题
I've been following the tutorials and now have a twisted reactor running. I've used telnet to test that it does stuff but I've not managed to find anything in the twisted tutorials on how to connect to the reactor.
My assumption was there would be something within twisted to do this, should I instead use the built in socket?
Edit:
This is the Server script:
import time
import multiprocessing
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class TTT(LineReceiver):
def __init__(self, users):
self.users = users
self.name = None
self.state = "GETNAME"
def connectionMade(self):
self.sendLine("You are connected")
def connectionLost(self, reason):
if self.users.has_key(self.name):
del self.users[self.name]
def lineReceived(self, line):
if line == "quit":
reactor.stop()
if self.state == "GETNAME":
self.handle_GETNAME(line)
else:
self.handle_CHAT(line)
def handle_GETNAME(self, name):
if self.users.has_key(name):
self.sendLine("Name taken, please choose another.")
return
self.sendLine("Welcome, %s!" % (name,))
self.name = name
self.users[name] = self
self.state = "CHAT"
def handle_CHAT(self, message):
message = "<%s> %s" % (self.name, message)
for name, protocol in self.users.iteritems():
if protocol != self:
protocol.sendLine(message)
class TTTFactory(Factory):
def __init__(self):
self.state = [0 for x in range(9)]
self.turn = -1
self.users = {} # maps user names to Chat instances
def make_move(self, player, x, y):
if player != self.turn:
return "Not your turn"
i = x + y * 3
if self.state[i] != 0:
return "Invalid move"
self.state[i] = player
# Horrizontal
if self.state[0] == self.state[1] == self.state[2]: return "Win"
if self.state[3] == self.state[4] == self.state[5]: return "Win"
if self.state[6] == self.state[7] == self.state[8]: return "Win"
# Vertical
if self.state[0] == self.state[3] == self.state[6]: return "Win"
if self.state[1] == self.state[4] == self.state[7]: return "Win"
if self.state[2] == self.state[5] == self.state[8]: return "Win"
# Diagonal
if self.state[0] == self.state[4] == self.state[8]: return "Win"
if self.state[6] == self.state[4] == self.state[2]: return "Win"
# Swap turn
self.turn = 0 - self.turn
return "Next move"
def buildProtocol(self, addr):
return TTT(self.users)
# def reactor_runner():
def new_server(conn):
port_num = 8007
conn.send(port_num)
reactor.listenTCP(port_num, TTTFactory())
reactor.run()
I want to have another python program/process send and recieve messages from it. The idea behind the project is create a multiplayer tic tac toe game.
I want to have a server process and 1 or more client processes. For ease of running I'm currently using multiprocessing to run them at the same time. When complete the client process needs to be able to connect over a network as it may not be on the same computer as the host.
回答1:
Here's a small example of a client capable of connecting to your server above.
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
class TTTClientProtocol(LineReceiver):
def lineReceived(self, line):
line = line.strip()
if line == 'You are connected':
self.sendLine(self.factory.username)
else:
print 'SERVER SAYS:', line
class TTTClientFactory(ClientFactory):
protocol = TTTClientProtocol
def __init__(self, name):
self.username = name
name = raw_input('Please enter your name: ')
print 'Connecting...'
reactor.connectTCP('localhost', 8007, TTTClientFactory(name))
reactor.run()
I kept it as simple as I could so you could understand it easily, but to implement the chat part I would need code to read from stdin
without blocking the reactor. Since you mentioned you're using a GUI instead of terminal standard input/output, then that is a lot easier actually - just choose a reactor compatible with your GUI library and then use your normal GUI events.
Hope that helps...
来源:https://stackoverflow.com/questions/8379110/i-have-a-twisted-reactor-running-how-do-i-connect-to-it