青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

隨筆-167  評論-8  文章-0  trackbacks-0
We had project last friday to write a windows service that would poll an Exchange mail box and write to a database. It was executed in C#. Well... i got thinking... hmmm... can't python be used to do this? Because if it can, then that project would not last more than a couple of hours (actualy, the C# project shouldn't also last more than a couple of hours if you know what you'r doing, but hey.. i have to have something to say about why i decided to do it in Python write? i mean.... I LOVE Python may not cut it for some folks :) )

OK... lets get to the meat of it.

I first hit google, looking for links to python and windows services. I then found some mailing list archives and in one of them, there was a reference to a chapter on Windows Services in the book, Programming Python on Win32.

Well... i had access to the book, and went headlong. Long and short of it, 15 mins of perusing latter, I had a windows service running. Lets attempt to get this stuff to work together now.


First there are two very important modules that helps our lives out. win32service and win32serviceutil

We're going to write a service that just writes to the event log every 10 seconds. U'll have to stop this service soon or else, you'll run out of EvenLog space ;)

First some important pre-parations and background on python-windows services. There is a program called pythonservice.exe, that actually handles everything that concerns windows services written in Python. I'm walking a thin rope here with this explanation now, but this is how i understand it, just don't quote me in a conference where there are other Pythonistas and Lords Of the Eggs, I'll deny it vehemently ;)

The way i figure it, pythonservice.exe is to python services what the usual python.exe is for all other normal python scripts. Normally on windows, once .py is associated with python.exe, each time you run a script, the python intepreter, python.exe is called to 'intepret' it. For services, apparently some funky stuff goes on, so instead of calling python.exe as the intepreter, pythonservice.exe is called as the intepreter (well, not exactly intepreter i guess, but it is the process that runs the services.) You can also look at it like this: You say service start, windows identifies it as a python service, and starts pythonservice.exe passing it parameters to find the service itself. Pythonservice.exe locates the service, and starts it. As far as windows is concerned, it is running a process called PythonService.exe (You'll see that without special tricks, when writting to eventlog, PythonService is the one that does the writing)

Now the preceeding means that windows has to know to associate python services with pythonservice.exe . This is essentially called registeration. So pythonservice.exe must be registered with windows to handle python windows services. to do that, locate where the python win32 extensions are on your box. They'll probably be in your site-packages folder. Look for the win32\ subdirectory, and you'll locate pythonservice.exe sitting somewhere there:

Mine is at C:\Python24\Lib\site-packages\win32\pythonservice.exe

you can change to that directory:

C:\Python24\Lib\site-packages\win32

and then do: pythonservice.exe /register

You should see a message about registering the Python Service Manager.

After that, if there are no errors, we're ready to plunge.


First, we need to import the two all important modules.

For those who don't understand Python... the lines beginning with #, are just comments.




import win32service
import win32serviceutil
import time

#at this point. We're ready to go.
#Put simply, a python windows service inherits from win32serviceutils.ServiceFramework
#simply extending that class, sets up all you ever need to do.

class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "aservice"
_svc_display_name_ = "aservice - It Does nothing"

def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
#at this point service is created pefectly.
#you could stop here and jump to setting up the '__main__' section,
#but you wont be able to stop your service, and it won't do anything.
#at the very least, you need to implement SvcDoRun() and better still SvcStop():
#This next attribute is used when its stopping time.
self.isAlive = True

def SvcDoRun(self):
import servicemanager
while self.isAlive:
#remember when i said you needed only two modules?
#well... i think i lied. If you're going to do anything
#usefull, you're going to obviously need more modules.
#This next module servicemanager, has some funny
#properties that makes it only to be visible when
#the service is properly setup. This means it can't be imported
#in normal python programs, and can't even be imported
#in the Global Namespace, but only in local functions that
#will be called after the service is setup. Anyway,
#this module contains some utilities for writing to EventLog.

servicemanager.LogInfoMsg("aservice - is alive and well")
time.sleep(10)
servicemanager.LogInfoMsg("aservice - Stopped")

def SvcStop(self):
#before you stop, you'll want to inform windows that
#you've recieved a stop signal, and you're trying to stop.
#in the windows Service manager, this is what shows the status message as
#'stopping'. This is important, since SvcDoRun() may take sometime before it stops.
import servicemanager
servicemanager.LogInfoMsg("aservice - Recieved stop signal")
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
self.isAlive = False #this will make SvcDoRun() break the while loop at the next iteration.

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(aservice) #this line sets it all up to run properly.



Uhh... just incase you haven't guessed. That is all for the service.
The next part is installing and starting it. You can save this as aservice.py

cd to the directory where it is saved and do:

aservice.py install

Note that 'aservice.py remove' will remove the service

you can start and stop with Windows Service manager or:

aservice.py start

and

aservice.py stop


OK... that's it... play around, flesh out... anything.

You may or may not have figured out that the entire functionality of the serivice gets started from SvcDoRun()


That's all folks... i hope this is usefull :)
Hey... i just had a brainwave. I'll repeat the code here without any comments :)

 1 import win32service
 2 import win32serviceutil
 3 import time
 4 
 5 class aservice(win32serviceutil.ServiceFramework):
 6     _svc_name_ = "aservice"
 7     _svc_display_name_ = "aservice - It Does nothing"
 8 
 9     def __init__(self,args):
10         win32serviceutil.ServiceFramework.__init__(self,args)
11         self.isAlive = True
12 
13     def SvcDoRun(self):
14         import servicemanager
15 
16         while self.isAlive:
17             servicemanager.LogInfoMsg("aservice - is alive and well")
18             time.sleep(10)
19             servicemanager.LogInfoMsg("aservice - Stopped")
20 
21     def SvcStop(self):
22         import servicemanager
23 
24         servicemanager.LogInfoMsg("aservice - Recieved stop signal")
25         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
26         self.isAlive = False
27 
28 if __name__ == '__main__':
29     win32serviceutil.HandleCommandLine(aservice)
30 



Note that if you're going to copy and paste this stuff, you may have some white space issues, since i'm just typing straight and editing here.

posted on 2009-08-01 14:42 老馬驛站 閱讀(853) 評論(0)  編輯 收藏 引用 所屬分類: python
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            国产精品久久久久久久久免费桃花| 亚洲欧美999| 欧美成人国产| 亚洲精品久久久久久久久久久久久| 久久一区中文字幕| 久久九九精品| 亚洲国产精品免费| 亚洲乱亚洲高清| 国产精品日韩欧美一区二区| 欧美一区1区三区3区公司| 久久丁香综合五月国产三级网站| 亚洲福利免费| 日韩一区二区精品视频| 国产欧美日韩高清| 免费视频一区二区三区在线观看| 欧美韩日精品| 欧美一区日本一区韩国一区| 久久精品视频免费观看| 亚洲精品一二三| 亚洲午夜av| 亚洲第一精品在线| 亚洲一二三四久久| 亚洲第一狼人社区| 亚洲一区二区三区成人在线视频精品 | 亚洲动漫精品| 日韩亚洲成人av在线| 国产一区二区三区在线免费观看| 欧美激情一二三区| 国产精品一区久久久久| 亚洲成人在线视频播放| 久久精品国产免费观看| 免费不卡在线视频| 欧美亚洲尤物久久| 欧美搞黄网站| 久久综合色婷婷| 国产精品地址| 亚洲国产精品一区制服丝袜 | 牛牛影视久久网| 亚洲午夜精品网| 中文亚洲欧美| 日韩一本二本av| 久久精品国产亚洲一区二区| 久久在线视频| 欧美在线播放一区| 欧美日本亚洲视频| 免费中文日韩| 国产亚洲精品久| 亚洲免费一在线| 一区二区三区你懂的| 久久亚洲欧美| 久久亚洲风情| 欧美freesex交免费视频| 国产精品久久久久久久午夜片| 亚洲大片在线| 黄色日韩网站视频| 亚洲午夜精品福利| 久久全球大尺度高清视频| 经典三级久久| 欧美在线观看一区二区| 亚洲欧美日韩另类精品一区二区三区| 欧美精品v日韩精品v韩国精品v | 亚洲一区免费| 欧美日韩激情网| 亚洲裸体俱乐部裸体舞表演av| 亚洲日韩视频| 欧美精品日韩www.p站| 亚洲激情六月丁香| 久久亚洲欧美| 欧美v日韩v国产v| 在线免费观看成人网| 久久久久国产一区二区| 久久久之久亚州精品露出| 国产欧美一区二区三区视频| 亚洲午夜精品一区二区三区他趣| 亚洲欧美美女| 国产精品免费观看在线| 午夜精品久久久久99热蜜桃导演| 欧美亚洲综合网| 国产性天天综合网| 欧美一区二区精美| 欧美一区二区三区在线观看视频| 国产午夜精品一区二区三区欧美 | 久久亚洲精品一区| 亚洲国产99精品国自产| 欧美国产大片| 亚洲午夜精品久久久久久app| 欧美在线播放| 在线精品观看| 欧美日本一道本| 午夜精品久久久久久久蜜桃app| 久久久久成人精品免费播放动漫| 亚洲国产天堂久久国产91| 免费观看成人鲁鲁鲁鲁鲁视频| 亚洲国产日日夜夜| 欧美婷婷久久| 欧美在线关看| 亚洲精品久久久久| 欧美一区二区成人| 亚洲人成艺术| 国产精品日韩在线观看| 老鸭窝91久久精品色噜噜导演| 亚洲精品自在久久| 老司机久久99久久精品播放免费| 日韩午夜av| 国内外成人免费激情在线视频| 牛牛精品成人免费视频| 香港成人在线视频| 亚洲日本激情| 久久婷婷影院| 亚洲五月六月| 亚洲国产欧美一区二区三区同亚洲 | 亚洲三级影院| 久久精品综合网| 一本色道久久综合亚洲精品按摩| 久久精品国产精品亚洲| 国产欧美一区二区精品婷婷| 欧美激情一区二区三区蜜桃视频 | 日韩一区二区精品| 欧美aa国产视频| 欧美一区二区网站| 一区二区精品在线| 在线不卡亚洲| 国产欧美一区二区三区国产幕精品 | 欧美刺激午夜性久久久久久久| 亚洲激情女人| 一区视频在线播放| 国产女主播一区二区三区| 欧美日韩免费观看一区| 欧美激情一区二区三区不卡| 久久久久成人精品| 亚洲毛片在线| 亚洲精品久久| 亚洲国产视频一区二区| 欧美国产精品日韩| 欧美国产日韩一区二区| 麻豆91精品| 性色av一区二区三区红粉影视| 中文精品视频| 中国女人久久久| 99精品视频一区| 一本色道久久综合亚洲二区三区| 91久久精品国产91性色| 亚洲国产一区二区视频| 亚洲国产精品一区二区www在线| 国产一区在线观看视频| 黄色小说综合网站| 激情国产一区| 黄色精品一区| 黑人一区二区三区四区五区| 国产在线乱码一区二区三区| 精品1区2区| 在线观看日韩精品| 亚洲激情成人网| 亚洲国内自拍| 亚洲高清久久网| 99精品99| 欧美一区二区三区在线免费观看| 久久精品亚洲一区| 欧美福利网址| 亚洲精品综合在线| 亚洲欧美成人| 久久久久综合一区二区三区| 欧美aa国产视频| 欧美日韩精品伦理作品在线免费观看| 欧美视频免费| 国产日韩欧美一区| 亚洲福利视频二区| 亚洲一区二区三区四区五区黄| 欧美一区午夜精品| 另类成人小视频在线| 日韩视频在线播放| 欧美在线观看网址综合| 欧美激情第六页| 国产三级精品在线不卡| 亚洲第一页自拍| 一区二区三区精品| 久久成人一区| 欧美福利影院| 亚洲一区免费视频| 久久中文在线| 欧美视频四区| 一区二区三区在线免费观看| 在线视频亚洲一区| 久久成人在线| 日韩一区二区精品| 久久午夜激情| 欧美香蕉大胸在线视频观看| 国内精品久久久久伊人av| 一本大道久久a久久精二百| 久久精品国产亚洲精品| 亚洲精品老司机| 欧美淫片网站| 欧美日韩国产一中文字不卡| 在线观看欧美亚洲| 性做久久久久久久免费看| 亚洲人成人99网站| 久久亚洲高清| 国产一区二区三区不卡在线观看 | 亚洲天堂av图片| 美女脱光内衣内裤视频久久影院|