我一直在关注这些教程,现在正在运行一个扭曲的反应堆。我已经使用telnet测试它确实有用,但是我没有在扭曲的教程中找到任何关于如何连接到反应堆的内容。
我的假设是,如果我改为使用内置套接字,那么会有什么内容扭曲吗?
编辑:
这是服务器脚本:
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()
我想让另一个python程序/进程发送并从中接收消息。该项目背后的想法是创建一个多人tic tac toe游戏。
我想要一个服务器进程和一个或多个客户端进程。为了便于运行,我目前正在使用多处理同时运行它们。完成后,客户端进程需要能够通过网络连接,因为它可能与主机不在同一台计算机上。
答案 0 :(得分:3)
以下是一个能够连接到上述服务器的客户端的小例子。
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()
我保持尽可能简单,因此您可以轻松理解它,但要实现聊天部分,我需要代码从stdin
读取而不会阻塞反应堆。既然你提到你使用的是GUI而不是终端标准输入/输出,那么实际上要容易得多 - 只需choose a reactor compatible with your GUI library然后使用普通的GUI事件。
希望有帮助...