当前位置:
文档之家› 使用Python进行Windows系统编程(Windows System Programming Using Python)
使用Python进行Windows系统编程(Windows System Programming Using Python)
– Files can be passed as stdin/out/err – Process and thread handles are waitable using the win32event module.
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 14
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 13
Ultimate Process Control (1 of 2)
win32process module exposes the low level Win32 API. Full support for CreateProcess, CreateProcessAsUser, CreateThread etc. Full support for Windows Handles
– Hence we are here.
Often used in a similar problem domain to Perl/Tcl Proponents consider readability and maintainability a big strong-point
– Very similar to os.system(), but overcomes limitations. – >>> import win32api – >>> win32api.WinExec("notepad") – Optional parameter allows you to specify the Window’s initial state (eg, minimized)
Official releases can be found at
/windows
Extensions home is at
/crew/mhammond
Windows System Programming using Python Slide 6
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 2
Who this talk is for?
Existing Python programmers
– Even those without any Windows experience should follow this without problem.
Most content taken directly from upcoming book for O’Reilly Python Programming on Win32 By Mark Hammond and Andy Robinson
/catalog/pythonwin32/
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 12
Better Process Control (2 of 2)
win32api.ShellExecute()
– Typically opens “documents” - eg, execute “foo.doc”, and (typically) Word will open. – Finer control over the new process. – Can also execute arbitrary executables - not limited to documents. – For example, to print a specific document: win32api.ShellExecute(0, "print", \ "MyDocument.doc", None, "", 1)
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 9
Portable Process Control (2 of 3)
os.execv family
– Doesn’t search system path, and doesn’t parse command lines
Windows System Programming using Python
Slide 10
Portable Process Control (3 of 3)
os.popen()
>>> file = os.popen("echo Hello") >>> file.read() 'Hello\012' – Works fine from Windows NT console programs, but fails miserably from a GUI! – win32pipe module in the Win32 extensions provides a working replacement.
Although
there is not enough time to explain the relevant Windows APIs
Existing Windows Programmers
– Even without Python experience, you should immediately see the similarities between your existing language.
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 7
Why not just use Python?
Python has excellent native support for files, processes, threads etc. These features are typically limited to those defined by ANSI C.
O’Reilly Python Conference Aug 24, 1999
System Level Programming?
For this talk, we define system level programming as working with low-level features of Windows Files, Pipes, Processes, Threads, Services, Event Log and so forth. Python and similar languages really not suitable for device-driver type development, and other more system-like Systems Programming!
– Many advanced Windows features are not exposed using these interfaces. – Standard implementation of some of the standard library functions leaves a little to be desired in a Windows environment.
O’Reilly Python Conference Aug 24, 1999 Windows System Programming using Python Slide 5
Python Windows Extensions
Includes:
– Pythonwin: MFC based GUI environment and IDE/Debugger – win32com: Interfaces Python and COM – win32 Extensions: Interfaces to native Win32 API.
– Just often not quite how we would like!
os.system()
import os os.system(“notepad C:\\autoexec.bat”)
Problems – Creates a new console window when run from a GUI. – Waits for process to terminate.
O’Reilly Python Conference Aug 24, 1999
Windows System Programming using Python
Slide 3
What is Python
Interpreted, dynamic high-level language Obviously open source
Windows System Programming using Python
Mark Hammond mhammond@.au
OReilly Open Source Python Conference August 1999, Monterey, CA