I have a few dozen AppleScript routines I was able to convert to Python. I'm learning to like the language. However, I have one script that worked very well under AppleScript that I've been unable to convert. The Applescript is a simple routine to upload a file using curl:
set this_file to "Macintosh HD:Transfers:alerts:alertoff:indexalert.html"
set ftp_path to ""
set ftp_name to "name"
set ftp_pw to "password"
set ftp_server to "ftp://ftp.mapletonfire.com//mapletonfire.com/www/"
set thisPOSIXfile to quoted form of POSIX path of this_file
set shellscript to "curl -u " & ftp_name & ":" & ftp_pw & " -T " & thisPOSIXfile & " " & ¬
ftp_server & ""
do shell script shellscript
I tried the following variations but can't get any to work. Still trying to figure out the nuances of Python code constructs .
import requests
with open('/Transfers/alerts/alertfog/indexalert.html', 'rb') as f:
requests.post('http://www.mapletonfire.com', files={'/Transfers/alerts/alertfog/indexalert.html': f})
--------------------------
import requests
myfile = '/Transfers/alerts/alertfog/indexalert.html'
url = 'http://www.mapletonfire.com/indexalert.html'
data = {'dir':'/indexalert.html', 'submit':'Submit'}
files = {'file':(myfile, open(myfile, 'rb'))}
requests.post(url, data=data, files=files)
-------------------------
import requests
user_name = "name"
user_pw = "password"
myfile = '/Transfers/alerts/alertfog/indexalert.html'
url = 'http://www.mapletonfire.com/indexalert.html'
payload = open(myfile, 'rb')
headers = {'content-type': 'application/x-www-form-urlencoded'}
requests.post(url, auth=(user_name, user_pw), data=payload, verify=False, headers=headers)