[Zope-CVS] CVS: Packages/WebsiteLoadTest/utilities - proxy.py:1.5

Andreas Jung andreas@andreas-jung.com
Wed, 16 Jul 2003 08:32:34 -0400


Update of /cvs-repository/Packages/WebsiteLoadTest/utilities
In directory cvs.zope.org:/tmp/cvs-serv29521/utilities

Modified Files:
	proxy.py 
Log Message:
- minor fixes
- code cleanup


=== Packages/WebsiteLoadTest/utilities/proxy.py 1.4 => 1.5 ===
--- Packages/WebsiteLoadTest/utilities/proxy.py:1.4	Wed Mar 20 08:57:34 2002
+++ Packages/WebsiteLoadTest/utilities/proxy.py	Wed Jul 16 08:32:28 2003
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2.1
+#!/usr/bin/env python2.3
 
 ##############################################################################
 #
@@ -13,10 +13,10 @@
 # 
 ##############################################################################
 
-import os,sys,re,getopt
-import urlparse,time,shutil
+import os, sys, re, getopt
+import urlparse, time, shutil
 import BaseHTTPServer
-from types import FileType,StringType
+from types import FileType, StringType
 from websiteloadtest.common import myhttplib
 from websiteloadtest.common.Request import Request
 
@@ -51,7 +51,6 @@
 
 interesting_headers = None # by default, keep all of them
 
-
 ############################################################################
 # Don't change anything below
 ############################################################################
@@ -62,7 +61,6 @@
 # timestamp start of test
 start_ts = time.time()
 
-
 numProxyData = numPosts = 0
 
 # Create storage directories
@@ -71,16 +69,15 @@
         os.makedirs(d)
 
 # determine the last used number for post data
-files = os.listdir(postDataDir)
-files = filter(lambda x: x.startswith('post.'),files)
+files = [f for f in os.listdir(postDataDir) if f.startswith('post.') ]
 files.sort()
+
 try:
     numPosts = int(files[-1].split('.')[-1]) + 1
 except: pass
 
 # determine the last used number for proxy data
-files = os.listdir(proxyDataDir)
-files = filter(lambda x: x.startswith('proxy.'),files)
+files = [f for f in os.listdir(postDataDir) if f.startswith('proxy.') ]
 files.sort()
 try:
     numProxyData = int(files[-1].split('.')[-1]) + 1
@@ -90,9 +87,9 @@
 def debug(*args):
     """ write debug to file or file descriptor """
 
-    if debug_level==0: return
+    if not debug_level: return
 
-    if isinstance(debug_fp,FileType) and not debug_fp.closed:
+    if isinstance(debug_fp, FileType) and not debug_fp.closed:
         for a in args: debug_fp.write(str(a)+' ')
         debug_fp.write('\n')
 
@@ -175,7 +172,7 @@
 
         for hd in headers.headers:
             fields = filter(lambda x: x!='', hd.strip().split(":"))
-            if fields==[]: continue
+            if not fields: continue
 
             k = fields[0]
             v = ':'.join(fields[1:])
@@ -203,7 +200,7 @@
         # Create request object to be written to file
 
         req = Request(  
-                    timestamp = time.time()-start_ts,
+                    timestamp=time.time()-start_ts,
                     path=tp[2],
                     params=tp[3],
                     query=tp[4],
@@ -223,21 +220,22 @@
 
     do_HEAD = do_GET = do_POST = do_XXX
 
-    def send_srv(self, method,path, headers='', body=''):
+    def send_srv(self, method, path, headers='', body=''):
 
-        x = headers.getheader('host')
-        if x.find(':')>-1:
-            remoteHost,remotePort = x.split(':')
+        host_hd = headers.getheader('host')
+        if host_hd is None: return 
+
+        if host_hd.find(':') >- 1:
+            remoteHost, remotePort = host_hd.split(':')
         else:
-            remoteHost,remotePort = x,80
-        
+            remoteHost, remotePort = host_hd, 80
 
-        h = myhttplib.HTTP(remoteHost,int(remotePort))
+        h = myhttplib.HTTP(remoteHost, int(remotePort))
         h.putrequest(method,path)
 
         debug("sending -----------------------------------")
-        debug("Remote: %s:%s" % (remoteHost,remotePort))
-        debug(method,path)
+        debug("Remote: %s:%s" % (remoteHost, remotePort))
+        debug(method, path)
         debug(headers)
 
         for hd in headers.headers:
@@ -256,10 +254,8 @@
         h.endheaders()
 
         if method == 'POST': 
-
             debug('sending post body:')
             debug(body)
-
             # Bad hack - usually we don't need to add \n - but for Zope 2.3 !
             h.send(body+'\n')
 
@@ -295,12 +291,11 @@
     print "Version:",__version__
 
 
-def main():
-    global localHost,localPort,debug_level,debug_fp,postonly,storeobjects
+if __name__=='__main__':
 
     opts,args = getopt.getopt(sys.argv[1:],
                               'ihdo:p:m:',
-                              ['port=',
+                              ('port=',
                                'help',
                                'debug',
                                'output=',
@@ -309,28 +304,23 @@
                                'storeobjects',
                                'init',
                                'interesting-headers='
-                              ])
+                              ))
 
     for k,v in opts:
-        if k in ['--help','-h']:            usage(); sys.exit(1)
-        if k in ['--debug','-d']:           debug_level = 1
-        if k in ['--port','-p']:            localPort   = int(v)
-        if k in ['--host','-m']:            localHost   = v
-        if k in ['--output','-o']:          debug_fp    = v
-        if k in ['--postonly']:             postonly    = 1
-        if k in ['--storeobjects']:         storeobjects = 1
-        if k in ['--init','-i']:            init()
+        if k in ['--help','-h']:    usage(); sys.exit(1)
+        if k in ['--debug','-d']:   debug_level = 1
+        if k in ['--port','-p']:    localPort   = int(v)
+        if k in ['--host','-m']:    localHost   = v
+        if k in ['--output','-o']:  debug_fp    = v
+        if k in ['--postonly']:     postonly    = 1
+        if k in ['--storeobjects']: storeobjects = 1
+        if k in ['--init','-i']:    init()
 
         if k in ['--interesting-headers']:
-            global interesting_headers
             interesting_headers = [ x.strip().lower() for x in v.split(',') ]
 
-    print "Proxy listening on host: %s, port: %d" % (localHost or "all IPs",localPort)
+    print >>sys.stderr, "Proxy listening on host: %s, port: %d" % (localHost or "all IPs",localPort)
 
     proxy = BaseHTTPServer.HTTPServer( (localHost,localPort), ReqHandler)
     proxy.serve_forever()
 
-    sys.exit(0)
-
-if __name__=='__main__':
-    main()