-2

I am new to python, i want to know in python how to receive the URL parameter value? I tried with below code, but

import urllib.parse as urlparse
from urllib.parse import urlencode

url = "http://domainanme.com/api/filename.py?para_name=value"


url_parts = list(urlparse.urlparse(url))

query = dict(urlparse.parse_qsl(url_parts[4]))

Using python i am unable to receive para_name = value But in PHP can easily get URL parameter value using $_GET['para_name'], with out knowing the full URL, but in python how do i know the full URL with params

Note: i want to do using core python without any Python framework

Udhayakumar
  • 261
  • 1
  • 10
  • I do not see the problem, this code returns: `{'para_name': 'value'}` (see: https://onlinegdb.com/cxxsYR_MN ), This `print(query['para_name'])` will also return just `value`. – Luuk Dec 25 '22 at 14:46
  • Yes, but my question is how the python will know the full url with parameter? basically python will respond back based on query parameter value. Value will be dynamic right, Basically python will know only parameter name – Udhayakumar Dec 25 '22 at 14:55
  • This question seems to be a duplicate of [Retrieving parameters from a URL](https://stackoverflow.com/questions/5074803/retrieving-parameters-from-a-url), and when it's not than it should be made clearer. – Luuk Dec 25 '22 at 15:03
  • the question is not duplicate one, without the URL, how to receive the parameter value in python – Udhayakumar Dec 26 '22 at 16:37
  • [How can I read the contents of an URL with Python?](https://stackoverflow.com/questions/15138614/how-can-i-read-the-contents-of-an-url-with-python) ?? – Luuk Dec 26 '22 at 16:47

2 Answers2

1

I hope this will help you

cgi.FieldStorage()

It returns a dictionary with the key as the field and value as its value.

Without any python framework, you can get the URL parameter value

Sample code

import cgi
import cgitb; cgitb.enable() # Optional; for debugging only

print("Content-Type: text/html")

arguments = cgi.FieldStorage()
for i in arguments.keys():

    print(arguments[i].value)

Example: save this file as test.py and execute it in your browser like http://youdomainname.com/test.py?name=john in the browser, you can see the result as a john

Vetrivel
  • 1,149
  • 1
  • 7
  • 16
0

You can try this:

  import urllib.parse as urlparse
  from urllib.parse import urlencode

  url = "http://domainanme.com/api/filename.py?para_name=value"

  url_parts = urlparse.urlparse(url)

  query = urlparse.parse_qs(url_parts.query)

  value = query['para_name']

You can access the query string from the query attribute of the ParseResult object. Then just access the value by key name.

  • Hi, how python knows http://domainanme.com/api/filename.py?para_name=value full URL with params, i hope param value will be dynamic right? – Udhayakumar Dec 25 '22 at 15:02
  • I don't understand the question. Can you make it clearer what you are trying to ask? – Nazere Wright Dec 25 '22 at 15:21
  • I mean in the first place how the python will know the url with dynamic parameter value? because the request will be coming from client right, basically an API call request – Udhayakumar Dec 25 '22 at 15:28
  • Without a framework, you could use the http server module to capture the path from the client sending the request. Then, perform the same operations of parsing the URL. You still need to specify the parameter you wish to access. – Nazere Wright Dec 25 '22 at 16:09