简体   繁体   English

PYTHON:能够从同一类中调用方法有什么必要?

[英]PYTHON: What is necessary to be able to call a method from within the same class?

I feel like I am missing a major piece of how this code is working, or something really strange is going on. 我感觉好像缺少了这段代码的主要工作方式,或者正在发生一些非常奇怪的事情。 I have included a method called "handle" which is being passed to thread.start_new_thread as a parameter, however it seems that this method is not being recognized/executed. 我已经包含了一个称为“ handle”的方法,该方法将作为参数传递给thread.start_new_thread,但是似乎无法识别/执行该方法。 Even within the PythonWin editor, when you type "self." 即使在PythonWin编辑器中,当您键入“ self”时。 it will usually prepopulate a list of recognized options, and handle is not showing up in this version, but in a working version of this file it does show up within the editor. 它通常会预先填充可识别选项的列表,并且此版本中不会显示句柄,但是在此文件的有效版本中,它确实会显示在编辑器中。

Here is the code: 这是代码:

(lots of imports go here) (很多进口都在这里)

class AppServerSvc (win32serviceutil.ServiceFramework):
    _svc_name_ = "flaspsrv"
    _svc_display_name_ = "Flash Policy Server"
    _svc_description_ = "Initialize Flash Policy Server"

    def __init__(self,args):
        logging.basicConfig(filename='C:\\Uploads\\testing.log', level=logging.DEBUG, filemode='a', format='%(asctime)s %(levelname)s %(message)s')
        win32serviceutil.ServiceFramework.__init__(self,args)
        self.hWaitStop = win32event.CreateEvent(None,0,0,None)
        socket.setdefaulttimeout(60)

    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                              servicemanager.PYS_SERVICE_STARTED,
                              (self._svc_name_,''))
        self.main()

    def main(self):
        try:
            self.sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
        except AttributeError:
            # AttributeError catches Python built without IPv6
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        except socket.error:
            # socket.error catches OS with IPv6 disabled
            self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
            logging.info('Listening on port 843')
            self.sock.bind(('localhost', 843))
            self.sock.listen(5)

        try:
            while True:
                thread.start_new_thread(self.handle, self.sock.accept())
        except socket.error, e:
            logging.info('Error accepting connection: %s' % (e[1],))

    def handle(self, conn, addr):
        logging.info('Temp2')
        addrstr = '%s:%s' % (addr[0],addr[1])
        try:
            logging.info('Connection from %s' % (addrstr,))
            with contextlib.closing(conn):
                # It's possible that we won't get the entire request in
                # a single recv, but very unlikely.
                request = conn.recv(1024).strip()
                if request != '<policy-file-request/>\0':
                    logging.info('Unrecognized request from %s: %s' % (addrstr, request))
                    return
                logging.info('Valid request received from %s' % (addrstr,))
                fo = file('flashpolicy.xml', 'rb')
                conn.sendall(fo.read(10001))
                logging.info('Sent policy file to %s' % (addrstr,))
        except socket.error, e:
            logging.info('Error handling connection from %s: %s' % (addrstr, e[1]))
        except Exception, e:
            logging.info('Error handling connection from %s: %s' % (addrstr, e[1]))

if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(AppServerSvc)

Change the line 换线

thread.start_new_thread(self.handle, self.sock.accept())

to

thread.start_new_thread(self.handle, *self.sock.accept())

From the documentation of socket.accept() , this socket method returns a tuple (conn, address) , where as your handler accepts three parameters (including a reference to self). socket.accept()的文档中可以看到 ,此socket方法返回一个元组(conn, address) ,在其中您的处理程序将接受三个参数(包括对self的引用)。

The second and third parameter of thread.start_new_thread is the args (positional arguments) and kwargs (key word arguments) that are supposed to be passed to the callback registered with the threading module. thread.start_new_thread的第二个和第三个参数是应该传递给在线程模块中注册的回调的args (位置参数)和kwargs (关键字参数)。 So unless you unpack the arguments from socket.accept() , you would likely be receiving a Type Error . 因此,除非您从socket.accept()解压缩参数, socket.accept()很可能会收到Type Error

So you need to unpack the return tuple from self.socket before passing as an argument to handle. 因此,您需要self.socket的返回元组解包,然后再将其作为参数传递给handle。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从python中相同的重载派生类方法中调用基类方法? - How do I call a base class method from within the same overloaded derived class method in python? Python:从“ DIFFERENT类”的方法中调用类方法 - Python: Call a class method from within a 'DIFFERENT class''s method Python:如何从同一类的类方法调用实例方法 - Python: How to call an instance method from a class method of the same class 如何从python中的类中的方法调用该类以重新定义变量? - how to call a the class from a method within the class in python to redefine a variable? 在同一类中的方法中调用变量 - call variable in method within the same class 在 Python 中的同一类中从另一个方法调用一个方法 - Calling one method from another within same class in Python 如何在 python 3.x 中的同一 class 的命名空间内调用 class 方法 - How to call class method within namespace of the same class in python 3.x 如何从同一 class 中的静态方法访问 class 中的 python 方法 - how to access a python method within a class from a staticmethod within the same class 类方法不能调用同一类中的其他类方法 - Class method can not call other class methods within the same class 在同一个 class 中调用方法及其依赖项,得到 Python 中的期望值 - Call a method and its dependencies within the same class to get the expected value in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM