Re: [Tutor] HTML Parsing

2008-04-22 Thread Stephen Nelson-Smith
Hello,

>  For data this predictable, simple regex matching will probably work fine.

I thought that too...

Anyway - here's what I've come up with:

#!/usr/bin/python

import urllib, sgmllib, re

mod_status = urllib.urlopen("http://10.1.2.201/server-status";)
status_info = mod_status.read()
mod_status.close()

class StatusParser(sgmllib.SGMLParser):
def parse(self, string):
self.feed(string)
self.close()

def __init__(self, verbose=0):
sgmllib.SGMLParser.__init__(self, verbose)
self.information = []
self.inside_dt_element = False

def start_dt(self, attributes):
self.inside_dt_element = True

def end_dt(self):
self.inside_dt_element = False

def handle_data(self, data):
if self.inside_dt_element:
self.information.append(data)

def get_data(self):
return self.information


status_parser = StatusParser()
status_parser.parse(status_info)

rps_pattern = re.compile( '(\d+\.\d+) requests/sec' )
connections_pattern = re.compile( '(\d+) requests\D*(\d+) idle.*' )

for line in status_parser.get_data():
rps_match = rps_pattern.search( line )
connections_match =  connections_pattern.search( line )
if rps_match:
rps = float(rps_match.group(1))
elif connections_match:
connections = int(connections_match.group(1)) +
int(connections_match.group(2))

rps_threshold = 10
connections_threshold = 100

if rps > rps_threshold:
print "CRITICAL: %s Requests per second" % rps
else:
print "OK: %s Requests per second" % rps

if connections > connections_threshold:
print "CRITICAL: %s Simultaneous Connections" % connections
else:
print "OK: %s Simultaneous Connections" % connections

Comments and criticism please.

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


Re: [Tutor] HTML Parsing

2008-04-22 Thread Kent Johnson

Stephen Nelson-Smith wrote:

Comments and criticism please.


The SGML parser seems like overkill. Can't you just apply the regexes 
directly to status_info?


If the format may change, or you need to interpret entities, etc then 
the parser is helpful. In this case I don't see how it is needed.


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


[Tutor] Sending Mail

2008-04-22 Thread Stephen Nelson-Smith
smtpserver = 'relay.clara.net'

RECIPIENTS = ['[EMAIL PROTECTED]']
SENDER = '[EMAIL PROTECTED]'
message = """Subject: HTTPD ALERT: %s requests %s connections
Please investigate ASAP.""" % (rps, connections)

session = smtplib.SMTP(smtpserver)
smtpresult = session.sendmail(SENDER, RECIPIENTS, message)
if smtpresult:
errstr = ""
for recip in smtpresult.keys():
errstr = """Could not delivery mail to: %s

Server said: %s
%s

%s""" % (recip, smtpresult[recip][0], smtpresult[recip][1], errstr)
raise smtplib.SMTPException, errstr

This sends emails

But gmail says it came from "unknown sender"

I see an envelope-from in the headers.

What am I missing?

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


Re: [Tutor] Sending Mail

2008-04-22 Thread linuxian iandsd
i can send email w/ no problem using this :

import smtplib
smtp = smtplib.SMTP()
smtp.connect('smtp_server_here')
smtp.login('user_name', 'password')
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.quit()
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sending Mail

2008-04-22 Thread Kent Johnson
On Tue, Apr 22, 2008 at 6:06 AM, Stephen Nelson-Smith <[EMAIL PROTECTED]>
wrote:

> smtpserver = 'relay.clara.net'
>
> RECIPIENTS = ['[EMAIL PROTECTED]']
> SENDER = '[EMAIL PROTECTED]'
> message = """Subject: HTTPD ALERT: %s requests %s connections
> Please investigate ASAP.""" % (rps, connections)
>
> This sends emails
>
> But gmail says it came from "unknown sender"


You have to include the From and To headers in the message body as well as
passing them to sendmail(). See the example here:
http://docs.python.org/lib/SMTP-example.html

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


Re: [Tutor] Little problem with math module

2008-04-22 Thread Andreas Kostyrka
Hmmm, the power function has basically two definitions:

a ** n

for integer n, pow is defined for all a in R.
for real n, pow is defined only for non-negative a.

If you think how a**x is derived (as a generalization from a**(p/q)),
that makes sense.

Basically, a ** (p/q), p > 0, q > 0, gcd(p, q) = 1, is defined as the
q-th root of a ** p. Now for all even q, there is no result in R, only q
results in C.

Now, trying to remember my highschool math, a**x, x in R is defined
basically by interpolation of the the a**x, x in Q function. As a**x
with x in Q is only defined as a real function for a >= 0, so is a**x, x
in R.

In practice a ** x can be also derived from the exp and ln functions:
(which are basically exp(x) = e ** x, and ln is the logarithm to the
base e, funnily, the math module calls that log, which is often used by
mathematicians for the logarithm to base 10, at least in German)

math.exp(x * math.log(a)) == a ** x

(this can be expressed with any base, but for deeper reasons it's
usually e=2.7182... that is used for a base)

Ok, hope that I didn't put my foot in my mouth, considering that high
school is over a decade in that past for me ;)
At least it's probably offtopic for the python tutor list :)

Andreas



Am Montag, den 21.04.2008, 17:50 + schrieb ALAN GAULD:
> On Mon, Apr 21, 2008 at 12:07 AM, Alan Gauld
> <[EMAIL PROTECTED]> wrote:
> 
> 
> >>> pow(-20, 0.333)
> Traceback (most recent call last):
>  File "", line 1, in 
> ValueError: negative number cannot be raised to a fractional power
> >>> -20**0.333
> -2.7144173455393048
> >>>
> 
> 
> I think you're confusing the order of operations.
> 
> math.pow(-20, (1.0/3.0)) and -20**(1.0/3.0) are not equivalent
> 
> Whereas, as john mentioned, -20**(1.0/3.0) is actually
> -(20**(1.0/3.0)), math.pow(-20, (1.0/3.0)) is (-20)**(1.0/3.0)
> 
> 
> Yes, quite correct, I posted my reply before the others had 
> posted theirs although it showed up afterwards(at least on gmane)
>  
> > exponentiation has higher precedence over positive, negative.
>  
> Yep, I hadn't realized that, although it does make sense when you
> think about it :-)
> 
> > Note that math.pow is unrelated to the builtin power operator 
> > and the result of math.pow(0.0, -2.0) will vary by platform. 
>  
> This is interesting, I had assumed that pow simply called **.
> Does anyone know why they made it different?
>  
> Alan G.
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor


signature.asc
Description: Dies ist ein digital signierter Nachrichtenteil
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] XMLRPC

2008-04-22 Thread Spencer Parker
I am looking for a way to run commands remotely on another computer.  I was
thinking of doing this with XMLRPC.  All the machines I have are behind a
private network so I don't have the security implications of them being
public.  I need to run actual system level commands on the box themselves.
What I need it to do is accept a certain level of parameters and the run a
command according to that.  Is this even possible?

-- 
Spencer Parker
___

"if you can't go to heaven, may you at least die in Ireland."

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


Re: [Tutor] XMLRPC

2008-04-22 Thread linuxian iandsd
>
> looking for a way to run commands remotely on another computer.
>

I can run pretty much complicated commands on remote servers using only ssh.
I also run programs on remote server (when inside lan) using cgi module,
works perfectly.

maybe if you describe your problem i can provide you with some code.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XMLRPC

2008-04-22 Thread Spencer Parker
I am just trying to automate series of commands while keeping a low security
profile.  Most of the stuff we do now is through SSH and Pyexpect.  I would
like to see if there is a way of just triggering commands without requiring
us to SSH into the server to get them to run.  We currently ssh into a box
and use expect to trigger a script that sets up Xen Virtual machines.  All I
really need it to do is hit the box and trigger a command...then give me the
status once its done.  The script does this now, but only as a log file in
the same server.

On Tue, Apr 22, 2008 at 12:13 PM, linuxian iandsd <[EMAIL PROTECTED]>
wrote:

> looking for a way to run commands remotely on another computer.
> >
>
> I can run pretty much complicated commands on remote servers using only
> ssh.
> I also run programs on remote server (when inside lan) using cgi module,
> works perfectly.
>
> maybe if you describe your problem i can provide you with some code.
>
>


-- 
Spencer Parker
___

"if you can't go to heaven, may you at least die in Ireland."

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


[Tutor] mod python

2008-04-22 Thread SwartMumba snake
Hello Python Mailing ListI am trying to set up mod python 3.3.1. I have python 2.5.1, apache 2.2 server, and my os is Vista. The problem is that after I install mod python, there is no mod_python.so in the apache modules directory. Thus the "LoadModule python_module modules/mod_python.so" call does not work. What should I do.Thanks.



  Be a better friend, newshound, and 
know-it-all with Yahoo! Mobile.  Try it now.

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


Re: [Tutor] mod python

2008-04-22 Thread Carlos Daniel Ruvalcaba Valenzuela
Hello, I think this is a problem with mod_python and Apache
configuration, you should checkout mod_python configuration for
Apache+Windows.

Most likely your problem is the mod_python library file, use this
instead (more appropiate for windows):

"LoadModule python_module modules/mod_python.dll"

I suggest you to consult the Apache configuration documentation on Windows.

Regards,
Carlos

On Tue, Apr 22, 2008 at 4:18 PM, SwartMumba snake <[EMAIL PROTECTED]> wrote:
> Hello Python Mailing List
>
> I am trying to set up mod python 3.3.1. I have python 2.5.1, apache 2.2
> server, and my os is Vista. The problem is that after I install mod python,
> there is no mod_python.so in the apache modules directory. Thus the
> "LoadModule python_module modules/mod_python.so" call does not work.
>
> What should I do.
>
> Thanks.
>
>  
> Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it
> now.
> ___
>  Tutor maillist  -  Tutor@python.org
>  http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] knowing when a list is updated by a thread

2008-04-22 Thread Vaibhav.bhawsar
i have this code to print every new element in a list only when the list
length changes (while the list is updated by a thread running elsewhere)...I
was wondering if there is a pythonic way to do this? how does one know when
there is a new element in the list?

prevlength = 0
while geoCode.running:
places = geoCode.getResults() #returns a list with most up to date
elements..the list grows as the thread that updates it
if len(places) > prevlength:
print places[prevlength]
prevlength = len(places)


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