客户端正在读/写扭曲的数据?

2024-10-03 04:31:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习twisted,这样我就可以把它与21点游戏相结合。在解决如何将数据从一个客户机传递到服务器,然后再传递给其他客户机时,我试图了解如何操作通常在每个客户机屏幕上打印到终端的字符串:

当我说操纵时,我的意思是将数据类型更改为“data”的list、int、tuple等,打印它的信息(type(data)),并为任何关键字设置条件。在

from twisted.internet import stdio, reactor, protocol
from twisted.protocols import basic
import re

## when client receives data from server // the client script checks then blits ##

class DataForwardingProtocol(protocol.Protocol):
    def __init__(self):
        self.output = None
        self.normalizeNewlines = False

    def dataReceived(self,data):
        if self.normalizeNewlines:

    ## see if data is == to secret ##
    ## this never returns True ? ##
            if data == 'secret':
                print "This line isn't secure"
            else:
                data = re.sub(r"(\r\n|\n)","\r\n",data)
        if self.output:
            if data == "secret":
                print "This line isn't secure"
            else:

         ## this will return the error message below ##
         ## so I'm very unsure of what is going on with 'data' ##
                self.output.write(type(data))

class StdioProxyProtocol(DataForwardingProtocol):
    def connectionMade(self):
        inputForwarder = DataForwardingProtocol()
        inputForwarder.output = self.transport
        inputForwarder.normalizeNewlines = True
        stdioWrapper = stdio.StandardIO(inputForwarder)
        self.output = stdioWrapper

class StdioProxyFactory(protocol.ClientFactory):
    protocol = StdioProxyProtocol

reactor.connectTCP('192.168.1.2', 6000, StdioProxyFactory())
reactor.run()

退货:

^{pr2}$

所以我永远不能'检查'的'数据',当我试图打印出任何关于数据或改变它的类型,我得到一个广泛的错误?还是我错过了什么?如果这有帮助的话,可以使用server脚本


Tags: 数据fromimportselfoutputdata客户机if
1条回答
网友
1楼 · 发布于 2024-10-03 04:31:35

调用type()时,它返回对象的类型,而不是表示对象类型的字符串。在

您可以这样检查类型:

>>> aString = 'abc'
>>> anInt = 123
>>> type(aString) is str
True
>>> type(aString) is int
False
>>> type(anInt) is str
False
>>> type(anInt) is int
True

除非序列化,否则接收到的数据不会是列表、元组或其他类型的对象。查看模块Pickle,查看如何序列化对象的示例!在

相关问题 更多 >