Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
URL String with parameters
#11
(May-06-2020, 01:32 PM)buran Wrote: please, don't change post content after you get reply
there are many web frameworks - e.g. Django, Flask, bottle, to name a few. We have tutorials for Flask
https://python-forum.io/Thread-Flask-Sta...ent-part-1
https://python-forum.io/Thread-Flask-Wea...pp-Updatet

You should choose a framework, then we may suggest tutorilas

I want to make App without framework ( it is tiny background app).
Reply
#12
I have found this:

 

import os
import sys 
from cgi import parse_qs, escape

def application (environ, start_response):
 
    d = parse_qs(environ['QUERY_STRING'])
 
    age = d.get('age', [''])[0] # Returns the first age value    age = escape(age)    
    hobbies = d.get('hobbies', []) # Returns a list of hobbies
    hobbies = [escape(hobby) for hobby in hobbies]
Reply
#13
(May-09-2020, 02:13 PM)nikoloz Wrote: I want to make App without framework ( it is tiny background app).
To make it very clear you don't want to that and CGI is dead it will be removed.
Python Wrote:cgi is going to be deprecated in Python 3.8 and removed in 3.10
So the modern way to do CGI in Python is the use a smaller web-framework like Flask, Bottle, ect...
Python community has written WSGI(all Python code) to replace CGI(had a lot of problems that could not be fixed).

So eg Flask is layer above WSGI to make all easier connect to web with HTML/CSS than using WGSI directly.
Reply
#14
Thank you for information, python is new language for me; and have no teacher. Big Grin
Reply
#15
Was the question about constructing URL, or downloading content?

Parsing:
# required for parsing
from urllib.parse import urlparse, parse_qs

# required to contruct
from urllib.parse import urlunparse, urlencode

url_str = "http://example.com?param1=a&token=TOKEN_TO_REPLACE&param2=c"



# Parsing the url_str
url = urlparse(url_str)
# Convert the query to a dict
query = parse_qs(url.query)

# later to construct an url, you require following parameters:
scheme, netloc, path, params, query, fragment = url


# Construct the url with a new query
query = {"foo": ["bar", "fizz", "buzz"], "bar": [42]}
query_s = urlencode(query)

new_url = urlunparse((scheme, netloc, path, params, query_s, fragment))
# it's only one parameter which is a tuple with 6 elements
print(new_url)

query_from_url = urlparse("http://example.com?foo=%5B%27bar%27%2C+%27fizz%27%2C+%27buzz%27%5D&bar=%5B42%5D").query
print(parse_qs(query_from_url))
There is another module, called yarl which looks a bit better.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020