Skip to content

Instantly share code, notes, and snippets.

@toshihiroryuu
Last active August 19, 2019 06:00
Show Gist options
  • Save toshihiroryuu/f9ec1a4db67e29b58913cfd0e2589931 to your computer and use it in GitHub Desktop.
Save toshihiroryuu/f9ec1a4db67e29b58913cfd0e2589931 to your computer and use it in GitHub Desktop.
500 Internal Server Error – Analysis
1. [Python] Flask Web App in Azure throws (Internal Server Error)
Firstly, it works fine locally on my computer.
So I tried to deploy my website to azure Web App, it deployed successfully, but it shows 'Internal Server Error' as I try to browse it. Checked logs and didn't find any specific problem. Here's wsgi log:
2016-10-16 16:34:09.476620: Activating venv with executable at D:\home\site\wwwroot\env\Scripts\python.exe
2016-10-16 16:34:09.507951: Getting handler Test_Azure.app
2016-10-16 16:34:10.242244: Activating venv with executable at D:\home\site\wwwroot\env\Scripts\python.exe
2016-10-16 16:34:10.257894: Getting handler Test_Azure.app
2016-10-16 16:34:10.492272: Got handler: <Flask 'Test_Azure'>
2016-10-16 16:34:10.507895: wfastcgi.py will restart when files in D:\home\site\wwwroot\ are changed: .*((\.py)|(\.config))$
2016-10-16 16:34:10.523492: wfastcgi.py 2.1.1 initialized
2016-10-16 16:34:11.054761: Got handler: <Flask 'Test_Azure'>
2016-10-16 16:34:11.070388: wfastcgi.py will restart when files in D:\home\site\wwwroot\ are changed: .*((\.py)|(\.config))$
2016-10-16 16:34:11.070388: wfastcgi.py 2.1.1 initialized
In DetailedError folder it shows this error:
HTTP Error 500.0 - INTERNAL SERVER ERROR
The page cannot be displayed because an internal server error has occurred.
Sol:
try to put try/catch and print out exc_info around the suspicious call. This might yield more error details.
I was able to get more specific error message, the problem was that app.secret_key was not set, even though it is assigned in run_server.py file. Looks like "if __name__ == '__main__':" is not true in deployment environment.
Link : https://social.msdn.microsoft.com/Forums/sqlserver/en-US/d11c2c15-8994-43d7-aa4f-6711d097b431/python-flask-web-app-in-azure-throws-internal-server-error?forum=windowsazurewebsitespreview
2. Gunicorn 500'ing on POST to Flask app
Here's the Flask app:
from flask import Flask, request, render_template, redirect, url_for, flash
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/contact', methods = ['POST'])
def contact():
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
message = request.form['message']
flash(name)
flash(email)
flash(message)
return redirect(url_for('index'))
if __name__ == '__main__':
app.debug = True
app.secret_key='a;sldfjka;oeiga;lbneas; biaweag'
app.run()
Here's the template I'm using:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="/contact" method="post">
<input type="text" name="name" />
<input type="text" name="email" />
<textarea name="message"> </textarea>
<input type="submit" />
</form>
<ul>
{% for message in get_flashed_messages() %}
<li><h3> {{ message }} </h3></li>
{% endfor %}
</ul>
</body>
</html>
And here's the gunicorn command line I use to serve it:
gunicorn --error-logfile err.log --access-logfile access.log \
--log-level debug -b 127.0.0.1:5000 --debug -w 4 wsgi:app
It serves the GET request for the template just fine. However, when I actually POST the form, it 500's out. Here's the response headers:
HTTP/1.1 500 INTERNAL SERVER ERROR
Server: gunicorn/0.17.2
Date: Thu, 21 Mar 2013 21:57:25 GMT
Connection: close
Content-Type: text/html
Content-Length: 291
Here's what shows up in the gunicorn logs:
==> err.log <==
2013-03-21 17:12:38 [10092] [DEBUG] POST /contact
==> access.log <==
"127.0.0.1 - - [21/Mar/2013:17:12:38] "POST /contact HTTP/1.1" 500 291 \
"http://localhost:5000/" "Mozilla/5.0 (X11; Linux x86_64; rv:19.0) \
Gecko/20100101 Firefox/19.0"
Works fine when I serve it using Flask's built in dev server.
Sol :
I defined the secret_key in the name == main block. Gunicorn was choking because the app didn't have said key, which it needs to handle form submissions.
if __name__ == '__main__':
app.secret_key = #...
That line needs to be... anywhere else, so long as it gets evaluated when the script is run by gunicorn.
app = Flask(__name__)
app.secret_key = #...
Link : https://stackoverflow.com/questions/15558276/gunicorn-500ing-on-post-to-flask-app
3. Azure Flask HTTP Error 500.0 - INTERNAL SERVER ERROR
I have a simple flask web app hosted on Microsoft Azure which allows users to login, upload files, view uploaded files and logout.
Now to handle users more efficiently I decided to use session from flask.
it works fine on my local machine. The Internal server error occurs only when it is deployed to azure
here is my sample code
'''import statements'''
from flask import render_template, request, session
'''more import statements'''
@app.route('/')
def home():
return render_template('login.html')
@app.route('/login_user', methods=['GET', 'POST'])
def login_user():
username = str(request.form['username'])
password = request.form['password']
connection = MongoClient('mongodb://username:password@xyz.com:pot_number/db_name')
collection = connection.db_name.demo
record = collection.find_one({'username':username})
if password == record['password']:
session['username'] = username
return render_template('index.html')
else:
login_message = "Invalid Username/Password"
return render_template('login.html',login_message=login_message)
@app.route('/upload', methods=['GET', 'POST'])
def upload():
if 'username' in session:
return render_template('upload.html')
else:
return render_template('login.html')
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session.pop('username',None)
return render_template('login.html')
whenever i try to add session['username'] = username the app crashes giving a Internal server error.
Im not sure what log file to post since I am new to Azure. Here is the event log
Application ID: /LM/W3SVC/1909144379/ROOT
Process ID: 69820
Exception: System.Configuration.ConfigurationErrorsException
Message: Couldn't find type for class Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35.
StackTrace: at System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type baseType, String initializeData)
at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceInternal.get_Listeners()
at System.Diagnostics.TraceInternal.WriteLine(String message)
at System.Diagnostics.Debug.WriteLine(String message)
at Microsoft.Web.Compilation.Snapshots.SnapshotHelper.TakeSnapshotTimerCallback(Object stateInfo)
at System.Threading.TimerQueueTimer.CallCallbackInContext(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()
at System.Threading.TimerQueue.AppDomainTimerCallback()</Data></EventData></Event><Event><System><Provider Name=".NET Runtime"/><EventID>1026</EventID><Level>0</Level><Task>0</Task><Keywords>Keywords</Keywords><TimeCreated SystemTime="2016-07-05T10:03:46Z"/><EventRecordID>348982031</EventRecordID><Channel>Application</Channel><Computer>RD00155DFA5791</Computer><Security/></System><EventData><Data>Application: w3wp.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Diagnostics.TraceUtils.GetRuntimeObject(System.String, System.Type, System.String)
at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceInternal.get_Listeners()
at System.Diagnostics.TraceInternal.WriteLine(System.String)
at System.Diagnostics.Debug.WriteLine(System.String)
at Microsoft.Web.Compilation.Snapshots.SnapshotHelper.TakeSnapshotTimerCallback(System.Object)
at System.Threading.TimerQueueTimer.CallCallbackInContext(System.Object)
at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
at System.Threading.TimerQueueTimer.CallCallback()
at System.Threading.TimerQueueTimer.Fire()
at System.Threading.TimerQueue.FireNextTimers()
at System.Threading.TimerQueue.AppDomainTimerCallback()
</Data></EventData></Event></Events>
here is the stack trace
2016-07-05T09:48:45
System.ApplicationException: The trace listener AzureBlobTraceListener is disabled. ---> System.InvalidOperationException: The SAS URL for the cloud storage account is not specified. Use the environment variable 'DIAGNOSTICS_AZUREBLOBCONTAINERSASURL' to define it. at Microsoft.WindowsAzure.WebSites.Diagnostics.AzureBlobTraceListener.RefreshConfig()
--- End of inner exception stack trace ---
Here is the Detailed error
Detailed Error Information:
Module FastCgiModule
Notification ExecuteRequestHandler
Handler Python FastCGI
Error Code 0x00000000
Requested URL http://SuperFlask:80/handler.fcgi/login_user
Physical Path D:\home\site\wwwroot\handler.fcgi\login_user
Logon Method Anonymous
Logon User Anonymous
Sol :
Have you set app.secret_key before using session variable? I had same issue, it worked locally, but threw 'Internal Server Error' in azure, because I set it in if _name__ == '_main__' block like this:
if __name__ == '__main__':
HOST = environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.secret_key = "secret_key"
app.run(HOST, port=PORT)
I set app.secret_key = "secret_key" outside of the if block and it worked.
Link : https://stackoverflow.com/questions/38200782/azure-flask-http-error-500-0-internal-server-error
4. Internal Server Error on Azure Flask Web App
I created a Flask web app hosted on Azure with a Dreamspark subscription. It uses libraries such as Numpy, Scipy and Theano.
When I deploy the app using a simple Theano calculations, it works perfectly. However, when I change the code to a more complex Theano calculation, I get an Internal Server Error.
Here is a sample code. When I call simpleFunction (something like a Theano function for a sum) it works but when calling complexFunction (something like an image classification calculation) then it creates an Internal Server Error:
Here is the Detailed Error Message:
Event: MODULE_SET_RESPONSE_ERROR_STATUS
ModuleName FastCgiModule
Notification EXECUTE_REQUEST_HANDLER
HttpStatus 500
HttpReason INTERNAL SERVER ERROR
HttpSubStatus 0
ErrorCode The operation completed successfully.
(0x0)
Sol:
Incorrect configuration for your webapp like web.config, please refer to the section web.config to check whether it's configured correctly.
Link :
5. Http 500 Internal Server Error on POST Request ( Incomplete)
I got "Http 500 Internal Server Error" during an update process (POST request) on production server. Its working fine if I restart my site from inetmgr. But after sometime it gives me same error. Please see attached "Developer Tool console" and "IIS server logs" images and suggest me any solution.
Sol : The following in the Web.config would give you additional information.
<system.webServer>
<httpErrors errorMode="Detailed" />
<asp scriptErrorSentToBrowser="true"/>
</system.webServer>
Link : https://forums.asp.net/t/2071474.aspxHttp+500+Internal+Server+Error+on+POST+Request
6. 500 internal server error on POST request
I am using Postman to create a new user with this raw body:
{
"uid": "fake.user@fake.com",
"first_name": "Fake",
"last_name": "User",
"email": "fake.user@fake.com"
}
The Content-Type header is application/json & Accept header is application/json
I get the 500 internal server error.
PS: I was able to retrieve users through a GET request. So, doesn't look like a problem with the token.
Sol :
Found the issue and solved it. I was not using any prefix for the endpoint and Postman defaulted to HTTP whereas it is a secure endpoint. Adding HTTPS prefix to the endpoint solves the problem.
Link : https://community.bridgeapp.com/thread/1622
7. 500 Internal Server Error with POST Requests
Here is my code for the POST request to the server.
The JSON to be posted to the server :
{
"User": {
"Name": "dog","Password": "123" }
}
How I am creating the JSON Object
object = new JSONObject();
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("Name", "dog");
jsonObject.put("Password", "123");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
object.put("User", jsonObject);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Posting to the server as
public HttpResponse request(JSONObject request)
throws ClientProtocolException, IOException, IllegalStateException,
JSONException {
client = (DefaultHttpClient) WebClientDevWrapper
.getNewHttpClient();
HttpPost post = new HttpPost(
"https://wbapi.cloudapp.net:443/api/User/LocalLogin/");
post.setEntity(new StringEntity(request.toString(), "utf-8"));
HttpResponse response = client.execute(post);
return response;
}
Along with classes provided by Paresh Mayani Here Send HTTPS Post Request to the server
I am getting the response object. But my response.getStatusLine() keeps showing 500/ Internal server error for only POST requests.
Note : GET requests are working fine.
Sol : Changed the request code as ( As recommended by Bhavdip Pathar)
public HttpResponse request(JSONObject request)
throws ClientProtocolException, IOException, IllegalStateException,
JSONException {
HttpPost post = new HttpPost(
"https://wbapi.cloudapp.net:443/api/User/LocalLogin/");
// post.setEntity(new StringEntity(request.toString(), "utf-8"));
StringEntity entity = new StringEntity(request.toString(), HTTP.UTF_8);
entity.setContentType("application/json");
post.setHeader("Content-Type", "application/json");
post.setHeader("Accept", "application/json");
post.setEntity(entity);
HttpResponse response = client.execute(post);
return response;
}
I did set application/json and I am getting HTTP/200 OK
Link : https://stackoverflow.com/questions/14065236/500-internal-server-error-with-post-requests
Usefull links (proxy blocked website access) :
https://www.reddit.com/r/flask/comments/2vu18t/error_500_internal_server_error_with_post/
https://groups.google.com/d/topic/appfog-users/FBp-kLPsOOI
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment