Internet Robot

Red Hat Linux User hiren at c526184-a.frmt1.sfba.home.com
Tue Apr 13 04:13:04 EDT 1999


On Sat, 10 Apr 1999 04:52:06 GMT, gscot at my-dejanews.com  wrote:
>      I can now POST my request but the server is asking for authentication.
>You mentioned that it might be helpful to capture and look at the client’s
>out put. How do I do that. 

Here's one way to do it - 

-----------------http_portmon.py----------------------------

#!/usr/bin/env python

# sits between a http client and http server and
# outputs data going either way. 

# Todo:
#   1. Add getopt switches
#   2. Add host name options
#   3. Modify to use select to detect data on either end.


# "@(#) Module Name: portmon.py Version: 1.2 Date: 96/12/09";
import sys
import SocketServer
import string
import os
import posix
import mimetools
from socket import *
import urlparse
import re

BUFSIZE = 1024


class PortServer(SocketServer.ForkingMixIn,SocketServer.TCPServer):
  def printDebug(self):
    pass
  def collect_children(self):
    print 'doing a waitpid'
    try:
      pid,errorStatus = os.waitpid(0,os.WNOHANG)
    except posix.error,x:
      print 'no children'
      return
    print 'after waitpid'
    if not pid: 
      return
    print 'pid = ', pid
    print 'pidlist = ', self.active_children
    if pid in self.active_children:
      self.active_children.remove(pid)

def is_gif(cmd):
  import regex
  pattern = '\.gif'
  prog = regex.compile(pattern)
  index = prog.search(cmd)
  if index >= 0 :
    return 1
  else:
    return 0

def is_external(host):
  # assume that if the host name is of the
  # form xxx.xxx.xxx then it is external 
  # else it is internal. 
  x = string.splitfields(host,'.')
  if len(x) > 1:
    return 1
  else :
    return 0

class RequestHandler(SocketServer.StreamRequestHandler):
  MessageClass = mimetools.Message
  def handle(self):
    self.raw_requestline = self.rfile.readline()
    requestline = self.raw_requestline
    if requestline[-2:] == '\r\n':
      requestline = requestline[:-2]
    elif requestline[-1:] == '\n':
      requestline = requestline[:-1]
    self.requestline = requestline
    cmd = requestline
    print 'processing ',cmd
    [command, path, version] = string.split(cmd)
    s,n,p,a,q,frag =  urlparse.urlparse(path)
    index = string.find(n,':')
    req =  urlparse.urlunparse(('','',p,a,q,frag))
    request = '%s %s %s\r\n' % (command ,req,version)
    if index > 0:
      # we have host:port in the network field
      [host,host_port_str] = string.splitfields(n,':')
      host_port = string.atoi(host_port_str)
    else:
      host = n
      host_port = 80    
    if is_external(host):
      host = 'internet'
      host_port = 80
      request = cmd
    print host,host_port
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((host, host_port))
    s.send(request)
    self.headers = self.MessageClass(self.rfile, 0)
    if command == 'POST':
      length = string.atoi (self.headers.getheader('content-length') )
      self.content_data = self.rfile.read(length)
    print 'headers = ', self.headers.headers
    #s.send(self.raw_requestline)
    print 'sending ---------------------------------'
    sys.stdout.write('portmon_client:%s' % request )
    for header in self.headers.headers:
      s.send(header)
      sys.stdout.write('portmon_client:%s' %header )
    s.send('\r\n')
    sys.stdout.write('portmon_client:\r\n')
    if command == 'POST':
      s.send(self.content_data)
      sys.stdout.write('portmon_client:%s' % self.content_data)
    print '----------------------------------------------'
    reply = ''
    print '------------------portmon_server data begin------------------'
    while 1:
      data = s.recv(BUFSIZE)
      if not data: break
      self.wfile.write(data)
      if not is_gif(cmd):
        sys.stdout.write(data)
      print '------------------portmon_server data end------------------'
#print data
#reply = reply + data
#s.shutdown(1)

        

def main():
  global f
  if sys.argv[1:]:
    port = string.atoi(sys.argv[1])
  else:
    port = 9000
  server_address = ('',port)
  echomon = PortServer(server_address,RequestHandler)
  print 'HTTP PortMon Ready'
  echomon.serve_forever()

main()
-----------------http_portmon.py----------------------------

What you need to do is - 

1. run http_portmon.py on a port (say 9000) on your Linux box (say
linux_host)

2. Now in your browser , select linux_host and port 9000 as your proxy

3. Now invoke your cgi script as you normally would and watch the
client and server data on the std output where you ran http_portmon.py


Hope that helps,
Hiren




More information about the Python-list mailing list