本文介绍了使用 python socketserver 如何将变量传递给处理程序类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我想将我的数据库连接传递给 EchoHandler 类,但是我无法弄清楚如何做到这一点或根本无法访问 EchoHandler 类.

类 EchoHandler(SocketServer.StreamRequestHandler):定义句柄(自我):打印 self.client_address, 'connected'如果 __name__ == '__main__':conn = MySQLdb.connect (host = "10.0.0.5", user = "user", passwd = "pass", db = "database")SocketServer.ForkingTCPServer.allow_reuse_address = 1server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)打印服务器在 localhost:4242 上侦听..."尝试:server.allow_reuse_addressserver.serve_forever()除了键盘中断:打印 "\nbailing..."
解决方案

不幸的是,确实没有一种简单的方法可以直接从服务器外部访问处理程序.

您有两个选项可以获取 EchoHandler 实例的信息:

  1. 将连接存储为服务器的属性(在调用 server_forever() 之前添加 server.conn = conn),然后通过 self.server.conn.
  2. 您可以覆盖服务器的 finish_request 并在那里分配值(您必须将其传递给 EchoHandler 的构造函数并覆盖 EchoHandler.__init__).这是一个更麻烦的解决方案,而且它几乎需要您无论如何都将连接存储在服务器上.

我的最佳选择:

class EchoHandler(SocketServer.StreamRequestHandler):定义句柄(自我):# 我不知道你为什么要打印这个,但这是一个例子打印(self.server.conn);打印 self.client_address, 'connected'如果 __name__ == '__main__':SocketServer.ForkingTCPServer.allow_reuse_address = 1server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)server.conn = MySQLdb.connect (host = "10.0.0.5",user = "user", passwd = "pass", db = "database")# 继续正常

I would like to pass my database connection to the EchoHandler class, however I can't figure out how to do that or access the EchoHandler class at all.


class EchoHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        print self.client_address, 'connected'

if __name__ == '__main__':
    conn = MySQLdb.connect (host = "10.0.0.5", user = "user", passwd = "pass", db = "database")

    SocketServer.ForkingTCPServer.allow_reuse_address = 1

    server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)

    print "Server listening on localhost:4242..."
    try:
        server.allow_reuse_address
        server.serve_forever()
    except KeyboardInterrupt:
        print "\nbailing..."
解决方案

Unfortunately, there really isn't an easy way to access the handlers directly from outside the server.

You have two options to get the information to the EchoHandler instances:

  1. Store the connection as a property of the server (add server.conn = conn before calling server_forever()) and then access that property in EchoHandler.handler through self.server.conn.
  2. You can overwrite the server's finish_request and assign the value there (you would have to pass it to the constructor of EchoHandler and overwrite EchoHandler.__init__). That is a far messier solution and it pretty much requires you to store the connection on the server anyway.

My optionon of your best bet:

class EchoHandler(SocketServer.StreamRequestHandler):
    def handle(self):
        # I have no idea why you would print this but this is an example
        print( self.server.conn );
        print self.client_address, 'connected'

if __name__ == '__main__':
    SocketServer.ForkingTCPServer.allow_reuse_address = 1

    server = SocketServer.ForkingTCPServer(('10.0.0.6', 4242), EchoHandler)
    server.conn = MySQLdb.connect (host = "10.0.0.5",
                     user = "user", passwd = "pass", db = "database")
    # continue as normal

这篇关于使用 python socketserver 如何将变量传递给处理程序类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 17:01