简体   繁体   中英

Calling a function between quotation marks

I'm a beginner in programming and I'm currently trying to call a function between quotation marks.

Here, more precisely, I would like to replace the name in \\"name\\": \\"name\\", with 88. How would I be able to do that with all those quotation marks?

Thanks in advance for your help

import requests

url = "https://api.multiloginapp.com/v1/profile/create"

def descr():
    return 88

querystring = {"token":"xxxx"}

payload = "{\n\t\"generateZeroFingerprintsData\": true,\n    \"name\": \"name\",\n    \"OS\": \"MacOS\",\n    \"platform\": \"MacIntel\",\n    \"browserType\": \"mimic\",\n    \"proxyHost\": \"0.0.0.0\",\n    \"proxyPort\": 0,\n    \"proxyIpValidation\": false,\n    \"proxyType\": \"socks5\",\n    \"maskFonts\": true,\n    \"disablePlugins\": true,\n    \"disableWebrtcPlugin\": true,\n    \"disableFlashPlugin\": true,\n    \"canvasDefType\": \"noise\",\n    \"hardwareConcurrency\": 2,\n    \"langHdr\": \"en-US,en;q=0.8\",\n    \"timeZone\": \"US/Eastern\",\n    \"audio\": {\n        \"noise\": true\n    },\n    \"geolocation\": {\n        \"permitType\": \"block\"\n    },\n    \"mediaDevices\": {\n        \"audioInputs\": 1,\n        \"audioOutputs\": 1,\n        \"videoInputs\": 1\n    },\n    \"webgl\": {\n        \"noise\": true\n    },\n    \"webRtc\": {\n        \"type\": \"block\"\n    },\n    \"shared\": false\n\t},\n}"
headers = {
    'Content-Type': "application/json",
    'Cache-Control': "no-cache",
    'Postman-Token': "xxx
    }

response = requests.request("POST", url, data=payload, headers=headers, params=querystring)

print(response.text)

The way to do what you're directly asking how to do here is to use string formatting. For example, using printf-style formatting , 1 instead of this:

payload = " … \"name\": \"name\" … "

… do this:

template = " … \"name\": \"%s\" … "
payload = template % (descr(),)

While we're at it, if you put the payload in single quotes, you wouldn't need to escape every " . Even better, if you put it in triple quotes, you wouldn't need to escape all the newlines. See the tutorial section on Strings for details:

template = """{
    \t"generateZeroFingerprintsData": true,
    \t"name": "%s",
    # …
}"""

But you can make things a whole lot easier by not doing any of this. You're trying to build a JSON text representing a dictionary. The easy way to do that is to just create the dictionary and JSON-ify it :

payloadobj = {
    "generateZeroFingerprintsData": True,
    "name": descr(),
    # …
}
payload = json.dumps(payloadobj)

Or, even better, let requests do it for you. Instead of passing data= with a string, pass it json= with a dict, and it automatically JSON-ifies it, and also sets the Content-Type header for you :

payload = {
    "generateZeroFingerprintsData": True,
    "name": name,
    # …
}
# …

response = requests.post(url, json=payload, headers=headers, params=querystring)

(Also, notice that, instead of calling the generic request function and passing POST , which you really only want to do for custom HTTP verbs that requests doesn't know about , I used the simpler post function, as shown in the quickstart docs .)


1. In general, the newer braces-based formatting is simpler. Especially in 3.6+, where you can literally call a function inside quotes, just by wrapping it in braces inside an f-string, like `spam = f'eggs = {eggs()}'. But when the string you're trying to create is full of literal braces, but isn't full of literal percent characters, often that's an exception than the "in general".

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