[Python-checkins] CVS: python/dist/src/Lib urllib.py,1.96,1.97

Eric S. Raymond python-dev@python.org
Mon, 10 Jul 2000 08:00:35 -0700


Update of /cvsroot/python/python/dist/src/Lib
In directory slayer.i.sourceforge.net:/tmp/cvs-serv11503/dist/src/Lib

Modified Files:
	urllib.py 
Log Message:
Moves some test code into the scope of if __name__ == '__main__', where it
won't get loaded when we're using the classes in this module.


Index: urllib.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/urllib.py,v
retrieving revision 1.96
retrieving revision 1.97
diff -C2 -r1.96 -r1.97
*** urllib.py	2000/07/01 07:03:30	1.96
--- urllib.py	2000/07/10 15:00:32	1.97
***************
*** 1078,1166 ****
          return proxies
  
! 
! # Test and time quote() and unquote()
! def test1():
!     import time
!     s = ''
!     for i in range(256): s = s + chr(i)
!     s = s*4
!     t0 = time.time()
!     qs = quote(s)
!     uqs = unquote(qs)
!     t1 = time.time()
!     if uqs != s:
!         print 'Wrong!'
!     print `s`
!     print `qs`
!     print `uqs`
!     print round(t1 - t0, 3), 'sec'
! 
! 
! def reporthook(blocknum, blocksize, totalsize):
!     # Report during remote transfers
!     print "Block number: %d, Block size: %d, Total size: %d" % (blocknum, blocksize, totalsize)
! 
! # Test program
! def test(args=[]):
!     if not args:
!         args = [
!             '/etc/passwd',
!             'file:/etc/passwd',
!             'file://localhost/etc/passwd',
!             'ftp://ftp.python.org/etc/passwd',
! ##          'gopher://gopher.micro.umn.edu/1/',
!             'http://www.python.org/index.html',
!             ]
!         if hasattr(URLopener, "open_https"):
!             args.append('https://synergy.as.cmu.edu/~geek/')
!     try:
!         for url in args:
!             print '-'*10, url, '-'*10
!             fn, h = urlretrieve(url, None, reporthook)
!             print fn, h
!             if h:
!                 print '======'
!                 for k in h.keys(): print k + ':', h[k]
!                 print '======'
!             fp = open(fn, 'rb')
!             data = fp.read()
!             del fp
!             if '\r' in data:
!                 table = string.maketrans("", "")
!                 data = string.translate(data, table, "\r")
!             print data
!             fn, h = None, None
!         print '-'*40
!     finally:
!         urlcleanup()
  
! def main():
!     import getopt, sys
!     try:
!         opts, args = getopt.getopt(sys.argv[1:], "th")
!     except getopt.error, msg:
!         print msg
!         print "Use -h for help"
!         return
!     t = 0
!     for o, a in opts:
!         if o == '-t':
!             t = t + 1
!         if o == '-h':
!             print "Usage: python urllib.py [-t] [url ...]"
!             print "-t runs self-test;",
!             print "otherwise, contents of urls are printed"
!             return
!     if t:
!         if t > 1:
!             test1()
!         test(args)
!     else:
          if not args:
              print "Use -h for help"
!         for url in args:
!             print urlopen(url).read(),
  
- # Run test program when run as a script
- if __name__ == '__main__':
      main()
--- 1078,1165 ----
          return proxies
  
! # Run test program when run as a script
! if __name__ == '__main__':
!     # Test and time quote() and unquote()
!     def test1():
!         import time
!         s = ''
!         for i in range(256): s = s + chr(i)
!         s = s*4
!         t0 = time.time()
!         qs = quote(s)
!         uqs = unquote(qs)
!         t1 = time.time()
!         if uqs != s:
!             print 'Wrong!'
!         print `s`
!         print `qs`
!         print `uqs`
!         print round(t1 - t0, 3), 'sec'
! 
! 
!     def reporthook(blocknum, blocksize, totalsize):
!         # Report during remote transfers
!         print "Block number: %d, Block size: %d, Total size: %d" % (blocknum, blocksize, totalsize)
  
!     # Test program
!     def test(args=[]):
          if not args:
+             args = [
+                 '/etc/passwd',
+                 'file:/etc/passwd',
+                 'file://localhost/etc/passwd',
+                 'ftp://ftp.python.org/etc/passwd',
+     ##          'gopher://gopher.micro.umn.edu/1/',
+                 'http://www.python.org/index.html',
+                 ]
+             if hasattr(URLopener, "open_https"):
+                 args.append('https://synergy.as.cmu.edu/~geek/')
+         try:
+             for url in args:
+                 print '-'*10, url, '-'*10
+                 fn, h = urlretrieve(url, None, reporthook)
+                 print fn, h
+                 if h:
+                     print '======'
+                     for k in h.keys(): print k + ':', h[k]
+                     print '======'
+                 fp = open(fn, 'rb')
+                 data = fp.read()
+                 del fp
+                 if '\r' in data:
+                     table = string.maketrans("", "")
+                     data = string.translate(data, table, "\r")
+                 print data
+                 fn, h = None, None
+             print '-'*40
+         finally:
+             urlcleanup()
+ 
+     def main():
+         import getopt, sys
+         try:
+             opts, args = getopt.getopt(sys.argv[1:], "th")
+         except getopt.error, msg:
+             print msg
              print "Use -h for help"
!             return
!         t = 0
!         for o, a in opts:
!             if o == '-t':
!                 t = t + 1
!             if o == '-h':
!                 print "Usage: python urllib.py [-t] [url ...]"
!                 print "-t runs self-test;",
!                 print "otherwise, contents of urls are printed"
!                 return
!         if t:
!             if t > 1:
!                 test1()
!             test(args)
!         else:
!             if not args:
!                 print "Use -h for help"
!             for url in args:
!                 print urlopen(url).read(),
  
      main()