Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use a variable in python as string query to pass parameter in url

Tags:

python

http

url

I want to use query strings to pass values through a URL. For example:

http://127.0.0.1:5000/data?key=xxxx&secret=xxxx

In Python, how can I add the variables to a URL? For example:

key = "xxxx"
secret = "xxxx"
url = "http://127.0.0.1:5000/data?key=[WHAT_GOES_HERE]&secret=[WHAT_GOES_HERE]"
like image 807
xy254 Avatar asked Oct 16 '25 18:10

xy254


1 Answers

The safest way is to do the following:

import urllib

args = {"key": "xxxx", "secret": "yyyy"}
url = "http://127.0.0.1:5000/data?{}".format(urllib.urlencode(args))

You will want to make sure your values are url encoded.

The only characters that are safe to send non-encoded are [0-9a-zA-Z] and $-_.+!*'()

Everything else needs to be encoded.

For additional information read over page 2 of RFC1738

like image 72
sberry Avatar answered Oct 18 '25 10:10

sberry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!