229

I want to detect if the request came from the localhost:5000 or foo.herokuapp.com host and what path was requested. How do I get this information about a Flask request?

davidism
  • 121,510
  • 29
  • 395
  • 339
Dogukan Tufekci
  • 2,978
  • 3
  • 17
  • 21

5 Answers5

357

You can examine the url through several Request fields:

Imagine your application is listening on the following application root:

http://www.example.com/myapplication

And a user requests the following URI:

http://www.example.com/myapplication/foo/page.html?x=y

In this case the values of the above mentioned attributes would be the following:

    path             /foo/page.html
    full_path        /foo/page.html?x=y
    script_root      /myapplication
    base_url         http://www.example.com/myapplication/foo/page.html
    url              http://www.example.com/myapplication/foo/page.html?x=y
    url_root         http://www.example.com/myapplication/

You can easily extract the host part with the appropriate splits.

An example of using this:

from flask import request

@app.route('/')
def index():
    return request.base_url
Kevin
  • 74,910
  • 12
  • 133
  • 166
icecrime
  • 74,451
  • 13
  • 99
  • 111
  • 4
    new to Flask, i didn’t know where request object come from and how it works, here it is: http://flask.pocoo.org/docs/0.12/reqcontext/ – Ulysse BN Jan 27 '17 at 23:20
  • 1
    url_root returns `http://www.example.com/` not `http://www.example.com/myapplication/` base_url returns `http://www.example.com/myapplication/` – moto Dec 04 '18 at 21:27
  • my app route is like @app.route('/index//') how can i guest path from request without variables. I expect **/index** in result. Which flask function to use? – user4772933 Nov 28 '19 at 16:11
337

another example:

request:

curl -XGET http://127.0.0.1:5000/alert/dingding/test?x=y

then:

request.method:              GET
request.url:                 http://127.0.0.1:5000/alert/dingding/test?x=y
request.base_url:            http://127.0.0.1:5000/alert/dingding/test
request.url_charset:         utf-8
request.url_root:            http://127.0.0.1:5000/
str(request.url_rule):       /alert/dingding/test
request.host_url:            http://127.0.0.1:5000/
request.host:                127.0.0.1:5000
request.script_root:
request.path:                /alert/dingding/test
request.full_path:           /alert/dingding/test?x=y

request.args:                ImmutableMultiDict([('x', 'y')])
request.args.get('x'):       y
chason
  • 3,534
  • 1
  • 7
  • 7
  • 1
    For people who want to use `request.full_path`, suggest to use `request.environ['RAW_URI']` instead. Because, when the actual full query path is `/alert/dingding/test`, `request.full_path` returns `/alert/dingding/test?`, an extra question mark will be added to the result, which might not be desirable. – AnnieFromTaiwan Nov 24 '20 at 03:59
  • **request.remote_addr** for 127.0.0.1 – PYK Jan 12 '22 at 01:27
  • 2
    what is the different flask "host_url" "root_url" "url_root"? – CS QGB Jul 05 '22 at 10:10
  • request.query_string – anvd Aug 25 '22 at 14:15
14

you should try:

request.url 

It suppose to work always, even on localhost (just did it).

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Ran
  • 165
  • 1
  • 4
2

If you are using Python, I would suggest by exploring the request object:

dir(request)

Since the object support the method dict:

request.__dict__

It can be printed or saved. I use it to log 404 codes in Flask:

@app.errorhandler(404)
def not_found(e):
    with open("./404.csv", "a") as f:
        f.write(f'{datetime.datetime.now()},{request.__dict__}\n')
    return send_file('static/images/Darknet-404-Page-Concept.png', mimetype='image/png')
Antonio
  • 307
  • 2
  • 11
1

if user requests the following URI:

http://www.example.com/myapplication/foo/page.html?x=y

and the user wants y

you can use

request.args.get("x")