class MyGUI:
""" Main class for this application (holds the GUI) """
def __init__ (self):
self.tree = gtk.glade.XML("myGui.glade")
self.tree.signal_autoconnect(self)
def on_mywindow_delete (self, widget, dummy):
gtk.main_quit()
if __name__ == "__main__":
h = MyGUI()
gtk.main()
colormode(HSB)
class Icon(object):
def __init__(self):
self.width = 5
self.height = 5
self.pixels = [random(2) \
for i in xrange(self.width * self.height)]
self._symmetrize()
self.pixelsize = 8
self.fg_col = (random(1.0), random(0.4, 1.0), 0.25)
self.bg_col = ((self.fg_col[0] + 0.2) % 1.0, self.fg_col[1], 1.0)
def paint(self, x_pos=0, y_pos=0):
nostroke()
for y in xrange(self.height):
for x in xrange(self.width):
if self.pixels[x + self.width * y]:
col = self.fg_col
else:
col = self.bg_col
fill(color(col))
rect(x * self.pixelsize + x_pos, y * self.pixelsize + y_pos,
self.pixelsize, self.pixelsize)
def _symmetrize(self):
for y in xrange(self.height):
for x in xrange(int(self.width / 2), self.width):
self.pixels[x + self.width * y] = \
self.pixels[(self.width - (x + 1)) + self.width * y]
class IconGrid(object):
def __init__(self, width, height):
self.width = width
self.height = height
self.icons = [Icon() for x in xrange(self.width) \
for y in xrange(self.height)]
def paint(self):
for y in xrange(self.height):
for x in xrange(self.width):
icon = self.icons[x + y * self.width]
pw = icon.width * (icon.pixelsize + 1)
ph = icon.height * (icon.pixelsize + 1)
icon.paint(x * pw, y * ph)
grid = IconGrid(8, 13)
grid.paint()
import os, fnmatch
def locate(pattern, root=os.curdir):
'''Locate all files matching supplied filename pattern in and below
supplied root directory.'''
for path, dirs, files in os.walk(os.path.abspath(root)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
# Get the UID the application was started with
# which must have been 'root' if you want this process
# to have 'root' like abilities.
privUID = os.geteuid()
# Get the UID that the application should use for
# most of its processing, 'nobody' is usually
# a good choice.
normalUID = pwd.getpwnam( 'nobody' )[2]
def runAsNormal():
"""Switch this process to normal privileges, we shouldn't be able to
do anything naughty in this mode."""
os.seteuid( normalUID )
def runAsPrivileged():
"""Switch to super user privileges, here we can do anything we want."""
os.seteuid( privUID )
# Once program has initialized, drop privileges
runAsNormal()
# ... some normal application code...
# Do do something that requires super user privileges
try:
runAsPrivileged()
# ... do the stuff we need to be super for
finally:
# Switch out of super mode and back to normal
runAsNormal()
#!/usr/bin/env python
"""
appname v1.0.1 Copyright(c) 2008; Me, Inc.
Description of the program
usage: appname [-g config ]
-g config file (default is /usr/local/etc/mcp.conf)
"""
import os, sys, time, string, traceback
def _banner():
"""
Displays a banner announcing what this program is and what it does.
"""
import string
doc = string.split( __doc__, '\n' )
s = doc[1] + '\n' + doc[3]
n = 4
while doc[n][:6] != 'usage:':
s = s + '\n' + doc[n]
n = n + 1
return s + '\n'
def _usage():
"""
Displays the command line arguments for this application.
"""
import string
print _banner()
doc = string.split( __doc__, '\n' )
n = 0
while ( doc[n][:6] != 'usage:' ):
n = n + 1
s = doc[n]
n = n + 1
while ( len(doc[n]) ):
s = s + '\n' + doc[n]
n = n + 1
return s + '\n'
def _processOpts():
# Process command line options
import getopt, sys
try:
# If the option takes a parameter, don't forget to add
# a colon immediately after the parameter.
opts, args = getopt.getopt( sys.argv[1:], "g:", "" )
except:
print _usage()
sys.exit( 1 )
config = 'config.file'
for o, a in opts:
if o == "-g":
config = a
else:
# we don't know this command line option
print _usage()
sys.exit( 1 )
return config
# If this module is being run as the main module
if __name__ == "__main__":
# Get our command line options
config = _processOpts()
import socket
HOST = '127.0.0.1'# Nome simbolico che rappresenta il nodo locale
PORT = 90007 # Porta non privilegiata arbitraria
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(6)
while 1:
conn, addr = s.accept()
data = conn.recv(1024)
if data=='/quit':
print '...FINE CONNESSIONE CON',addr,'...'
conn.close()
if not data: break
print 'Client',addr,'::. \t'+data
risp=raw_input('Server::. \t')
conn.send(risp)
conn.close()