2011-09-21 29 views
3

我正在使用Twisted库在Python中编写MUD。我目前正在尝试通过sendLine方法将整数发送到LineReceiver模块。然而,每当我试图发送一个整数,我得到了以下错误消息,运行我的程序时:sendLine不会发送整数(扭曲的Python)

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) 
+0

当然,它不发送整数。它发送...行... – Glyph

回答

5

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

self.sendLine(str(SomeVarWhichIsANumber)) 
+0

我试过这个,它没有工作。但是,在你提出这个建议之后,我第二次尝试了它,并且出于某种奇怪的原因它正在工作。 –