2013-07-12 170 views
0

我已经建立了一个TCP服务器使用扭曲的例子(有一些修改)。连接到扭曲的TCP服务器

from twisted.internet.protocol import Factory 
from twisted.protocols.basic import LineReceiver 
from twisted.internet import reactor 
from os import path 

import yaml 

class User(LineReceiver): 
    def __init__(self,users): 
     self.users = users 
     self.name = None 

    def connectionMade(self): 
     print 'new connection' 
     self.sendLine('username:') 

    def connectionLost(self,reason): 
     print 'connection lost' 
     if not self.name == None: 
      msg = '%s has disconnected' % (self.name) 
      print msg 
      self.toAll(msg,None) 
      del self.users[self.name] 

    def lineRecieved(self,line): 
     print line 
     if self.name == None: 
      self.setName(line) 
     else: 
      self.toChat(line) 

    def toAll(self,msg,to_self): 
     for name, protocol in self.users.iteritems(): 
      if protocol == self and not to_self == None: 
       self.sendLine(to_self) 
      else: 
       protocol.sendLine(msg) 

    def setName(self,name): 
     if self.users.has_key(name): 
      self.sendLine('username in use') 
      return 
     elif ' ' in name: 
      self.sendLine('no spaces!') 
      return 
     print 'new user %s' % (name) 
     self.sendLine('logged in as %s' % (name)) 
     self.name = name 
     self.users[name] = self 

    def toChat(self,message): 
     msg = '<%s> %s' % (self.name,message) 
     print msg 
     to_self = '<%s (you)> %s' % (self.name,message) 
     self.toAll(msg,to_self) 


class Main(Factory): 
    def __init__(self,motd=None): 
     self.users = {} 
     self.motd = motd 
     print 'loaded, waiting for connections...' 

    def buildProtocol(self,addr): 
     return User(self.users) 

if not path.isfile('config.yml'): 
    open('config.yml','w').write('port: 4444\nmotd: don\'t spam') 

with open('config.yml','r') as f: 
    dump = yaml.load(f.read()) 
    motd = dump['motd'] 
    port = dump['port'] 

reactor.listenTCP(port,Main(motd=motd)) 
reactor.run() 

我想知道如何能够连接到它?我尝试过修改他们的示例Echo客户端和Echo服务器,但是我的服务器在数据发回给它时只发生巨大的错误。

(回声服务器是here和回波客户端是here

我使用的客户端是

from twisted.internet import reactor 
from twisted.internet.protocol import Protocol,ClientFactory 

class Main(Protocol): 
    def dataReceived(self,data): 
     print data 
     self.transport.write(data) 

class MainFactory(ClientFactory): 
    def buildProtocol(self,addr): 
     print 'connected' 
     return Main() 

    def clientConnectionLost(self,connector,reason): 
     print 'connection lost' 

    def clientConnectionFailed(self,connector,reason): 
     print 'connection failed' 

reactor.connectTCP('localhost',4444,MainFactory()) 
reactor.run() 

以下是错误

Image of error

的图片我需要做什么才能将数据发送回服务器?我需要继承什么课程?

+1

拼写数; 'lineRecieved'不会被Twisted的'LineReceiver'代码调用... – abarnert

回答

3

问题是一个简单的错字。

LineReceiver在每行上调用其方法lineReceived。你应该重写这个。但是,您不需要,您可以定义lineRecieved。所以,你会得到默认的实现,这会引发NotImplemented


如果你修复了这个问题,你的代码仍然有点奇怪。通过沟通追踪。

客户端连接,这就要求它做服务器的User.connectionMade,这样的:

self.sendLine('username:') 

所以客户端获得,在Main.dataReceived和做到这一点:

self.transport.write(data) 

所以,它发送的提示回作为回应。

服务器将收到在lineReceived(一旦你确定名称),并做到这一点:

if self.name == None: 
    self.setName(line) 

所以,你要的用户名设置为'username:'

+0

我知道这个奇怪的部分,我只是想让它工作。另外,如果我尝试使用raw_input()获取输入,则不会发生任何事情。该程序似乎完全忽略了我输入的内容,但它确实等着我按下输入 – DoctorSelar