2

I'm trying to build a simple URL shortener in Python. Save the URLs is easy using a GET request (with the cgi.FieldStorage() method), something like:

http://example.com/shortener.py?url=http://otherwebsite.com/

But how could I get the entire URL when someone try to access the shortened address? Like:

http://example.com/urlcode

I need to deal with the URL as a string and extract only the "urlcode" after the slash.

Edit: I believe that the question was not explained very well. The problem is not parse the URL, is how do I get the URL some user typed or clicked into his browser? All the 404 error requests will be redirected to "index.py". Then I need to deal with what argument caused the 404 error and get the full URL associated to the shortened code.

The question is: how can I read the current URL inside the Python script?

0

2 Answers 2

1

Save the url in a dictionary, where "urlcode" is the key and the url "http://otherwebsite.com/" is the value. Then, when the shortened url http://example.com/urlcode is accessed, lookup the url that has the key "urlcode" in the dictionary.

Sign up to request clarification or add additional context in comments.

1 Comment

That's exactly what I want to do: how can I save the URL some user typed or clicked into his browser to a dictionary? How the script "index.py" or "shortener.py" will read it?
0

Save the long URLs alongwith their shortened codes in a database and then every time you get a short code, just extract the corresponding long URL and redirect to that URL.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.