我覺得我錯過了這段代碼工作的主要部分,或者真的很奇怪。我已經包含了一個名爲「handle」的方法,該方法被傳遞給thread.start_new_thread作爲參數,但似乎這個方法沒有被識別/執行。即使在PythonWin編輯器中,鍵入「self」時也是如此。它通常會預先填充一個可識別的選項列表,並且該版本中不顯示句柄,但是在該文件的工作版本中,它在編輯器中顯示。PYTHON:能夠從同一個班級中調用某種方法的必要條件是什麼?
下面是代碼:
(大量進口到這裏)
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)
請將您的程序減少到仍然存在錯誤的最小程序,然後將整個程序粘貼到您的問題中。有關更多信息,請參閱http://SSCCE.ORG。 –
「*似乎這種方法沒有被識別/執行*」 - 精確的錯誤信息包括追溯是什麼? –
我沒有收到任何錯誤消息。這個腳本用於安裝一個服務,並且在運行服務時應該執行「main」。服務安裝正確,甚至會運行並寫入日誌文件以及端口843上的LISTENS,直到我嘗試建立連接,此時服務將自動移至STOPPED。我不確定是否有任何方式將它變成錯誤日誌,因爲我對python非常不熟悉,並且不確定從哪裏開始構建這個。句柄方法中的第一行註釋從不打印到日誌文件。 – cuckoo