1

我正在学习扭曲,所以我可以将它与我拥有的二十一点 pygame 集成。在弄清楚如何将数据从一个客户端传递到服务器然后再传递给其他客户端时,我试图了解如何操作通常在本示例中的每个客户端屏幕中打印到终端的字符串:

当我说操作时,我的意思是将数据类型更改为“数据”的列表、整数、元组等,打印它的信息(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()

回报:

Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 84, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/log.py", line 69, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python2.7/dist-packages/twisted/python/context.py", line 81, in callWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 203, in doRead
    return self._dataReceived(data)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 209, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "Downloads/trial.py", line 22, in dataReceived
    self.output.write(type(data))
  File "/usr/lib/python2.7/dist-packages/twisted/internet/_posixstdio.py", line 53, in write
    self._writer.write(data)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/process.py", line 174, in write
    abstract.FileDescriptor.write(self, data)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/abstract.py", line 335, in write
    self._tempDataLen += len(data)
exceptions.TypeError: object of type 'type' has no len()

所以我永远无法“检查”“数据”,当我尝试打印出有关数据的任何内容或更改其类型时,我会得到一个广泛的错误?有什么明显的我遗漏了,或者这是错误的方法吗?如果有帮助,这里是服务器脚本

4

1 回答 1

3

当您调用 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以查看如何序列化对象的示例!

于 2012-09-10T05:29:24.660 回答