I have a Flutter Web app with an iOS Safari-specific error. To debug it I create a build (flutter build web) and run Python's http.server (python3 -m http.server), then use ngrok to be able to open the app on my mobile device.
To be able to see logs I use OverlayEntry with Text, but it's not very convenient.
Python's http.server does some logging that looks like this:
Serving HTTP on :: port 8000 (http://[::]:8000/) ...
::1 - - [10/Sep/2022 20:05:06] "GET / HTTP/1.1" 200 -
::1 - - [10/Sep/2022 20:05:07] "GET /flutter.js HTTP/1.1" 304 -
Is it possible to log something from a Flutter app to see it inside Python's http.server logs?
Yes, it's possible. You can use print() to log something to the console. You can also use the dart:developer package to log to the browser's console. Example:
import 'dart:developer' as developer;
developer.log('Hello world!', name: 'my.app.category');
Related
I used Visual Studio 2017 as IDE for my small Flask project, but when I tried to upgrade to VS2019, it just won't start any more, the Python console stucked at
* Serving Flask app "flask_vs2019" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
Then I created a new Flask project via VS2019 wizard, added debug=True and it stucked at restarting. The launch code are shown below:
import os
from flask_vs2019 import app
if __name__ == '__main__':
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT, debug=True)
I tried multiple workarounds and I'm pretty sure it is a VS 2019 issue.
Flask works when:
using VS 2017 with exact same code
using a cmd console and launch by flask run or python -m flask run
switch off reloader with use_reloader=False or DEBUG=False, which is unacceptable since you have to relaunch Flask whenever you change your code.
Flask won't work when:
using VS 2019 (16.7) for any version of Flask and werkzeug
set FLASK_ENV=development and/or DEBUG=True as environment variable
installing watchdog package in virtual environment
In fact I found that the exact same issue was reported on Visual Studio Developer Community back on March 2020, but "this issue didn't have much product team activity and a very small amount of new votes or comments. Based on this, its severity, and affected area, it’s my experience that this issue is very unlikely to be fixed". Well I guess I will have to stick with VS 2017 for now.
Even if I turned off debug mode, the Python console still didn't work properly as the color output was broken.
127.0.0.1 - - [08/Aug/2020 13:59:02] "[37mGET / HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Aug/2020 13:59:02] "[37mGET /static/content/bootstrap.min.css HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Aug/2020 13:59:02] "[37mGET /static/content/site.css HTTP/1.1[0m" 200 -
127.0.0.1 - - [08/Aug/2020 13:59:03] "[33mGET /favicon.ico HTTP/1.1[0m" 404 -
Work around with colorama setup colorama.init(autoreset=True) won't work. In VS2017 no workaround would be needed and color output just works as is.
So I made a very simple flask-based app and hosted it in a kubernetes pod.
When I open the logs in Rancher, I can see this warning:
* Serving Flask app "app/preapproved_limits/api.py"
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
that I see as well when I start flask on my local machine.
But what I don't see on my local machine and do see in Rancher is this:
10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -
INFO:werkzeug:10.0.67.20 - - [02/Jul/2020 16:49:20] "GET /health HTTP/1.1" 200 -
which gets logged every 5 seconds.
What the heck?
But the most important and annoying thing is I can't see my logs which I make with the Python's print() function.
Can someone explain:
Why do I see something both in Rancher and locally (the initial Flask warning), but
why do I see something only in Rancher (the /health or INFO:werkzeug logs)
and why do I see something only locally on my machine, but not in Rancher (print())
/health is a normal healthcheck endpoint that k8s needs.
Flask doesn't print to stdout by default because it buffers lines to make I/O more efficient. You can either call sys.stdout.flush(), print(flush=True), set env var PYTHONUNBUFFERED=1 in your Dockerfile (probably the easiest) or just use logging module as we all should.
You can read more about this env var here.
I have a simple python script that I want to serve as a website:
import SimpleHTTPServer
import SocketServer
PORT = 8000
Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
print "serving at port", PORT
httpd.serve_forever()
I'm in that folder and run
$ python3 -m http.server
then I visit
http://hassbian.local:8000/song.py
The terminal says this and I get the file as a txt file, the script won't execute.
Serving HTTP on 0.0.0.0 port 8000 ...
192.168.1.115 - - [04/Jun/2017 14:19:59] "GET / HTTP/1.1" 200 -
192.168.1.115 - - [04/Jun/2017 14:20:04] "GET /song.py HTTP/1.1" 200 -
Running on a rasberry pi
SimpleHTTPServer doesn't do CGI. If you want CGI you will have to use CGIHTTPServer
This module can run CGI scripts on Unix and Windows systems.
I am not sure if that's what you really want, but invoking a python script in the manner you have shown is CGI. CGI is a really old way of doing things. Running simple web apps with python is now almost exclusively the domain of webapp2 or flask. While more complex apps involving databases are dominated by django.
When I run python -m SimpleHTTPServer <port> on my office network, I can get my full PC name displayed in the log output.
Serving HTTP on 0.0.0.0 port 59992 ...
<pc name> - - [04/Feb/2014 04:59:30] "GET / HTTP/1.1" 200 -
But when I run the same in my home network or locally host it, the logs only display the IP address which accessed the service.
Serving HTTP on 0.0.0.0 port 59992 ...
<my ip> - - [04/Feb/2014 04:59:30] "GET / HTTP/1.1" 200 -
Is there any way I could make my home network do the same?
I am testing Heroku's ability to write a Facebook app with Python. I'm having a problem running the basic tutorial. It seemed like this question was worth asking on StackOverflow in case there's an answer that helps other people who run into the exact same problem.
I followed the instructions on heroku's facebook development page (http://devcenter.heroku.com/articles/facebook). Deploying to Heroku worked fine.
However, running the app locally does not. When I follow the instructions and bring up
http://localhost:5000
I get to the Facebook Login screen. But when I click Log In on that screen, I get:
SSL connection error Unable to make a secure connection to the
server. This may be a problem with the server, or it may be requiring
a client authentication certificate that you don't have. Error 107
(net::ERR_SSL_PROTOCOL_ERROR): SSL protocol error.
and the console output is
09:55:07 web.1 | https://localhost:5000/ 09:55:07 web.1 |
https://www.facebook.com/dialog/oauth?client_id=179852202088814&redirect_uri=https://localhost:5000/&scope=user_likes,user_photos,user_photo_video_tags
09:55:07 web.1 | 127.0.0.1 - - [24/Sep/2011 09:55:07] "GET / HTTP/1.1"
302 - 09:59:02 web.1 | 127.0.0.1 - - [24/Sep/2011 09:59:02] code 400,
message Bad request syntax
('\x16\x03\x00\x00U\x01\x00\x00Q\x03\x00N}\xe2&\xf9\xf7"\x15\xd5\xb6\xf6\xa6\x0f\xb01\x97N\xcc\xb3l\xed\x97\xd1!-\x91c?\x1f\xac\xa2h\x00\x00*\x00\xff\x00\x88\x00\x87\x009\x008\x00\x84\x005\x00E\x00D\x00f\x003\x002\x00\x96\x00A\x00\x04\x00\x05\x00/\x00\x16\x00\x13\xfe\xff\x00')
09:59:02 web.1 | 127.0.0.1 - - [24/Sep/2011 09:59:02]
"UQN}?&??"ն??1?N̳l??!-?c???h*???98?5EDf32?A/??" 400 -
When I try it in Safari, the address bar shows the following very long URL:
https://localhost:5000/?code=AQBPWpkbRdL2bt7KER0fcUS9ZnheXiGApkaF5MXbNgyIJqzw46SGve1iVyLIx1sDltNh0PkXPDdxhjAxoa1YED1cpcaflCXCkqzO27A-rhgjBpXwWUClpGRpRmDD2eIXcOyIczo_qGf45tbpvDZO5hFa0gmUeSHri4vY3bqw-5jBjZRoZfEB7pI8cLPOIsnNICI#_=_
Safari compains that it can't establish a secure connection.
This is running on OS X 10.6.8.
This is because https is not enabled locally on your machine, you could enable this or you could alternatively run without the SSL on your localhost. To do this you would edit the function to look something like:
def get_home():
return 'http://' + request.host + '/'