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

隨筆-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>
            国模精品一区二区三区| 欧美成年人网站| 欧美激情一区二区三区全黄| 久久久人成影片一区二区三区| 久久国产精品久久久久久久久久 | 亚洲高清在线观看| 伊人成年综合电影网| 亚洲激情偷拍| 亚洲女性喷水在线观看一区| 欧美在线不卡| 亚洲成人中文| aa级大片欧美| 欧美中文字幕第一页| 欧美激情一区二区三区蜜桃视频| 欧美日韩在线免费观看| 国产色综合久久| 亚洲精品系列| 欧美亚洲自偷自偷| 欧美大片一区| 亚洲在线视频观看| 免费视频最近日韩| 国产精品推荐精品| 亚洲欧洲一区二区三区| 亚洲在线1234| 欧美激情综合网| 国产欧美一区二区三区久久 | 国产综合香蕉五月婷在线| 久久福利视频导航| 一二三区精品| 久久99在线观看| 欧美人体xx| 极品少妇一区二区三区| 亚洲一区二区动漫| 亚洲高清免费| 久久久777| 国产日韩一区在线| 亚洲视频导航| 亚洲国产精品久久久久久女王| 亚洲嫩草精品久久| 欧美日韩情趣电影| 亚洲精品中文字幕女同| 女主播福利一区| 欧美一区二区三区精品| 国产精品日本| 午夜精品久久久久久久白皮肤 | 亚洲尤物视频在线| 欧美日韩少妇| 日韩一二三在线视频播| 欧美激情导航| 猛男gaygay欧美视频| 好看不卡的中文字幕| 久久av一区二区三区亚洲| 一本色道88久久加勒比精品 | 樱花yy私人影院亚洲| 久久国产精彩视频| 亚洲欧美日韩国产成人| 国产精品欧美激情| 亚洲综合成人在线| 亚洲五月六月| 国产免费观看久久| 久久精品中文字幕免费mv| 欧美在线影院| **性色生活片久久毛片| 欧美a一区二区| 欧美成年视频| 欧美成人精品福利| 亚洲国产精品传媒在线观看| 麻豆精品精华液| 久久尤物视频| 亚洲精品在线二区| 一区二区三区 在线观看视| 欧美精品免费在线| 亚洲字幕在线观看| 亚洲欧美日韩国产精品| 含羞草久久爱69一区| 欧美成人精品h版在线观看| 免费在线国产精品| 99国内精品| 亚洲性夜色噜噜噜7777| 国产综合色精品一区二区三区| 免费观看在线综合色| 欧美激情一区| 欧美在线黄色| 欧美激情视频给我| 国产手机视频一区二区| 久久一日本道色综合久久| 免费一级欧美片在线观看| 一区二区三区 在线观看视| 在线亚洲成人| 亚洲电影视频在线| 亚洲色诱最新| 在线观看日韩欧美| 日韩午夜电影av| 国产主播精品在线| 日韩特黄影片| 18成人免费观看视频| 亚洲图色在线| 亚洲精品久久| 亚久久调教视频| 中文亚洲免费| 男人插女人欧美| 久久狠狠婷婷| 欧美日韩亚洲一区二区三区四区| 久久精品二区亚洲w码| 欧美日韩mp4| 欧美国产亚洲视频| 国产偷国产偷亚洲高清97cao| 亚洲日本一区二区| 国产综合在线视频| 亚洲欧美久久久| 亚洲图片在区色| 欧美mv日韩mv亚洲| 老牛嫩草一区二区三区日本| 国产精品激情电影| 亚洲美女av网站| 亚洲国产一区二区三区高清| 亚洲欧美日本精品| 亚洲自拍三区| 国产精品白丝av嫩草影院| 欧美黑人多人双交| 1769国产精品| 玖玖精品视频| 免费欧美日韩国产三级电影| 国产视频亚洲精品| 亚洲精品一品区二品区三品区| 久久精品国产99国产精品| 性久久久久久久久久久久| 欧美视频不卡| 一区二区三区毛片| 亚洲在线视频一区| 国产精品啊啊啊| 一区二区三区四区蜜桃| 一区二区三区国产在线| 欧美激情一区二区三区 | 香蕉av777xxx色综合一区| 欧美日韩精品高清| 亚洲欧洲视频在线| 在线视频精品一| 欧美日韩亚洲在线| 亚洲视频在线二区| 欧美一区午夜视频在线观看| 国产精品视频一区二区高潮| 亚洲一区欧美一区| 久久精品最新地址| 亚洲成色www久久网站| 男女视频一区二区| 亚洲精品网站在线播放gif| 夜夜爽www精品| 国产精品乱人伦一区二区| 午夜一级久久| 亚洲精品乱码久久久久久久久| 久久在线视频| 亚洲精品美女在线观看| 亚洲性感美女99在线| 国产精品日韩高清| 久久国产黑丝| 91久久久久久国产精品| 亚洲一区二区高清视频| 国产有码一区二区| 欧美成人伊人久久综合网| 一区二区三区黄色| 免费看黄裸体一级大秀欧美| 一本色道精品久久一区二区三区| 国产精品久久久久久妇女6080| 欧美主播一区二区三区| 亚洲欧洲在线播放| 性伦欧美刺激片在线观看| 激情六月婷婷综合| 欧美日韩麻豆| 久久精品国产77777蜜臀| 亚洲黄色成人网| 性欧美长视频| 亚洲日韩欧美一区二区在线| 国产精品二区在线观看| 久久蜜臀精品av| 亚洲视频网站在线观看| 欧美国产国产综合| 欧美一区二区三区免费观看视频| 亚洲高清资源| 国产精品一区二区三区四区| 久久久亚洲国产美女国产盗摄| 日韩网站免费观看| 欧美刺激性大交免费视频| 欧美激情久久久久久| 亚洲欧美激情精品一区二区| 亚洲国产精品va在看黑人| 久久精品综合一区| 亚洲欧美另类国产| 亚洲精品日韩综合观看成人91| 国产精品腿扒开做爽爽爽挤奶网站| 麻豆av一区二区三区久久| 亚洲欧美日本精品| 日韩午夜剧场| 亚洲国产欧美一区二区三区久久| 久久精品72免费观看| 亚洲欧美一区二区精品久久久| 99国内精品| 亚洲精品在线视频观看| 亚洲国产成人tv| 精品成人在线观看|