Sunday, October 7, 2007

Boo - Asynchronous call

import System
import System.Collections
import System.Threading
import System.Net

def callback(result as IAsyncResult):
    print("callback")
    
def run():
    print "Executing"
    
print("started")

result = run.BeginInvoke(callback, null)
System.Threading.Thread.Sleep(50ms)
run.EndInvoke(result)

print ("done")

request = WebRequest.Create("http://www.python.org/")

result = request.GetResponse.BeginInvoke(null, null)
while not result.IsCompleted:
    Console.Write("!")
    Thread.Sleep(25ms)
Console.WriteLine()

// ready to get the response
response = request.GetResponse.EndInvoke(result)

Console.WriteLine("${response.ContentLength} bytes.")

print "Press any key to continue . . . "
Console.ReadKey(true)

Boo and WMI

namespace wmi

import System
import System.Management
import System.Collections

query =  """SELECT * FROM Win32_LogicalDisk  
            where drivetype=3"""

searcher = ManagementObjectSearcher("root\\CIMV2",query)
results = searcher.Get()

for b as ManagementObject in results:
    print b.Properties["DeviceID"].Value,b.Properties["Size"].Value

print "Press any key to continue . . . "
Console.ReadKey(true)

Thursday, September 13, 2007

wmic : un secret bien gardé

Cela fait maintenant un petit moment que travaille sous Windows, et je viens à peine
de découvrir cet outil surpuissant.Je vous engage à vous y intéresser.

Voici par exemple comment ouvrir une console 'cmd' sur un hôte distant

C:\Documents and Settings\artyprog>wmic /node:192.168.12.1 process call create 'cmd.exe'

Je vous laisse découvrir le reste :-)


Saturday, July 28, 2007

Python - HTA : Capturer la position de la Souris

Cet exemple montre comment capturer la position de la souris en Python

Article de référence :
http://msdn2.microsoft.com/en-us/library/ms531073.aspx

<HTML>
<HEAD><TITLE>Report mouse moves in Python</TITLE>

<BODY onmousemove="reportMove()">
<H1>Python catch the Mouse</H1>

<div id="pos"></div>

<SCRIPT LANGUAGE="Python">
d = document.all
def reportMove():
x = window.event.x
y = window.event.y
d.pos.innerHTML = "posx = %d, posy =%d"%(x, y)
</SCRIPT>

</BODY>
</HTML>

Python - HTA : Simple Wizard


Voici comment afficher des pages contenues dans de simples fichiers texte.
Très pratique pour écrire par exemple, des assistants


Fichier tutorhta.txt (servant dans le script tutor.hta)

%pywizard%
%
<h1>Introduction</h1>
D'apr&egrave;s l'article de microsoft :
<a href="http://msdn2.microsoft.com/en-us/library/bb264002.aspx">Getting Your Page's Data into a Bind</a>
%
%
<h2>Chapitre 1</h2>
%
%
<h2>Chapitre 2</h2>
%

fichier tutor.hta

<html>
<head>
<OBJECT ID="Content" WIDTH=0 HEIGHT=0
CLASSID="CLSID:333C7BC4-460F-11D0-BC04-0080C7055A83">
<PARAM NAME="TextQualifier" VALUE="%">
<PARAM NAME="FieldDelim" VALUE="|">
<PARAM NAME="DataURL" VALUE="tutorhta.txt">
<PARAM NAME="UseHeader" VALUE="true">
</OBJECT>
</head>
<body style="font-family: verdana">

<span style="font-weight: bold; cursor: hand" onclick="goBackward()"><<</span>
Python - HTA
<span style="font-weight: bold; cursor: hand" onclick="goForward()">>></span>
<br>
<span id=Status></span><br>
<div id=TheStory datasrc=#Content datafld="pywizard" dataformatas="HTML"></div>

<SCRIPT language=Python>

doc = document.all
rs = None

def goForward():
global rs
if (rs.absolutePosition != rs.recordCount) :
rs.MoveNext()
else :
rs.MoveFirst()

def goBackward():
global rs
if (rs.absolutePosition > 1) :
rs.MovePrevious()
else :
rs.MoveLast()

def initialize():
global rs
rs=doc.Content.recordset;


window.onload = initialize()

</SCRIPT>
</body>
</html>

Thursday, July 19, 2007

HTA - Python : Gestionnaire de Tâches


<html>
<head>
<title>Python Process Lister</title>
<HTA:APPLICATION
ID="processlister"
APPLICATIONNAME="Python Process Lister"
SCROLL="auto"
SCROLLFLAT = "yes"
SINGLEINSTANCE="yes"
>

<style type="text/css">

body {
background-color: #ababcd;
font-family: Verdana
font-size: 0.9 em;
color: #fff;
}

a:link {text-decoration: none; color:#343456}
a:visited {text-decoration: none;color:#343456}
a:active {text-decoration: none; color:#343456}
a:hover {text-decoration: underline; color:452200;}

</style>
</head>


<body onLoad="Initialisation()">
<center><h2>Liste des Process</h2></center>
<span id = "ProcessList"></span>


<!-- Script Python -->
<SCRIPT LANGUAGE="Python">
from win32com.client import GetObject
import win32gui

doc = document.All

ChaineConnexion = r"WinMgMts:\\%s\%s"

listProcess = []

process = None

def displayProc(args):
proc = None
numproc = int(args)
rep = window.confirm("Voulez-vous terminer le processus %s ?"%args )
if rep:
for p in process:
proc = p
if int(p.Handle) == numproc:
alert(u"Le processus %d va être supprimé"%numproc)
break
proc.Terminate

def getAllObjects(serveur = ".", espaceDenom = "root\cimv2", classe = "Win32_Process"):
c = GetObject(ChaineConnexion%(serveur,espaceDenom))
objects = c.ExecQuery(r"select * from %s"%classe)
return objects

def Initialisation():
win32gui.MessageBox(0, " Hello From Python", "Message d'Invite", 0)
RefreshList()
iTimerID = window.setInterval("RefreshList()", 10000)

def RefreshList():
global process

process = getAllObjects()

result = "<table>"
for processus in process:
result += """<tr><td align="right">%4d</td>
<td style="padding-left:15px;"><a class="maya" href="#" onClick="displayProc(%d)">%s</a></div></td>
<td>%s</td>
</tr>"""%(int(processus.Handle),int(processus.Handle),processus.Name,processus.CommandLine)

result += "</table>"

doc.ProcessList.InnerHTML = result

</SCRIPT>

</body>
</html>

Sunday, July 15, 2007

HTA : Arguments en ligne de commande


<HTML>

<HEAD>

<TITLE>HTA Demo</TITLE>
<HTA:APPLICATION ID="oHTA"
APPLICATIONNAME="myApp"
BORDER="thin"
BORDERSTYLE="normal"
CAPTION="yes"
ICON=""
MAXIMIZEBUTTON="yes"
MINIMIZEBUTTON="yes"
SHOWINTASKBAR="no"
SINGLEINSTANCE="no"
SYSMENU="yes"
VERSION="1.0"
WINDOWSTATE="maximize"/>

<SCRIPT language="Python">
d = document.all
lienMSN = "http://msdn2.microsoft.com/en-us/library/ms536495.aspx"
def window_onload():
sTempStr = "applicationName = " + d.oHTA.applicationName + "<br />"
sTempStr += "commandLineName = " + d.oHTA.commandLine + "<br />"
d.oPre.innerHTML = '<a href="%s">%s</a>'%(lienMSN,lienMSN) + "<br />" + sTempStr

</SCRIPT>

</HEAD>

<BODY SCROLL="no">

<p>Les arguments doivent suivre le nom du script et être entourés de guillemets</p>
<PRE ID=oPre></PRE>

</BODY>

</HTML>