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

Posted on
Sat Oct 09, 2021 10:06 am
changeagent offline
User avatar
Posts: 23
Joined: Aug 26, 2015

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

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)

Posted on
Sat Oct 09, 2021 3:31 pm
jay (support) offline
Site Admin
User avatar
Posts: 18220
Joined: Mar 19, 2008
Location: Austin, Texas

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

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.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Sat Oct 09, 2021 3:38 pm
FlyingDiver offline
User avatar
Posts: 7221
Joined: Jun 07, 2014
Location: Southwest Florida, USA

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

Or you can use my FTP plugin.


Sent from my iPhone using Tapatalk

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Sat Oct 09, 2021 8:40 pm
changeagent offline
User avatar
Posts: 23
Joined: Aug 26, 2015

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

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.

Posted on
Sun Oct 10, 2021 2:45 pm
berkinet offline
User avatar
Posts: 3290
Joined: Nov 18, 2008
Location: Berkeley, CA, USA & Mougins, France

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

As long as you are in learning mode, you might want to look at PyCurl
http://pycurl.io/

Page 1 of 1

Who is online

Users browsing this forum: Google [Bot] and 4 guests