我正在我所在大学的服务器/群集上开发一个Web应用程序,它包含我的所有代码,我想在后端运行。
我将首先发布我的一般问题,然后是我得到的详细代码和错误消息。我的主要问题是,当我通常在我的计算机上本地开发任何使用python和微框架或框架(如flask或django)的web应用程序时,我访问的浏览器上的地址,以确保一切正在运行:http://0.0.0.0:5000,或者localhost周围的东西。但是,当我对服务器/群集进行搜索时,我的新地址是什么?假设我ssh到user@cluster1.uni.ece.edu。我是否可以通过转到http://cluster1.uni.ece.edu来查看我网站的输出?
详细规格:
我正在运行的代码应该允许任何人通过网络浏览器将图像从客户端上传到服务器。
import os
from flask import Flask, render_template, request, redirect, url_for, send_from_directory
from werkzeug import secure_filename
# Initialize the Flask application
app = Flask(__name__)
# This will be th path to the upload directory
app.config['UPLOAD_FOLDER'] = 'uploads/'
# These are the extension that we are accepting to be uploaded
app.config['ALLOWED_EXTENSIONS'] = set(['png','jpg','jpeg'])
# For a given file, return whether it's an allowed type or not
def allowed_file(file_name):
return '.' in filename and \
filename.rsplit('.',1)[1] in app.config['ALLOWED_EXTENSIONS']
# This route will show a form to perform an AJAX request
# jQuery is loaded to execute the request and update the
# value of the operation
@app.route('/')
def index():
return render_template('index.html')
#Route that will process the file upload
@app.route('/upload',methods=['POST'])
def upload():
#Get the name of the uploaded file
file = request.files['file']
#Check if the file is one of the allowed types/extensions
if file and allowed_file(file.filename):
#Make the filename safe, remove unsupported chars
filename = secure_filename(file.filename)
#Move the file from the temporal folder to
#the upload folder we setup
file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
#Redirect the user to the uplaoded_file route, which
#will basically show on the browser the uploaded file
return redirect(url_for('uploaded_file',filename=filename))
# This route is expecting a parameter containing the name of a file.
# Then it will locate that file on the upload directory and show it on the
# browser, so if the user uploads an image, that image is going to be shown
# after the upload.
@app.route('/uploads/<filename>')
def uploaded_file(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],filename)
if __name__ == '__main__':
app.run(
host='0.0.0.0',
port=int("80"),
debug=True
)
上面的代码是从另一个网站借用的。无论如何,我得到的错误是这个:
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
Traceback (most recent call last):
File "Demo1.py", line 60, in <module>
debug=True
File "/home/arturo/miniconda/envs/venv1/lib/python2.7/site-packages/flask/app.py", line 772, in run
run_simple(host, port, self, **options)
File "/home/arturo/miniconda/envs/venv1/lib/python2.7/site-packages/werkzeug/serving.py", line 618, in run_simple
test_socket.bind((hostname, port))
File "/home/arturo/miniconda/envs/venv1/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 13] Permission denied
我的一位朋友正在开发一个类似的网站,不知怎的,他设法让它继续工作:http://cluster1.uni.ece.edu:8000/Demo1
答案 0 :(得分:2)
回答你的第一个问题是肯定的。如果您在服务器中运行Web应用程序,您将能够看到网站的输出。默认情况下,应用程序将在端口5000上运行。
对于第二个问题,请检查问题的这个答案:socket.error: [Errno 13] Permission denied。您已将开发服务器的端口更改为80,而不是root用户。像这样运行您的应用程序:
if __name__ == '__main__':
app.run(
host='0.0.0.0',
debug=True
)
您将能够在http://cluster1.uni.ece.edu:5000中看到输出。 如果要在80端口上运行它,则必须具有root访问权限。 如果您要部署它,我建议您使用Apache或Nginx等生产服务器。