rastest.py :  » Windows » pyExcelerator » pywin32-214 » win32 » Demos » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Windows » pyExcelerator 
pyExcelerator » pywin32 214 » win32 » Demos » rastest.py
# rastest.py - test/demonstrate the win32ras module.
# Much of the code here contributed by Jethro Wright.

import sys
import os
import win32ras

# Build a little dictionary of RAS states to decent strings.
# eg win32ras.RASCS_OpenPort -> "OpenPort"
stateMap = {}
for name, val in win32ras.__dict__.items():
    if name[:6]=="RASCS_":
        stateMap[val] = name[6:]

# Use a lock so the callback can tell the main thread when it is finished.
import win32event
callbackEvent = win32event.CreateEvent(None, 0, 0, None)

def Callback( hras, msg, state, error, exterror):
#       print "Callback called with ", hras, msg, state, error, exterror
    stateName = stateMap.get(state, "Unknown state?")
    print "Status is %s (%04lx), error code is %d" % (stateName, state, error)
    finished = state in [win32ras.RASCS_Connected]
    if finished:
        win32event.SetEvent(callbackEvent)
    if error != 0 or int( state ) == win32ras.RASCS_Disconnected:
        #       we know for sure this is a good place to hangup....
        print "Detected call failure: %s" % win32ras.GetErrorString( error )
        HangUp( hras )
        win32event.SetEvent(callbackEvent)

def ShowConnections():
    print "All phone-book entries:"
    for (name,) in win32ras.EnumEntries():
        print " ", name
    print "Current Connections:"
    for con in win32ras.EnumConnections():
        print " ", con

def EditEntry(entryName):
    try:
        win32ras.EditPhonebookEntry(0,None,entryName)
    except win32ras.error, (rc, function, msg):
        print "Can not edit/find the RAS entry -", msg

def HangUp( hras ):
    #       trap potential, irrelevant errors from win32ras....
    try:
        win32ras.HangUp( hras )
    except:
        print "Tried to hang up gracefully on error, but didn't work...."
    return None

def Connect(entryName, bUseCallback):
    if bUseCallback:
        theCallback = Callback
        win32event.ResetEvent(callbackEvent)
    else:
        theCallback = None
    #       in order to *use* the username/password of a particular dun entry, one must
    #       explicitly get those params under win95....
    try:
        dp, b = win32ras.GetEntryDialParams( None, entryName )
    except:
        print "Couldn't find DUN entry: %s" % entryName
    else:
        hras, rc = win32ras.Dial(None, None, (entryName, "", "", dp[ 3 ], dp[ 4 ], ""),theCallback)
    #       hras, rc = win32ras.Dial(None, None, (entryName, ),theCallback)
    #       print hras, rc
        if not bUseCallback and rc != 0:
            print "Could not dial the RAS connection:", win32ras.GetErrorString(rc)
            hras = HangUp( hras )
        #       don't wait here if there's no need to....
        elif bUseCallback and win32event.WaitForSingleObject(callbackEvent, 60000)!=win32event.WAIT_OBJECT_0:
            print "Gave up waiting for the process to complete!"
            #       sdk docs state one must explcitly hangup, even if there's an error....
            try:
                cs = win32ras.GetConnectStatus( hras )
            except:
                #       on error, attempt a hang up anyway....
                hras = HangUp( hras )
            else:
                if int( cs[ 0 ] ) == win32ras.RASCS_Disconnected:
                    hras = HangUp( hras )
    return hras, rc

def Disconnect( rasEntry ):
    # Need to find the entry
    name = rasEntry.lower()
    for hcon, entryName, devName, devType in win32ras.EnumConnections():
        if entryName.lower() == name:
            win32ras.HangUp( hcon )
            print "Disconnected from", rasEntry
            break
    else:
        print "Could not find an open connection to", entryName

usage = """
Usage: %s [-s] [-l] [-c connection] [-d connection]
-l : List phone-book entries and current connections.
-s : Show status while connecting/disconnecting (uses callbacks)
-c : Connect to the specified phonebook name.
-d : Disconnect from the specified phonebook name.
-e : Edit the specified phonebook entry.
"""

def main():
    import getopt
    try:
        opts, args = getopt.getopt(sys.argv[1:], "slc:d:e:")
    except getopt.error, why:
        print why
        print usage % (os.path.basename(sys.argv[0],))
        return

    bCallback = 0
    if args or not opts:
        print usage % (os.path.basename(sys.argv[0],))
        return
    for opt, val in opts:
        if opt=="-s":
            bCallback = 1
        if opt=="-l":
            ShowConnections()
        if opt=="-c":
            hras, rc = Connect(val, bCallback)
            if hras != None:
                print "hras: 0x%8lx, rc: 0x%04x" % ( hras, rc )
        if opt=="-d":
            Disconnect(val)
        if opt=="-e":
            EditEntry(val)

if __name__=='__main__':
    main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.