programing

플라스크 요청 URL의 다른 부분은 어떻게 얻을 수 있나요?

goodjava 2022. 11. 11. 23:33

플라스크 요청 URL의 다른 부분은 어떻게 얻을 수 있나요?

요청이 다음에서 온 것인지 탐지하고 싶다.localhost:5000또는foo.herokuapp.com호스트 및 요청된 경로.플라스크 요청에 대한 이 정보는 어떻게 얻을 수 있습니까?

URL 은, 다음의 몇개의 필드에서 확인할 수 있습니다.

응용 프로그램이 다음 응용 프로그램 루트에서 수신되고 있다고 가정합니다.

http://www.example.com/myapplication

또, 유저는 다음의 URI 를 요구합니다.

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

이 경우 위의 Atribute 값은 다음과 같습니다.

    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/

적절한 분할을 통해 호스트 부분을 쉽게 추출할 수 있습니다.

예를 들어 다음과 같습니다.

from flask import request

@app.route('/')
def index():
    return request.base_url

또 다른 예:

요청:

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

그 후, 다음과 같이 합니다.

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

다음을 시도해 보십시오.

request.url 

로컬 호스트에서도 항상 동작합니다(그냥 동작했습니다).

Python을 사용하는 경우 요청 개체를 탐색하여 다음을 제안합니다.

dir(request)

오브젝트는 메서드 dict를 지원하므로 다음과 같습니다.

request.__dict__

인쇄하거나 저장할 수 있습니다.플라스크에 404개의 코드를 기록하는 데 사용합니다.

@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')

언급URL : https://stackoverflow.com/questions/15974730/how-do-i-get-the-different-parts-of-a-flask-requests-url