Forums

Request Script PASSES with PA Bash but FAILS with Scheduled Task

Hello!

Great platform - love it!

I have used a script to automatically get images from a public website and send out at a scheduled time. The script runs fine when I run from within Bash environment of PA, but fails on scheduled task. Was wondering if someone could help me with why this may be?

Apologies, the script is rather long - have tried to shorten the best I can whilst still reproducing the error.

# Import modules

from datetime import date, timedelta
import os
import requests
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
import smtplib

# Create a dict of file-name and location on web

file_list = {
"WA_Rainfall.gif" : "http://www.bom.gov.au/fwo/IDW62203.gif",
"SA_Rainfall.gif" : "http://www.bom.gov.au/fwo/IDS65036.gif",
}

# Use Requests to download gif file

for k, url in file_list.items():
    r = requests.get(url, allow_redirects=True)
    with open(k, 'wb') as fd:
        fd.write(r.content)

msg = MIMEMultipart('related')

d={}
i=0
for k,v in file_list.items():
    i+=1
    fp = open(k, 'rb')
    d["string{0}".format(i)] = MIMEImage(fp.read())
    fp.close()
    d["string{0}".format(i)].add_header('Content-ID', '<{}>'.format(v))
    msg.attach(d["string{0}".format(i)])

fromaddr = 'XXXX@XXXX.com'
toaddr = ['XXXX@XXXXX.com']
subject = 'Evening AU Weather Maps'
msg['From'] = fromaddr
msg['To'] = ", ".join(toaddr)
msg['Subject'] = subject

smtpObj = smtplib.SMTP('smtp.office365.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login("XXXX@XXXX.com","XXXXX")
smtpObj.sendmail(fromaddr, toaddr, msg.as_string().encode('utf-8'))

The script throws the following error in the log:

Traceback (most recent call last): File "/home/dimariyasinghe/TestSnippet.py", line 57, in <module> d["string{0}".format(i)] = MIMEImage(fp.read()) File "/usr/lib/python3.6/email/mime/image.py", line 43, in init raise TypeError('Could not guess image MIME subtype') TypeError: Could not guess image MIME subtype

2018-06-29 01:23:09 -- Completed task, took 5.00 seconds, return code was 1.

Any help greatly appreciated!

Thanks

Dim

Make sure you're running the same version of Python or in the same virtualenv for both. See http://help.pythonanywhere.com/pages/ScheduledTasks/

Hi Glenn - thanks for your help.

Unfortunately that doesn't appear to have helped. For eg.

in Bash, I run: Python3.6 myscript.py

the scheduled task is: python3.6 /home/myusername/myproject/myscript.py

Any other possible issues?

Thanks again!

Dim

That's very odd. Perhaps you could try saving timestamped copies of the downloaded files from your scheduled task runs -- for example by changing

with open(k, 'wb') as fd:
    fd.write(r.content)

to

with open(k, 'wb') as fd:
    fd.write(r.content)
with open("{}-{}".format(k, datetime.utcnow()), 'wb') as fd:
    fd.write(r.content)

...and then take a look at the downloaded files to see if they have different content when it's the scheduled task downloading them to when it's a run in a console?