Run curl from Python script


6 posts
by trlaing » Thu Jun 04, 2015 10:16 am
Hi
I am trying to use a Python script to automatically upload a file to eBay FileExchange on a daily basis. I have written the following Python script to check files in a folder named in the format of the date it is to be uploaded (eg. 2015-04-06):
Code: Select all
#!/usr/bin/python
import time, datetime

dateString = '%Y-%m-%d'
datetimeString = '%Y/%m/%d %H:%M:%S'

from os import listdir
from os.path import isfile, join
uploaddate = datetime.datetime.now().strftime(dateString)
print(datetime.datetime.now().strftime(datetimeString) + ": Checking upload folder '" + uploaddate + "' for files...")
onlyfiles = [ f for f in listdir('./upload/' + uploaddate) if isfile(join('./upload/' + uploaddate,f)) ]
print(datetime.datetime.now().strftime(datetimeString) + ': ' + str(len(onlyfiles)) + ' files found:')

for filename in onlyfiles:
        print(filename)
        curl -k https://bulksell.ebay.co.uk/ws/eBayISAPI.dll?FileExchangeUpload -F"token=MYTOKEN" -F file=@$filename


From the command line I can successfully run the following to upload the file:
Code: Select all
curl -k https://bulksell.ebay.co.uk/ws/eBayISAPI.dll?FileExchangeUpload -F"token=MYTOKEN" -F file=@"myfile.csv"


I have replaced my actual token with MYTOKEN for security reasons.
I would like to know how to correctly format the line in my Python script to run the curl command for each file found in the folder. I know that the code I have to run curl is incorrect in my Python script, and have given up messing around with double and single quotes. If anyone knows how to correctly call the curl command in a Python script with variables in the command line please help!
Posts: 70
Joined: Sat Jul 19, 2014 9:43 pm
by ame » Thu Jun 04, 2015 10:22 am
Have you looked at pycurl?

http://pycurl.sourceforge.net/
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea
by trlaing » Thu Jun 04, 2015 10:54 am
Thank you for your quick reply. I was really trying to keep it simple. Can a command line command be called from within a Python script this way?
Posts: 70
Joined: Sat Jul 19, 2014 9:43 pm
by trlaing » Thu Jun 04, 2015 11:34 am
I managed to get it working by using the os.system command.

Add the following line near the top:
import os


to call the command:
os.system("curl -K.........")

I gave up messing around with multiple double quotes for the filename variable - I just made sure the file names had no spaces so I didn't need quotes. Maybe using pycURL would be better coding, but as long as it works I am happy.
Last edited by trlaing on Thu Jun 04, 2015 11:36 am, edited 1 time in total.
Posts: 70
Joined: Sat Jul 19, 2014 9:43 pm
by kusti8 » Thu Jun 04, 2015 11:35 am
trlaing wrote:Thank you for your quick reply. I was really trying to keep it simple. Can a command line command be called from within a Python script this way?


Code: Select all
import subprocess

subprocess.call(['command', 'argument'])
There are 10 types of people: those who understand binary and those who don't.
User avatar
Posts: 3433
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA
by ame » Thu Jun 04, 2015 12:52 pm
Glad you got it sorted. There are often many ways to do the same thing, but in general if it's hard there's probably a module to make it easy.
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea