简体   繁体   中英

Execute cURLs command line in Python

I generated my Python file via PHP.

import os

#state = 6f28bbc4-623a-4424-86df-76e10e82169d
#nonce = 6ff7995b-8425-4a66-bb9c-1671557e5778

cmd = 'curl -c session_cookies.txt "https://login.uat.site.be/openid/oauth/authorize?client_id=site&state=6f28bbc4-623a-4424-86df-76e10e82169d&nonce6ff7995b-8425-4a66-bb9c-1671557e5778&response_type=code&claims=%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D" 2>/dev/null| curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d 'j_username=manager-sitelogin@gmail.com&j_password=site1' "https://login.uat.site.be/openid/login.do" 2>&1 >/dev/null | grep Location | grep code= '

os.system(cmd)

I kept getting SyntaxError: invalid syntax . It has so many single quotes inside the double quotes, I am not sure what I did wrong. I've tried to use \\' to escape it already.

You can see the problem directly when you look at the colors in your block code.

j_username=manager-sitelogin@gmail... is black while the rest of the string is red, because ' before j_username closes the string prematurely.

You can use """string""" syntax, as in this documentation :

cmd = """curl -c session_cookies.txt "https://login.uat.site.be/openid/oauth/authorize?client_id=benu&state=6f28bbc4-623a-4424-86df-76e10e82169d&nonce6ff7995b-8425-4a66-bb9c-1671557e5778&response_type=code&claims=%7B%22id_token%22%3A%7B%22http%3A%2F%2Fsite.be%2Fclaims%2Froles%22%3Anull%7D%7D" 2>/dev/null| curl -v -b session_cookies.txt -L -H "Content-Type: application/x-www-form-urlencoded" -v -d 'j_username=manager-sitelogin@gmail.com&j_password=site1' "https://login.uat.site.be/openid/login.do" 2>&1 >/dev/null | grep Location | grep code= """

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM