Saturday, August 13, 2011

Installing Django Under Ubuntu 11-04

From the $ prompt of the terminal:

$ sudo apt-get install python-django

Monday, August 8, 2011

TCP/IP Port Pipe - UNDER CONSTRUCTION



import optparse
import socket, select, threading

class Pipe(threading.Thread):

time_out = 5
buffer_size = 4096
max_idle = 5

def __init__(self, server_host, server_port, dclient_connxn):

super(Pipe, self).__init__()

self.server_host = server_host
self.server_port = server_port
self.server_addr = (server_host, server_port)

self.client_connxn = client_connxn

def connect_to_server(self):
self.server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print(self.server_addr)
self.server_sock.connect(self.server_addr)

def log_request_response(self, req, resp):
print('request')
print(req)
print('response')
print(resp)

def run(self):
self.connect_to_server()

to_read = [self.client_connxn, self.server_sock]
to_write = []
to_watch_for_exception = to_read

request = ''
response = ''

seconds_idle = 0

while True:
(ready_to_read, ready_to_write, experienced_exception) = select.select(to_read, to_write, to_watch_for_exception, 1)

if (experienced_exception):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break

if (ready_to_read):
seconds_idle = 0

for waitable in ready_to_read:
data = waitable.recv(buffer_size)
if (waitable == self.client_connxn):
out = self.server_sock
request += data
else:
out = self.client_connxn
log_to = response
response += data
out.send(data)

else:
seconds_idle += 1
if (seconds_idle == max_idle):
self.log_request_response(request, response)
self.client_connxn.close()
self.server_sock.close()
break

class PortListener():'''

'''
def __init__(self, listen_on_port, pipe_to_host, pipe_to_port, max_connxns=1):
self.listen_on_port = listen_on_port
self.pipe_to_host = pipe_to_host
self.pipe_to_port = pipe_to_port

def listen():

listen_to_addr = ('127.0.0.1', self.listen_to_port)
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.bind(listen_to_addr)
listen_socket.listen(max_connxns)

while True:
# pause until client requests connection
(client_connxn, client_addr) = listen_socket.accept()
pipe = Pipe(self.pipe_to_host, self.pipe_to_port, client_connxn)
pipe.start()

def setup_info_logger(log_file_path):
logging.basicConfig(filename=log_file_path, level=logging.INFO)
return logging.getLogger()

def parse_command_line():'''
parse command line argument to populate dictionary
return dictionary with keys = client_port, server_host, server_port, log_file_path
returns None if mandatory option missing
'''
parser = optparse.OptionParser()
parser.add_option('--clientport', dest='client_port', help='port client to this port on localhost')
parser.add_option('--serverhost', dest='server_host', help='server host, default = 127.0.0.1')
parser.add_option('--serverport', dest='server_port', help='server port')
parser.add_option('--logfilepath', dest='log_file_path', help='log file path')

(options, args) = parser.parse_args()

if (options.client_port == None):
print('--clientport is mandatory')
return None
if (options.server_port == None):
print('--serverport is mandatory')
return None

d = {}

if (options.log_file != None): d['log_file'] = options.log_file
else: d['log_file'] = None

d['client_port'] = options.client_port

if (options.server_host != None): d['server_host'] = options.server_host
else: d['server_host'] = '127.0.0.1'

d['server_port'] = options.server_port

return d

def main():
opt = parse_command_line()
logger = None
if (opt['log_file_path'] != None):
logger = setup_info_logger(opt['log_file_path'])
listener = PortListener(opt['client_port'], opt['server_host'], opt['server_port']]

if (__name__ == '__main__'):
main()

Saturday, August 6, 2011

SUZUKI Hisao's - TinyProxy - Python HTTP Proxy

Original Site


Script reproduced without permission:



#!/bin/sh -
"exec" "python" "-O" "$0" "$@"

__doc__ = """Tiny HTTP Proxy.

This module implements GET, HEAD, POST, PUT and DELETE methods
on BaseHTTPServer, and behaves as an HTTP proxy. The CONNECT
method is also implemented experimentally, but has not been
tested yet.

Any help will be greatly appreciated. SUZUKI Hisao
"""

__version__ = "0.2.1"

import BaseHTTPServer, select, socket, SocketServer, urlparse

class ProxyHandler (BaseHTTPServer.BaseHTTPRequestHandler):
__base = BaseHTTPServer.BaseHTTPRequestHandler
__base_handle = __base.handle

server_version = "TinyHTTPProxy/" + __version__
rbufsize = 0 # self.rfile Be unbuffered

def handle(self):
(ip, port) = self.client_address
if hasattr(self, 'allowed_clients') and ip not in self.allowed_clients:
self.raw_requestline = self.rfile.readline()
if self.parse_request(): self.send_error(403)
else:
self.__base_handle()

def _connect_to(self, netloc, soc):
i = netloc.find(':')
if i >= 0:
host_port = netloc[:i], int(netloc[i+1:])
else:
host_port = netloc, 80
print "\t" "connect to %s:%d" % host_port
try: soc.connect(host_port)
except socket.error, arg:
try: msg = arg[1]
except: msg = arg
self.send_error(404, msg)
return 0
return 1

def do_CONNECT(self):
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(self.path, soc):
self.log_request(200)
self.wfile.write(self.protocol_version +
" 200 Connection established\r\n")
self.wfile.write("Proxy-agent: %s\r\n" % self.version_string())
self.wfile.write("\r\n")
self._read_write(soc, 300)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def do_GET(self):
(scm, netloc, path, params, query, fragment) = urlparse.urlparse(
self.path, 'http')
if scm != 'http' or fragment or not netloc:
self.send_error(400, "bad url %s" % self.path)
return
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
if self._connect_to(netloc, soc):
self.log_request()
soc.send("%s %s %s\r\n" % (
self.command,
urlparse.urlunparse(('', '', path, params, query, '')),
self.request_version))
self.headers['Connection'] = 'close'
del self.headers['Proxy-Connection']
for key_val in self.headers.items():
soc.send("%s: %s\r\n" % key_val)
soc.send("\r\n")
self._read_write(soc)
finally:
print "\t" "bye"
soc.close()
self.connection.close()

def _read_write(self, soc, max_idling=20):
iw = [self.connection, soc]
ow = []
count = 0
while 1:
count += 1
(ins, _, exs) = select.select(iw, ow, iw, 3)
if exs: break
if ins:
for i in ins:
if i is soc:
out = self.connection
else:
out = soc
data = i.recv(8192)
if data:
out.send(data)
count = 0
else:
print "\t" "idle", count
if count == max_idling: break

do_HEAD = do_GET
do_POST = do_GET
do_PUT = do_GET
do_DELETE=do_GET

class ThreadingHTTPServer (SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer): pass

if __name__ == '__main__':
from sys import argv
if argv[1:] and argv[1] in ('-h', '--help'):
print argv[0], "[port [allowed_client_name ...]]"
else:
if argv[2:]:
allowed = []
for name in argv[2:]:
client = socket.gethostbyname(name)
allowed.append(client)
print "Accept: %s (%s)" % (client, name)
ProxyHandler.allowed_clients = allowed
del argv[2:]
else:
print "Any clients will be served..."
BaseHTTPServer.test(ProxyHandler, ThreadingHTTPServer)