sendLine 无法发送整数(Twisted Python)

3 投票
1 回答
637 浏览
提问于 2025-04-17 02:47

我正在用Python和Twisted库写一个MUD(多用户地下城)。现在我想通过LineReceiver模块的sendLine方法发送一个整数。但是每当我尝试发送整数时,程序运行时就会出现下面的错误信息:

Unhandled Error
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 84, in
thLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\log.py", line 69, in
thContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 118
allWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "C:\Python27\lib\site-packages\twisted\python\context.py", line 81,
llWithContext
    return func(*args,**kw)
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\selectreactor.py",
46, in _doReadOrWrite
    why = getattr(selectable, method)()
  File "C:\Python27\lib\site-packages\twisted\internet\tcp.py", line 460,
ead
    rval = self.protocol.dataReceived(data)
  File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 56
dataReceived
    why = self.lineReceived(line)
  File "server.py", line 37, in lineReceived
    self.sendLine(level)
  File "C:\Python27\lib\site-packages\twisted\protocols\basic.py", line 62
sendLine
    return self.transport.write(line + self.delimiter)
exceptions.TypeError: unsupported operand type(s) for +: 'int' and 'str'

导致错误的那一行代码是:

self.sendLine(SomeVarWhichIsANumber)

1 个回答

5

所以... 发送一个字符串:

self.sendLine(str(SomeVarWhichIsANumber))

撰写回答