Select to view content in your preferred language

Checking ArcServer services with phthon, stuck on simple fp

711
6
07-27-2010 09:31 AM
PaulHuffman
Frequent Contributor
I am trying to write a little python script to send me an email to alert me that a troublesome ArcServer service has stopped on a windows server. I've done stuff like this in perl, but I thought I should do this in python because the server already has python 2.5 installed and I need to learn more python.

I found the WService Class http://code.activestate.com/recipes/1158...-services/ right away and it worked great. In just a few lines. I could write something that would stay running until a service stopped, then I can branch to sending the email:

from sys import exit
import time, win32api, win32con, win32service
wa, wc, ws = win32api, win32con, win32service

import WService
iis = WService.WService("IIS Admin")
iis.fetchstatus("stopped")
print "IIS Stopped"

class WService:
**
**


but the email part has given me some problems, making me realize I don't know much about python basics.

I've been trying to test with the simple text message example found around the net: http://docs.python.org/library/email-examples.html

# Import smtplib for the actual sending function
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading. For this example, assume that
# the text file contains only ASCII characters.
fp = open('textfile.txt', 'rb')
# Create a text/plain message
msg = MIMEText(fp.read())
fp.close()

me == "*****@yakama.com"
you == "*****@yakama.com"
msg['Subject'] = 'The contents of %s' % 'textfile.txt'
msg['From'] = me
msg['To'] = you

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP()
s.connect()
s.sendmail(me, [you], msg.as_string())
s.close()

But had some trouble figuring out where all my substitutions would go and what the syntax is supposed to be, like in quotes or not. But now I'm hitting an error at line 9:

  
fp = open('textfile.txt', 'rb')
    IOError: [Errno 2] No such file or directory: 'textfile.txt'


even though I created a textfile.txt in the same directory as the script. Why can't I get open to work? Do I need to set a path?

I looked at the Google App Engine python email but apparently it didn't install for me correctly. After running the msi,

   
from google.appengine.api import mail


in an interactive window can't find the module.
0 Kudos
6 Replies
PaulHuffman
Frequent Contributor
I figured out a way to see what python's default path to the text file is. In interactive window, I opened a new file named mailbody.txt with 'w', the write mode, and dropped a line of text into it. The searched C: for mailbody.txt. Found it in c:\python25. Now I'm using mailbody.txt for the msg. Couldn't figure out how to add a path to the open statement.

Now I'm getting errors returned by line 22 or 23 when it tries to connect to a mail server. 'Connection refused', although one time trying mail.charter.net as the STMP server, I got a message that it didn't permit connections from outside charter.nets IP.  Trying many combinations of email addresses and mail hosts, but haven't hit on the correct combination.
0 Kudos
KevinHibma
Esri Regular Contributor
Paul,

In ArcGIS 10 we have a tool which extracts data, zips it and then emails it.
If you have 10 and navigate to the Server Tools > Data Extraction > Send Email With Zip File Attachment, you can right click and "edit" to see how it works.

If you dont have 10 I've attached the python script which drives the tool.
Its very similar to what you have - though there are some minor differences. Hopefully between it and the script you have you can get your task working.

Goodluck!
0 Kudos
PaulHuffman
Frequent Contributor
I guess I've seen that script, Kevin.  It was a little far off the mark from what I am trying to do, and too complicated to simplify. 

This code worked for me, using gmail as my smtp server. But only when I tried running it from my rented linux web server. Running from my PC, I just get "SMTP AUTH extension not supported by server". Must have issues with the enterprise firewall at my location. I hope I can get it to work on my windows Arc Server that's on a different network.

By the way, here's a neat trick to get your current path in python: http://diveintopython.org/functional_programming/finding_the_path.html
0 Kudos
PaulHuffman
Frequent Contributor
Well, that was a good theory for a short time. It's not a firewall problem because I get the same error
File "C:\Python25\lib\smtplib.py", line 554, in login
    raise SMTPException("SMTP AUTH extension not supported by server.")
SMTPException: SMTP AUTH extension not supported by server.
from windows xp locally and windows server at the other location.  The script runs fine and sends mail from Linux both here at my office and my web server.  Must be something about windows that blocks mail authentication by default.
0 Kudos
PaulHuffman
Frequent Contributor
I don't understand why but adding a second server.ehlo() after server.starttls() seems to be necessary on windows machines.  Gmail seems to be the only mail server that will play this way.  Saw the tip about the second server.ehlo() at http://stackoverflow.com/questions/399129/failing-to-send-email-with-the-python-example/399240#39924..., the one posted by Jataro.

This is what is working for me now on windows:
import smtplib

FROMADDR = "******@gmail.com"
LOGIN    = FROMADDR
PASSWORD = "******"
TOADDRS  = ["******@yakama.com"]
SUBJECT  = "Test from Paule6500 WindowXP"

msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
       % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
msg += "some text from windows xp at Nelson Springs\r\n"

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.ehlo()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg)
server.quit()


I wonder if I can send to multiple email addresses.

The next step is to branch to this section when the service is found to be stopped by the section that uses the WService class.
0 Kudos
PaulHuffman
Frequent Contributor
Well, it's not elegant but this is what I'm going with. Seems to work. Not elegant in that I didn't set up any parameters to input the service name and the email addresses, you have to edit them in.  Not elegant in that there is no branching. No looping back at intervals to keep checking the service. It just starts, then hangs at the fetchstatus line.  If the status of the service ever changes to "stopped", then the script continues to the next lines and sends a email.  It will tie up pythonwin running this way, but you can also run it from a command line.  Taskmanager will show it as it hangs, and doesn't seem to use any CPU.  I suppose there is a way in windows to start it up during system startup.  I'll be able to try it on my troublesome ArcService system next week.
0 Kudos