Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

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)

Monday, April 26, 2010

Decorators & OO in Python

Jack Diederich
[Python-3000] PEP Draft: Class Decorators

Cesar Otero - IBM DevelopWorks White Paper
Python 3 primer, Part 2: Advanced topics
Metaclasses, decorators, and other strange creatures

Python Introspection/Reflection Utility Class

So the code below is for a python module 'mir.py', and contains a class 'Mir' and a rather thin main method 'run()' which demos the class.


class Mir:
"""mirror introspection/reflection class"""

meth_lookup_token_len = len('<function')
meth_type_lookup = {
'<bound me' : 'instance',
'<function' : 'static',
'<method-wr' : 'wrapper',
'<built-in ' : 'built-in'
}

def __init__(self):
"""docstring for initializer"""
pass

@staticmethod
def ins(target,show_magic_meths=False):

lines = []

"""provide basic information on the target"""

# column widthss
c1 = 20
c2 = 20
c3 = 40

field_names = []
method_names = []
mangled_field_names = []

dir_list = dir(target)

for ob in dir_list:

if (callable(getattr(target, ob))):
method_names.append(ob)
else:
field_names.append(ob)

# FIELDS --------------------------------------

# double leading and trailing underscore
magic_fields = []
# starts and ends with alphabetic
normal_fields = []

# double leading underscore # mangledFields = []
# single leading underscore # privateFields = []

for field_name in field_names:

a = getattr(target, field_name)
l = len(field_name)

# magic fields
if (field_name[0:2] == '__'):
if (field_name[l - 2 : l] == '__'):
magic_fields.append(field_name)
else:
mangled_field_names.append(field_names)
else:
normal_fields.append(field_name)

if '__doc__' in magic_fields:
docstring = getattr(target, '__doc__')
else:
docstring = 'no doc string'

if '__module__' in magic_fields:
moduleName = getattr(target, '__module__')
else:
moduleName = 'no module'

# METHODS

# class methods
# instance methods
# - constructor
# - vanilla

static_methods = []
instance_methods = []

magic_methods = []

meth_info = []

for method_name in method_names:

# DETERMINE METHOD TYPE = INSTANCE, STATIC

a = getattr(target, method_name)
try:
s = (str(a))
token = s[0:Mir.meth_lookup_token_len+1]
meth_type = Mir.meth_type_lookup[token]
except:
meth_type = s[0:c2]
else:
pass

# GET METHOD DOC-STRING

meth_docstring = getattr(a, '__doc__')

if (meth_docstring == None):
meth_docstring = ''
else:
meth_docstring = meth_docstring.replace('\n', ' | ')

if (meth_type == 'static'):
static_methods.append((method_name, meth_docstring))
elif (meth_type == 'instance'):
instance_methods.append((method_name, meth_docstring))
else:
pass

L = len(method_name)
prefix = (method_name[0:2] == '__')
suffix = (method_name[L-2:L] == '__')
if (prefix and suffix):
magic_methods.append(method_name)

meth_info.append((method_name, meth_type, meth_docstring))

lines.append('----------------------------------------------------------------------')
lines.append('target = ' + str(target))
lines.append('target type = ' + str(type(target)))
lines.append('source module: %s' % moduleName)
lines.append('----------------------------------------------------------------------')
lines.append('docstring: %s' % docstring)
lines.append('')
lines.append('----------------------------------------------------------------------')
lines.append('FIELDS')
lines.append('---------------')
# static
# class
# instance

for field_name in normal_fields:
a = getattr(target, field_name)
lines.append(field_name.ljust(20) + str(a).ljust(30))

if (show_magic_meths):
lines.append('----------------------------------------------------------------------')
lines.append('METHODS - MAGIC')
lines.append('---------------')
for (meth_name, meth_type, methDoc) in meth_info:
if (meth_name in magic_methods):
lines.append(meth_name.ljust(c1) + meth_type.ljust(c2) + methDoc.ljust(c3))

lines.append('----------------------------------------------------------------------')
lines.append('METHODS - NON-MAGIC')
lines.append('---------------')
for (meth_name, meth_type, methDoc) in meth_info:
if (meth_name not in magic_methods):
lines.append(meth_name.ljust(c1) + meth_type.ljust(c2) + methDoc.ljust(c3))

return lines

@staticmethod
def inspect(target):

lines = Mir.ins(target)

for line in lines:
print(line)

@staticmethod
def ins2f(target, fname):

file = open(fname, 'w')
for line in Mir.ins(target):
file.write(line + '\n')
file.close()


# #########################################################

def run():

lis = []
Mir.inspect(lis)

if __name__ == '__main__':

Tuesday, March 23, 2010

DevExpress XtraGrid - Group by Column - Enable/Disable

assuming you have

DevExpress.XtraGrid.GridControl gridControl;
DevExpress.XtraGrid.Views.Grid.GridView gridView;

to enable grouping capability
gridView.OptionsView.ShowGroupPanel = true

to disable
gridView.OptionsView.ShowGroupPanel = false

Monday, November 30, 2009

VBA Macro - Split/Parse CSV Strings


Option Explicit

Public Sub splitCSV(targetCell As range)

Dim row As Integer
row = targetCell.row

Dim col As Integer
col = targetCell.Column

Dim source As String
source = targetCell.Text

Dim count As Integer
count = 0

Dim startPos As Integer
startPos = 1

Dim nextCommaPos As Integer

Dim exitLoop As Boolean
exitLoop = False
While (exitLoop = False)

Dim commaPos As Integer
commaPos = InStr(startPos, source, ",")

Dim token As String

If (commaPos = 0) Then
exitLoop = True
Else

If (startPos = 1) Then
commaPos = 0
End If

nextCommaPos = InStr(commaPos + 1, source, ",")

If (nextCommaPos = 0) Then
nextCommaPos = Len(source) + 1
End If

token = Mid(source, commaPos + 1, nextCommaPos - commaPos - 1)
count = count + 1

Cells(row, col + count).Value = token

If (startPos <> 1) Then
startPos = commaPos + 1
Else
startPos = 2
End If


End If
Wend

End Sub

Public Sub SplitSelection()

Application.ScreenUpdating = False

Dim sel As range
Set sel = Selection

Dim row As Integer
For row = sel.row To (sel.row + sel.Rows.count - 1)

Dim targetCell As range
Set targetCell = Cells(row, sel.Column)
Call splitCSV(targetCell)

Next row

Application.ScreenUpdating = True

End Sub

Sunday, September 6, 2009

Retaining WhiteSpace When Posting Python Code

To post python code in a blog (html) and retain the line whitespace that is required in order for the code to remain unambiguous, simply enclose your code within pre html tag block.

The 'pre' tag yields a fixed-width (monospace) font, and preserves spaces and line-breaks.

<pre>

def doStuff(i):
print()
for z in range(i):
print(z)

c = 4

for z in range(c):
doStuff(z)

</pre>

Tuesday, June 16, 2009

plot f, f', f'' from csv with python & matplotlib


class rawDataObj:

pointCount = 0

colCount = 0
columns = []
colLabels = []

title = ""

# ===========================================

class Series:

def __init__(self):

self.x = []
self.y = []

#~ def __init__(self, xIn, yIn):

#~ if (len(xIn) != len(yIn)):
#~ raise lengthInequalityError, "length inequality"

#~ for zidx in range(len(xIn)):

#~ self.x.append(xIn[zidx])
#~ self.y.append(yIn[zidx])

# ================================

def DbyDX(self, xIn, yIn):

if (len(xIn) != len(yIn)):

raise setSizeInequalityError, 'set size inequality'

else:

for zidx in range(len(xIn) - 1):

xVal = (xIn[zidx + 1] + xIn[zidx]) / 2.0
self.x.append(xVal)

yVal = (yIn[zidx + 1] - yIn[zidx]) / (xIn[zidx + 1] - xIn[zidx])
self.y.append(yVal)

# ===========================================

import re

def loadrawDataObjFromFile(filePathAndName="/home/david/Desktop/ptpdrh.csv", hasHeader=False):

try:
f = open(filePathAndName, 'r')

one = []
two = []

eof = False

retDataObj = rawDataObj()

lineNum = 0
headerLineCount = 2

while (eof == False):

line = f.readline()
eof = (line == "")

if (line != ""):

lineNum = lineNum + 1

elements = re.split(',', line)

# strip newline (\n escape sequence) from last field of line
elements[len(elements) - 1] = elements[len(elements) - 1].rstrip("\n")

# validate column count
if (lineNum > headerLineCount):
if (len(elements) != retDataObj.colCount):
raise inconsistentColumnCountError, ('column count inconsistent at data line ' + (retDataOb.pointCount + 1))

if (lineNum <= headerLineCount):

if (lineNum == 1):

retDataObj.title = line

elif (lineNum == 2):

retDataObj.colCount = len(elements)

for num in range(retDataObj.colCount):
retDataObj.columns.append([])

for label in elements:
retDataObj.colLabels.append(label)

else: # if (lineNum > headerLineCount)

string = ""

for zidx in range(retDataObj.colCount):

string = elements[zidx]

if (len(string) == 0):
retDataObj.columns[zidx].append(None)
else:
retDataObj.columns[zidx].append(float(string))

retDataObj.pointCount = retDataObj.pointCount + 1

f.close()

except IOError:
print "io exception while attempting to load raw data"
print "from file:"
print filePathAndName


return retDataObj

# ===========================================

# ===========================================

from pylab import *

filePath = "/home/david/Desktop/FinData/"
fileName = "RSA_CPI_1981-2008.csv"
filePathAndName = filePath + fileName

dataObj = loadrawDataObjFromFile(filePathAndName, True)

#for y in range(dataObj.colCount - 1):
# plot(dataObj.columns[0], dataObj.columns[y + 1], label=str(dataObj.colLabels[y + 1]))

fig = figure()
fig.canvas.set_window_title(dataObj.title)

# BASE SERIES
axBase = subplot(311)
baseLabel = label=str(dataObj.colLabels[dataObj.colCount - 1])
plot(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1], label="base")
legend(numpoints=1, loc="upper left")

# FIRST DERIVATIVE
d1 = Series()
d1.DbyDX(dataObj.columns[0], dataObj.columns[dataObj.colCount - 1])
axD1 = subplot(312, sharex=axBase)
plot(d1.x, d1.y, label="d/dt")
legend(numpoints=1, loc="upper left")

#SECOND DERIVATIVE
d2 = Series()
d2.DbyDX(d1.x, d1.y)
axD2 = subplot(313, sharex=axBase)
plot(d2.x, d2.y, label="d2/dt2")
legend(numpoints=1, loc="upper left")

#xlabel(dataObj.colLabels[0])

show()