trlaing
Posts: 95
Joined: Sat Jul 19, 2014 9:43 pm

Run curl from Python script

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 [email protected]$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 [email protected]"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!

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Run curl from Python script

Thu Jun 04, 2015 10:22 am

Have you looked at pycurl?

http://pycurl.sourceforge.net/

trlaing
Posts: 95
Joined: Sat Jul 19, 2014 9:43 pm

Re: Run curl from Python script

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?

trlaing
Posts: 95
Joined: Sat Jul 19, 2014 9:43 pm

Re: Run curl from Python script

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.

User avatar
kusti8
Posts: 3441
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Run curl from Python script

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.

ame
Posts: 3172
Joined: Sat Aug 18, 2012 1:21 am
Location: Korea

Re: Run curl from Python script

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.

pythondev
Posts: 1
Joined: Thu Jan 10, 2019 5:17 am

Re: Run curl from Python script

Thu Jan 10, 2019 5:20 am

But how can I get the value of the curl to python?? for e.g.

ip = subprocess.call(['curl','checkip.amazonaws.com'])

I want to put into this variable but it just prints out to the screen

Andyroo
Posts: 2585
Joined: Sat Jun 16, 2018 12:49 am
Location: Lincs U.K.

Re: Run curl from Python script

Thu Jan 10, 2019 12:28 pm

pythondev wrote:
Thu Jan 10, 2019 5:20 am
But how can I get the value of the curl to python?? for e.g.

ip = subprocess.call(['curl','checkip.amazonaws.com'])

I want to put into this variable but it just prints out to the screen
I assume you mean the data returned by the curl command - in this case your external IP address?

If so, have a look at the popen method of subprocess - a good intro is at https://stackabuse.com/pythons-os-and-s ... -commands/ as it covers different versions of Python.

You may do best to open a new topic if you get stuck rather than continue on another post that’s been fixed :)
Need Pi spray - these things are breeding in my house...

Return to “Python”