[英]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.