Re: [Tutor] cgi script to start another process in background

2009-02-26 Thread Jay Deiman

Ravi Kondamuru wrote:

Hi,

I am trying to write a python cgi script, that invokes another process 
and exists.

Using the subprocess documentation on NO_WAIT, I am not having much success:

pid = os.spawnlp(os.P_NOWAIT, "/bin/mycmd", "mycmd", "myarg")
==>
pid = Popen(["/bin/mycmd", "myarg"]).pid

The script seems to wait for the new process to exit before returning to 
the user.


I tried doing the double-fork approach discussed here:
http://code.activestate.com/recipes/66012/


Are you on some kind of Unix box here?  The fork approach should
definitely work for you.  An even simpler example, which should still
work for you, is here:


import os , time , sys

print 'hello before the fork'

if os.fork():
print 'parent exiting'
sys.exit()

print 'hello from child: %d' % os.getpid()
time.sleep(30)
os._exit(0)


You should be able to verify that you have that process still running
after the main process exits.

--
Jay Deiman

\033:wq!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] passig parameter with CGI...

2009-02-26 Thread Jay Deiman

Spencer Parker wrote:
I am looking for a little instruction on how one would process a set of 
parameters being sent to it through CGI.  I have a script that sends 
info to another script that lives on another server.  The second script 
would then process the information that is passed to it through a 
parameters list in a URL.  It would then trigger the script to do some 
things via XMLRPC.  I have the portion that connects to the XMLRPC 
server, but not the 1st part.  I can send it a list of command line 
parameters, but the spec has changed.  We want to do it with just curl 
and send it parameters with a URL string.  Not really looking for direct 
code...just some ideas on how this is done for the most part.  I looked 
around, but nothing really fit what I wanted...mostly form 
processing...thanks!


I assume the receiving script is going to be a cgi or something like
that.  It sounds like all you want to do here is construct a GET request
to send to a web server.

=
import urllib

options = {'optone': '1' , 'opttwo': '2' , 'optthree': '3'}
url = 'http://www.example.com/path/to/script.cgi?%s' % \
urllib.urlencode(options)
u = urllib.urlopen(url)
# Here you can read the response if needs be
response = u.read()
u.close()
=

That's about it.

--
Jay Deiman

\033:wq!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] concatenating files

2009-02-27 Thread Jay Deiman

Bala subramanian wrote:

Hai,

I have file1.dat,file2.dat...file 300.dat  in one directory. I want to 
concatenate all the files in a single file  (total.dat) with a string 
"END" separating the file contents.

my total.dat should be
file1.dat contents
END
file2.dat contents
END


file300.dat.

now i have another 400 such *.dat files in another directory whose 
contents i hve to append to "total.dat", how can i do this task. i need 
to do something like, updating the file total.dat without overwritting it.


Thanks,
Bala


This should about do it:

-

#!/usr/bin/env python

import sys , os , glob

startDir = ''
totalFile = '/path/to/total.dat'

if len(sys.argv) > 1 and os.path.isdir(sys.argv[1]):
startDir = sys.argv[1]
else:
print 'Usage: %s ' % os.path.basename(sys.argv[0])
sys.exit(1)

tfh = open(totalFile , 'a')
for f in glob.glob(os.path.join(startDir , '*.dat')):
tfh.write('%s contents\n' % f)
tfh.write(open(f).read())
tfh.write('\nEND\n')
tfh.close()

-

All you have to do is set "totalFile" to your main output file and then 
call the script as "scriptname.py /path/to/datfile/directory".


--
Jay Deiman

\033:wq!
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] rrdtool examples.

2008-12-08 Thread Jay Deiman
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Jeremiah Jester wrote:
> Is anyone on here using the python-rrdtool module for graphing and
> analysis? If so, do you have some sample scripts you could show me.
> There doesn't seem to be a lot out there as far as real world python
> examples.
> 
> Thanks,
> JJ

Actually, I was just playing around with the rrdtool library for python
last week.  It turns out that you basically just use the command line
options as options for the different method calls (like create()).  All
you really have to do is check out the man pages for rrdtool to have all
the documentation you need for the rrdtool python module.  You literally
pass in the command line opts just as they would appear on the command
line.  Here is a quick little script that I whipped up for playing purposes:
- 

#!/usr/bin/env python

import rrdtool , time , random

stime = int(time.time()) - 5 * 86400
dpoints = 1000
etime = stime + (dpoints * 300)
fname = 'test.rrd'
gfname = 'test.png'

rrdtool.create('test.rrd' ,
'--start' , str(stime) ,
'DS:speed:COUNTER:600:U:U' ,
'RRA:AVERAGE:0.5:1:576' ,
'RRA:AVERAGE:0.5:6:336'
)

ctime = stime
cmiles = 0
for i in xrange(dpoints):
bump = random.randint(1 , 20)
cmiles += bump
ctime += 300
rrdtool.update(fname , '%d:%d' % (ctime , cmiles))

rrdtool.graph(gfname ,
'--start' , str(etime - (24 * 3600)) ,
'--end' , str(etime) ,
'--vertical-label' , 'Speed m/h' ,
'--imgformat' , 'PNG' ,
'--title' , 'Speeds' ,
'--lower-limit' , '0' ,
'DEF:myspeed=%s:speed:AVERAGE' % fname ,
'CDEF:mph=myspeed,3600,*' ,
'VDEF:msmax=mph,MAXIMUM' ,
'VDEF:msavg=mph,AVERAGE' ,
'VDEF:msmin=mph,MINIMUM' ,
'VDEF:mspct=mph,95,PERCENT' ,
'LINE1:mph#FF:My Speed' ,
r'GPRINT:msmax:Max\: %6.1lf mph' ,
r'GPRINT:msavg:Avg\: %6.1lf mph' ,
r'GPRINT:msmin:Min\: %6.1lf mph\l' ,
r'GPRINT:mspct:95th Perc\: %6.1lf mph\l'
)

- 

That, coupled with the rrdtool man pages (which are very good, complete
with examples) should be enough to get you started.

- --
Jay Deiman

\033:wq!
-BEGIN PGP SIGNATURE-
Version: GnuPG v2.0.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkk9YNwACgkQQ0lr+ZVKSBiWTQCgoBuzQVeRHBlrJ7GONQAL0RFT
qOwAn3cnbZot0q1qGf6mOFHS8QgQc53o
=h7CZ
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor