1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
import win32serviceutil
import win32service
import win32event
from PMIDI import *
from time import sleep
# -*- coding: iso-8859-1 -*-
import threading
import msvcrt
import socket
import sys
import time
import os
import subprocess
import string
global q
# configure the client
global port
global host
global size
global timeout
port = 9003
host = 'localhost'
size = 8192
timeout = 8
def bird():
seq = Sequencer()
song = seq.NewSong()
voice = song.NewVoice(0)
voice.SetInstrumentByName('Bird Tweet')
m = voice.NewMeasure()
m.NewNote(0, 8, 'A', 5)
m.NewNote(8, 8, 'D', 5)
m.NewNote(16, 8, 'A', 5)
seq.Play()
sleep(1.2) # enough seconds to play
seq.Close()
def gun():
seq = Sequencer()
song = seq.NewSong()
voice = song.NewVoice()
voice.SetInstrumentByName('Gunshot')
m = voice.NewMeasure()
m.NewNote(0, 12, 'F', 4)
seq.Play()
sleep(0.8) # enough seconds to play
seq.Close()
def wood():
seq = Sequencer()
song = seq.NewSong()
voice = song.NewVoice()
voice.SetInstrumentByName('Woodblock')
m = voice.NewMeasure()
m.NewNote(0, 24, 'G', 3)
seq.Play()
sleep(1) # enough seconds to play
seq.Close()
def run_prog(program, *args, **kw):
# find executable
mode = kw.get("mode", os.P_WAIT)
for path in string.split(os.environ["PATH"], os.pathsep):
file = os.path.join(path, program) + ".exe"
try:
return os.spawnv(mode, file, (file,) + args)
except os.error:
pass
raise os.error, "cannot find executable"
class SmallestPythonService(win32serviceutil.ServiceFramework):
_svc_name_ = "My service"
_svc_display_name_ = "My service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
# Create an event which we will use to wait on.
# The "service stop" request will set this event.
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.sock.settimeout(timeout)
bird() # bird chirps
sleep(1)
def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
# And set my event.
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.timeout=10*1000 # Attente 10 secondes
while 1:
#---------------------------------------------------------------------
# Wait for service stop signal, if I timeout, loop again
#---------------------------------------------------------------------
rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
#
# Check to see if self.hWaitStop happened
#
if rc == win32event.WAIT_OBJECT_0:
#
# Stop signal encountered
#
break
try:
bird() # bird chirps
sleep(1)
time.sleep(3)
self.sock.sendto('estula', (host, port))
self.response = self.sock.recv(8192)
#if self.response=='ok':
# print self.response
#if self.response<>'ok':
# print u'pas la bonne reponse'
except:
gun() # idiot shoots it
sleep(1)
rep=u'/K C:\\toto\\toto.bat'
#run_prog("cmd", "%-s"%(rep), mode=os.P_NOWAIT)
os.spawnl(os.P_NOWAIT, "C:/toto/toto.bat")
if __name__=='__main__':
win32serviceutil.HandleCommandLine(SmallestPythonService) |
Partager