flask Dynami c Путь к хосту - PullRequest
       0

flask Dynami c Путь к хосту

0 голосов
/ 17 февраля 2020

У меня есть следующие простые flask

шаблоны / форма файла. html

  <html>
      <head>
          <title>Simple file upload using Python Flask</title>
      </head>
      <body>
          <form action="/getSignature" method="post" enctype="multipart/form-data">
              Choose the file: <input type="file" name="photo"/><BR>
                  <input type="submit" value="Get Signature"/>
          </form>
      </body>
  </html>

шаблоны / результат. html

  <html>
  <body>
      <p>Signature for file : {{ variable }}</p>
  </body>
  </html>

app.py

  import os
  from flask import Flask, request, render_template, url_for, redirect
  import rsa
  from binascii import hexlify
  import pickle

  SIGNING_KEY = os.getenv('SIGNING_KEY')

  app = Flask(__name__)

  @app.route("/")
  def fileFrontPage():
      return render_template('fileform.html')

  def _sign_file(private_key_file, filename):
      print("Loading private signing key")
      with open(private_key_file,'r') as fh:
          privkey = rsa.PrivateKey.load_pkcs1(fh.read(), 'PEM')
      print("Signing file")
      with open(filename,'rb') as fh:
          signature = rsa.sign(fh, privkey, "SHA-1")
      signature = hexlify(bytearray(signature)) #Send as byte array
      return signature

  @app.route("/getSignature", methods=['POST'])
  def handleFileUpload():
      if 'photo' in request.files:
          photo = request.files['photo']
          if photo.filename != '':
              filepath = os.path.join('/flask/files', photo.filename)
              photo.save(filepath)
              signature = _sign_file(SIGNING_KEY, filepath)
              os.remove(filepath)
      return render_template('result.html', variable=signature)

  if __name__ == '__main__':
      app.run(host='0.0.0.0', port=5000)

Это прекрасно работает на локальном хосте. Но я подаю его в domain.com/signature через Nginx. Когда я нажимаю «Получить подпись», она переходит на domain.com/getSignature и показывает

405 Method Not Allowed
Code: MethodNotAllowed
Message: The specified method is not allowed against this resource.
Method: POST
ResourceType: OBJECT
RequestId: CAB22446BEAE3B3F
HostId: cYGbt4sjHpFfrp6iBXvuYx4qzk7dkT4BqpNNJYFbeMjz34kYLtnr/RTxp8CsqvEoHTx/fVQmWsE=

, необходимо go на domain.com / signature / getSignature

я пытался добавить app.config["APPLICATION_ROOT"] = "/signature/", но не сработало.

Как изменить хост для маршрута ("/ getSignature")?

1 Ответ

1 голос
/ 17 февраля 2020

Ваше действие формы должно go до Nginx конечной точки, если Nginx передает его на ваш маршрут flask. Исходя из того, что вы сказали Nginx слушает domain.com/signature Дайте этому попытку.

<form action="domain.com/signature/getSignature" method="post" enctype="multipart/form-data">
              Choose the file: <input type="file" name="photo"/><BR>
                  <input type="submit" value="Get Signature"/>
</form>

или

<form action="/signature/getSignature" method="post" enctype="multipart/form-data">
              Choose the file: <input type="file" name="photo"/><BR>
                  <input type="submit" value="Get Signature"/>
</form>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...