我有一个扭曲的反应堆在运行,我如何连接到它?

I have a twisted reactor running, how do I connect to it?(我有一个扭曲的反应堆在运行,我如何连接到它?)

本文介绍了我有一个扭曲的反应堆在运行,我如何连接到它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习教程,现在有一个扭曲的反应堆在运行。我已经使用telnet测试了它的功能,但我在扭曲的教程中没有找到任何关于如何连接到反应堆的内容。

我的假设是Twited内部会有某种东西来做这件事,我应该改用内置插座吗?

编辑:

这是服务器脚本:

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游戏。

我希望有一个服务器进程和一个或多个客户端进程。为了便于运行,我目前使用多进程来同时运行它们。完成后,客户端进程需要能够通过网络连接,因为它可能与主机不在同一台计算机上。

推荐答案

下面是一个能够连接到上面的服务器的客户端的小示例。

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()
我尽量保持简单,这样您就可以很容易地理解它,但是要实现Chat部分,我需要从stdin读取代码,而不会阻塞反应器。既然您提到您使用的是图形用户界面而不是终端标准输入/输出,那么这实际上要容易得多--只需choose a reactor compatible with your GUI library,然后使用您的正常的图形用户界面事件。

希望能有所帮助...

这篇关于我有一个扭曲的反应堆在运行,我如何连接到它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:我有一个扭曲的反应堆在运行,我如何连接到它?

基础教程推荐