================== file tree
"""Make HTML List of Files from a directory."""
import os
for i in os.listdir('./'):
print '
' + \
i.split('.')[0].replace('_', ' ') + ''
================== UDP protocol
The server:
import socket
PORT = 10000
BUFLEN = 512
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
server.bind(('', PORT))
while True:
(message, address) = server.recvfrom(BUFLEN)
print 'Received packet from %s:%d' % (address[0], address[1])
print 'Data: %s' % message
The client (replace "127.0.0.1" by the IP address of the server):
import socket
SERVER_ADDRESS = '127.0.0.1'
SERVER_PORT = 10000
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
for i in range(3):
print 'Sending packet %d' % i
message = 'This is packet %d' % i
client.sendto(message, (SERVER_ADDRESS, SERVER_PORT))
client.close()
===== html
import getpass,string,sys,win32com.client
from win32com.client import DispatchBaseClass
cfgfile=sys.path[0]+'/sqlaerrorlog.cfg'
print
pw = getpass.getpass()
uid = getpass.getuser()
htmfile = open('C:\\Documents and Settings\\All Users\\DESKTOP\\Logs\\SQLErrorLog.htm','w')
htmfile.write('SQL Server Error Logs\n')
for line in open(cfgfile,'r').readlines():
if line[0]<>'#':
servers = string.split(string.strip(line),',')
svr=servers[0]
auth=servers[1]
print '%s (%s)' % (svr,auth)
htmfile.write('\n')
htmfile.write('\n')
htmfile.write('| ' + svr + ' | \n')
htmfile.write('SQL Log Entry | \n')
htmfile.write('
\n')
# .....
htmfile.write('
\n')
htmfile.close()
adoConn = None
xit = raw_input('\nPress Enter...')
===== diff two dictionaries
KEYNOTFOUND = '' # KeyNotFound for dictDiff
def dict_diff(first, second):
""" Return a dict of keys that differ with another config object. If a value is
not found in one fo the configs, it will be represented by KEYNOTFOUND.
@param first: Fist dictionary to diff.
@param second: Second dicationary to diff.
@return diff: Dict of Key => (first.val, second.val)
"""
diff = {}
# Check all keys in first dict
for key in first.keys():
if (not second.has_key(key)):
diff[key] = (first[key], KEYNOTFOUND)
elif (first[key] != second[key]):
diff[key] = (first[key], second[key])
# Check all keys in second dict to find missing
for key in second.keys():
if (not first.has_key(key)):
diff[key] = (KEYNOTFOUND, second[key])
return diff
===== F --> C conversion
def f2c(f):
c = (f-32)*5.0/9.0
print '%.1f F = %.1f C\n' % (f, c)
def c2f(c):
f = c * 9/5 + 32
print '%.1f C = %.1f F\n' % (c, f)
===== create SHA Hashes of a Directory
import sha
import os
def createsha(directory):
"""Walk through a Directory creating Sha Hashes of each file path"""
for root, dirs, files in os.walk(str(directory)):
for name in files:
filepath = os.path.join(root, name)
value = sha.new(open(filepath, 'rb').read()).hexdigest()
print value, '*%s' % filepath