Page 1 of 1

From AppleScript curl to Python requests...away we go...

PostPosted: Sat Oct 09, 2021 10:06 am
by changeagent
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 8) .

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)

Re: From AppleScript curl to Python requests...away we go..

PostPosted: Sat Oct 09, 2021 3:31 pm
by jay (support)
The requests library is for HTTP communication, not FTP. You'll want to look up the ftplib python library to do FTP commands. Alternately, you can use subprocess library to run the curl command similarly to how you're running it via AppleScript.

Re: From AppleScript curl to Python requests...away we go..

PostPosted: Sat Oct 09, 2021 3:38 pm
by FlyingDiver
Or you can use my FTP plugin.


Sent from my iPhone using Tapatalk

Re: From AppleScript curl to Python requests...away we go..

PostPosted: Sat Oct 09, 2021 8:40 pm
by changeagent
Thanks for pointing me in the right direction. I've got it running using the ftplib library. Simple script that does the job:

Code: Select all
import ftplib 
ftp = ftplib.FTP("ftp.mapletonfire.com")
ftp.login("user", "password")
localfile='/Transfers/alerts/alertfog/indexalert.html'
remotefile='indexalert.html'
ftp.cwd("/mapletonfire.com/www")
with open(localfile, "rb") as file:
    ftp.storbinary('STOR %s' % remotefile, file)


I'll be checking out the FTP plugin, too.

[MODERATOR NOTE]: put code in code tags so python formatting is maintained.

Re: From AppleScript curl to Python requests...away we go..

PostPosted: Sun Oct 10, 2021 2:45 pm
by berkinet
As long as you are in learning mode, you might want to look at PyCurl
http://pycurl.io/