rasutil.py :  » Windows » pyExcelerator » pywin32-214 » win32 » scripts » 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 » scripts » rasutil.py
# A demo of using the RAS API from Python
import sys
import win32ras

# The error raised if we can not
class ConnectionError(Exception):
  pass
  
def Connect(rasEntryName, numRetries = 5):
  """Make a connection to the specified RAS entry.
  
  Returns a tuple of (bool, handle) on success.
  - bool is 1 if a new connection was established, or 0 is a connection already existed.
  - handle is a RAS HANDLE that can be passed to Disconnect() to end the connection.
  
  Raises a ConnectionError if the connection could not be established.
  """
  assert numRetries > 0
  for info in win32ras.EnumConnections():
    if info[1].lower()==rasEntryName.lower():
      print "Already connected to", rasEntryName
      return 0, info[0]

  dial_params, have_pw = win32ras.GetEntryDialParams(None, rasEntryName)
  if not have_pw:
    print "Error: The password is not saved for this connection"
    print "Please connect manually selecting the 'save password' option and try again"
    sys.exit(1)

  print "Connecting to", rasEntryName, "..."
  retryCount = numRetries
  while retryCount > 0:
    rasHandle, errCode = win32ras.Dial(None, None, dial_params, None)
    if win32ras.IsHandleValid(rasHandle):
      bValid = 1
      break
    print "Retrying..."
    win32api.Sleep(5000)
    retryCount = retryCount - 1
  
  if errCode:
    raise ConnectionError(errCode, win32ras.GetErrorString(errCode))
  return 1, rasHandle

def Disconnect(handle):
  if type(handle)==type(''): # have they passed a connection name?
    for info in win32ras.EnumConnections():
      if info[1].lower()==handle.lower():
        handle = info[0]
        break
    else:
      raise ConnectionError(0, "Not connected to entry '%s'" % handle)

  win32ras.HangUp(handle)

usage="""rasutil.py - Utilities for using RAS

Usage:
  rasutil [-r retryCount] [-c rasname] [-d rasname]
  
  -r retryCount - Number of times to retry the RAS connection
  -c rasname - Connect to the phonebook entry specified by rasname
  -d rasname - Disconnect from the phonebook entry specified by rasname
"""

def Usage(why):
  print why
  print usage
  sys.exit(1)
  
if __name__=='__main__':
  import getopt
  try:
    opts, args = getopt.getopt(sys.argv[1:], "r:c:d:")
  except getopt.error, why:
    Usage(why)
  retries = 5
  if len(args) != 0:
    Usage("Invalid argument")

  for opt, val in opts:
    if opt=='-c':
      Connect(val, retries)
    if opt=='-d':
      Disconnect(val)
    if opt=='-r':
      retries = int(val)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.