I writing a python script to run multiple curl commands (I only listed one as an example) that fetch some content from created URLs. However, as shown in the example, the URLs have a backslash which is interpreted, while being executed, as a double backslash. So this is one command I want to run (notice the backslash after "fff"), which runs fine if I copy it and paste it in Linux Ubuntu terminal:
curl https://api.example.com/fff\|2021-06-25--15-11-33/files -H 'Authorization: JWT eykjgjgjgkjgg'
This is my code:
import subprocess import shlex curl= "curl https://api.example.com/fff\|2021-06-25--15-11-33/files -H 'Authorization: JWT eykjgjgjgkjgg'") curlx=shlex.split(curl, posix=False) print(curlx) #the result of the printed URL is a valid url (with single backslash). However, when the command is executed it is with double backslash result= subprocess.run(curlx, stdout=subprocess.PIPE) result.stdout.decode('utf-8') print(result)
but I keep getting this error because, as shown from the result, the command runs with double backslash (after "fff") instead of one:
% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 232 100 232 0 0 163 0 0:00:01 0:00:01 --:--:-- 163 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0CompletedProcess(args=['curl', 'https://api.example.com/fff\\|2021-06-25--15-11-33/files', '-H', "'Authorization: JWT eykjgjgjgkjgg'"], returncode=0, stdout=b'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">\n<title>404 Not Found</title>\n<h1>Not Found</h1>\n<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>\n')
I tried replacing the two backslashes with a single one but that still gets interpreted as two and hence the error keeps showing. I appreciate any help because I have more than 500 curl commands that will take a long time to run manually.
FYI, I'm using Python 3.8 in Pycharm 2021.1 on Ubuntu 20.04.2.0 LTS