• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

도메인 바로 뒤에 txt 파일을 주소로하는 파일을 올리고 싶은데, 잘 안되네요..

20.04.14 13:22 작성 조회수 417

1

도메인.com/ads.txt

이런식으로 텍스트 파일이 바로 도메인 뒤에 위치하도록 업로드 하고 싶은데,

도메인.com/ads.txt 접속을 해보면 이렇게 에러 메시지가 발생합니다.

mimetype='text/plain'을 사용해서 코드를 작성해봤는데,

주소 인식을 못하더라구요.

남박사님 이런 경우에는 어떻게 코드를 수정해야 하나요?

너무 궁금합니다.

답변 2

·

답변을 작성해보세요.

0

from flask import Flask
from flask import send_from_directory
from flask import request

app = Flask(__name__)

@app.route("/robots.txt")
@app.route("/abc.txt")
def index():
    print("Root:{}".format(app.root_path))
    print("Request.path:{}".format(request.path))
    return send_from_directory(app.root_path, request.path[1:])


if __name__ == "__main__":
    app.run(debug=True)

제가 테스트 해본 코드에서는 문제없이 동작하는 걸 확인했습니다.

/robots.txt, /ads.txt 파일은 send_from_directory 로 전송하기 위해서 해당 py 파일이 위치한 경로에 robots.txt, ads.txt 가 존재해야 합니다. (static 폴더와 무관합니다)

0

yoon님의 프로필

yoon

질문자

2020.04.14

남박사님, 질문올리고 나서도 고민하고 적용시키길 반복해서 결국 결과적으로는 해결했습니다!^^

그런데 혹시 제가 한 방식이 괜찬은 건기 한번 봐주실 수 있나요?

app = Flask(__name__, static_folder='static')

@app.route('/ads.txt')
def root_directory():

    return send_from_directory(app.root_path, request.path[1:])

이렇게 적용시켰는데,

처음에는 app = Flask(__name__, static_folder='static', root_path='/')

이렇게 적용시키니까 도메인.com/ads.txt 부분은 제대로 반영이 되는데,

index.html이 작동을 안하더라구요.

혹시나 해서 root_path='/' 부분을 빼고 적용시키니까, 정상 작동 합니다^^

혹시 이렇게 적용해도 큰 무리는 없는건가요?

그리고 동일한 방법으로 도메인 뒤에 robots.txt를 위치시키려고 하는데,

아래와 같이 코드를 넣어봤는데,

app = Flask(__name__, static_folder='static')

@app.route('/robots.txt')
@app.route('/ads.txt')
def root_directory():

    return send_from_directory(app.root_path, request.path[1:])

ads.txt만 정상적으로 동작하고 robots.txt는 아래 오류가 발생하더라구요.

Not Found. The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again. 

어떻게 하면 ads.txt, robots.txt 모두 적용 시킬 수 있을까요?