在Twisted中形成太长的响应(self.transport.write)

时间:2014-02-17 19:49:09

标签: python json python-2.7 twisted

我试图用SSL编写最简单的客户端 - 服务器应用程序,我有一些问题:
1)如何减少JSON序列化的时间? 2)也许存在比LineReciver更好的东西,用于在服务器和客户端之间创建通信?或者我可以增加接收线的长度?

源代码:
a)ServerSLL

import server  
from twisted.internet.protocol import Factory  
from twisted.internet import reactor  
from OpenSSL import SSL   

class ServerSSL(object):
    def getContext(self):
        ctx = SSL.Context(SSL.SSLv23_METHOD)
        ctx.use_certificate_file('server_cert.pem')
        ctx.use_privatekey_file('server_key.pem')
        return ctx


if __name__ == '__main__':
    factory = Factory()
    factory.protocol = server.Server
    reactor.listenSSL(8000, factory, ServerSSL())
    reactor.run()

b)服务器

from json import dumps, loads 
import sqlalchemy
from sqlalchemy.orm import sessionmaker
from db.create_db import Users
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor

engine = sqlalchemy.create_engine('postgresql://user:test@localhost/csan', pool_size=20, max_overflow=0)


class Server(Protocol):
    def __init__(self):
        self.Session = sessionmaker(bind=engine)

    def __del__(self):
        self.session.close()

    def authorization(self, data):
        """
            Checking user with DB
        """
        session = self.Session()
        result = session.execute(sqlalchemy.select([Users]).where(Users.name == data['user']))
        result = result.fetchone()
        if result is None:
            data['error'] = 404
        else:
            if result['name'] == data['user']:
                # correct users info --> real user
                if result['password'] == data['pswd']:
                    data['auth'] = 1
                # incorrect password --> fake user
                else:
                    data['error'] = 403
        session.close()
        return data

    def dataReceived(self, data):
        """
            Processing request from user and send response
        """
        new_data = loads(data)
        if new_data['cmd'] == 'AUTH':
            response = self.authorization(new_data)
        self.transport.write(str(dumps(new_data)))


if __name__ == '__main__':
    f = Factory()
    f.protocol = Server
    reactor.listenTCP(8000, f)
    reactor.run()

c)client_console

from json import dumps, loads
from twisted.internet.protocol import ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import ssl, reactor

class ServerClientSSL(LineReceiver):
    """
        Basic client for talking with server under SSL
    """

    def connectionMade(self):
        """
        Send auth request to serverSLL.py
        """
        login = raw_input('Login:')
        password = raw_input('Password:')
        hash_password = str(hash(password))
        data = dumps({'cmd': 'AUTH', 'user': login, 'pswd': hash_password, 'auth': 0, 'error': 0})
        self.sendLine(str(data))

    def connectionLost(self, reason):
        """
            Says to client, why we are close connection
        """
        print 'connection lost (protocol)'

    def lineReceived(self, data):
        """
            Processing responses from serverSSL.py and send new requests to there
        """
        new_data = loads(data)
        if new_data['cmd'] == 'BBYE':
            self.transport.loseConnection()
        else:
            print new_data


class ServerClientSLLFactory(ClientFactory):
    protocol = ServerClientSSL

    def clientConnectionFailed(self, connector, reason):
        print 'connection failed:', reason.getErrorMessage()
        reactor.stop()

    def clientConnectionLost(self, connector, reason):
        print 'connection lost:', reason.getErrorMessage()
        reactor.stop()

if __name__ == '__main__':
    import sys
    if len(sys.argv) < 3:
        print 'Using: python client_console.py [IP] [PORT] '
    else:
        ip = sys.argv[1]
        port = sys.argv[2]
        factory = ServerClientSLLFactory()
        reactor.connectSSL(ip, int(port), factory, ssl.ClientContextFactory())
        reactor.run()

1 个答案:

答案 0 :(得分:1)

class ServerSSL(object):
    ...

不要编写自己的上下文工厂。请改用twisted.internet.ssl.CertificateOptions。它比你在这里遇到的问题少。

def __del__(self):
    self.session.close()

__del__的第一条规则:不要使用__del__。添加此方法不会为您提供自动会话清理。相反,它几乎肯定会保证您的会话永远不会被清理。协议有一个方法,在它们完成时被调用 - 它被称为connectionLost。请改用它。

result = session.execute(sqlalchemy.select([Users]).where(Users.name == data['user']))
result = result.fetchone()

Twisted是一个单线程多任务系统。这些语句阻止了网络I / O和数据库操作。当他们正在运行你的服务器时没有做任何事情。

使用twisted.enterprise.adbapitwext.enterprise.adbapi2alchimia来异步执行数据库交互。

class ServerClientSSL(LineReceiver):
    ...

有许多协议比LineReceiver更好。您可以做的最简单的改进是切换到Int32StringReceiver。更实质性的改进是切换到twisted.protocols.amp

  

1)如何减少JSON序列化的时间?

使用更快的JSON库。但是,在您的应用程序中修复阻塞数据库代码后,我怀疑您是否仍需要更快的JSON库。

  

2)也许存在比LineReciver更好的东西,用于在服务器和客户端之间创建通信?或者我可以增加接收线的长度?

LineReceiver.MAX_LENGTH。切换到Int32StringReceiverAMP后,您将不再需要此功能。