summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-06-25 02:39:00 (GMT)
committerGuido van Rossum <guido@python.org>1998-06-25 02:39:00 (GMT)
commit2349015a878adc203613257fa9578dbd921ec573 (patch)
treecf904745c840a3668301f89e62b461d6c7c39cab /Lib
parentb1f0812be77c435178533add4fd8e4f6d8606b23 (diff)
downloadcpython-2349015a878adc203613257fa9578dbd921ec573.zip
cpython-2349015a878adc203613257fa9578dbd921ec573.tar.gz
cpython-2349015a878adc203613257fa9578dbd921ec573.tar.bz2
Rewrite the (test) main program so that when used as a script, it can
retrieve one or more URLs to stdout. Use -t to run the self-test.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/urllib.py34
1 files changed, 29 insertions, 5 deletions
diff --git a/Lib/urllib.py b/Lib/urllib.py
index b9317f2..e21c9a2 100644
--- a/Lib/urllib.py
+++ b/Lib/urllib.py
@@ -937,9 +937,7 @@ def test1():
# Test program
-def test():
- import sys
- args = sys.argv[1:]
+def test(args=[]):
if not args:
args = [
'/etc/passwd',
@@ -970,7 +968,33 @@ def test():
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__':
- test1()
- test()
+ main()