Output from a Python page call by a JQuery getJSON function - python

I'm working on a website that uses Python web.py. There is a form where the user enters input and when the submit button is hit, a python page is called (matches) using the .getJSON JQuery function show below.
function buildMatches(input){
$.getJSON("/matches", {e:input}, function(json){
//Returned JSON object is worked on
}
}
The "matches" python page grabs a value from a DB, runs some string reg ex and returns a JSON object. Everything works fine, my question is how would I be able to output something from the python page "matches" to see what is exactly happening during the reg ex operations? I've tried print "" (python 2.5), but I understand that would print to the console. I've done some research but couldn't find anything for my situation. I don't necessarily need to print it out to the HTML page, just any where where I can see what's going on. Thanks in advance for any help.
I have access to the webserver (SSH, SFTP, etc.), I tried to log by importing the logging module, below is the code I used. I could get it to log if I ran the page from the command line, but not when it is called by the JS page.
import logging
logging.basicConfig(filename='./SomeClass.log', filemode='w', level=logging.DEBUG)
class SomeClass:
logging.info('Started')
logging.info('Another log')
def __init__(self, obj):
logging.info('In the init')
def another_functio(self):
logging.info('Logging inside the function')
I've tried setting the full path of the log and I still have the same problem where the log file will only be written or updated when I run this class from the console. This doesn't work when the class is called by the webserver.
logging.basicConfig(filename='/home/.../.../.../example.log', filemode='w', level=logging.DEBUG)

Depending on how much access you have to the web server you can run your code manually so web.py uses its built-in web server. Then print statements will end up on the console.
Otherwise, have you thought about simply writing to your own log file somewhere accessible with a browser?

Thanks again for all the help. After digging more into the setup of the Apache server and Python implementation I was able to find a solution to help me see what's going and debug my web app. I saw that Apache config is setup to log errors and WSGI also blocks (pukes on) std.out. What I was able to do is redirect the print command to the Apache error log files.
print >> sys.stderr, "Debugging print with variable" + variable
Then I check the Apache error log to start debugging the web app. I thought I would share this in case anyone else ran into this problem as it was a pain for me.
Thanks again.

Related

Any way to run an internal python script from a webpage?

I finally made a project that I wanted to make since a long time :
I'm using an Arduino Uno to replace my PC power button (with a simple relay) and that Arduino Board is connected to a Raspi 3 for network connection purposes
My wish is to do a webpage (or a API-Like request) that at a touch of a button (preferably in a password-protected page) It'll power the PC on
I know how to code in Python, and my script to control the Arduino is already done but I can't find a way to run, only server-side, a Python Script from a button in a webpage
I found that CherryPy framework but I don't think it'll suit my needs
Can someone give me any ideas about that please?
As already mentioned by #ForceBru, you need a python webserver.
If this can be useful to you, this is a possible unsecure implementation using flask:
from flask import Flask
from flask import request
app = Flask(__name__)
#app.route('/turnOn')
def hello_world():
k = request.args.get('key')
if k == "superSecretKey":
# Do something ..
return 'Ok'
else:
return 'Nope'
If you put this in an app.py name file and, after having installed flask (pip install flask), you run flask run you should be able to see Ok if visiting the url http://localhost:5000/turnOn?key=superSecretKey .
You could write a brief html gui with a button and a key field in a form but I leaves that to you (you need to have fun too!).
To avoid potential security issues you could use a POST method and https.
Look at the flask documentation for more infos.

Flask logging to file - working locally but not on server

I am trying to diagnose some mysterious Internal Server Errors, so I added logging to the flask app. After digging through many explanations, I found this answer which I am using below, along with the suggestion to put everything inside a #app.before_first_request decorator.
Locally this works as expected and werkzeuglog.log contains exactly what is printed to the screen.
But on the server:
The log file is created.
Nothing is written to it.
No errors are thrown, at least not to the user
#app.before_first_request
def initialize():
logger = logging.getLogger('werkzeug')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler('werkzeuglog.log')
logger.addHandler(handler)
if __name__ == '__main__':
#run app
app.run(
host="0.0.0.0",
port=int("80"),
debug=False
)
Am I doing something wrong or is this some server issue that is beyond my scope? Gentle answers appreciated!
EDIT:
Some context: An internal server error is caused when the user uploads a file and I am trying to figure out what's going wrong using the log.
I don't think it can be an issue with permissions since the script definitely creates the file. I checked that the disk is not full.
EDIT 2
I reran the run.fcgi file which starts the WSGI Server. Now the manual entries to the log file via logger.info('blah blah') show up but not the others i.e. GET POST etc.
I don't have access to /var/log/nginx. It's a student-volunteer run server so I would like to give them as much info as I can. But this is all I have so far.

How to measure runtime in web.py?

I created a python webserver with web.py.
This webserver works fine. But I will get the infromation how long the webserver needs to get the result from the database and send it back to the client.
Can I realize that with the modul timeit?
Does someone has an idea?
You can use web.profiler middleware to display some profiling information within each response.
application = web.application(urls, globals(), web.profiler)
If that information isn't enough, you could use cProfile to run your app and this will give you more detailed information.
import cProfile
cProfile.run("application.run()")

Python: Read POST Parameters

I've never used python before but need to write a fairly simple script for a project I'm working on. All I need is a script that listens on port 80 for incoming HTTP POSTs and prints the posted variables/parameters and values to the command line.
What's the easiest way for a beginner to get this done?
Thanks.
Consider using Flask Framework. With just 7-8 lines your job is done.
It will print all parameter sent using post and get request to your console.
Also I would recommend to learn it.
from flask import Flask
app = Flask(__name__)
#app.route("/",methods=['POST','GET'])
def index():
print request.form
return "Please Check your command line"
if __name__ == "__main__":
app.run(host='0.0.0.0',port=80,debug=True)

How can I display updating output of a slow script using Pylons?

I am writing an application in Pylons that relies on the output of some system commands such as traceroute. I would like to display the output of the command as it is generated rather than wait for it to complete and then display all at once.
I found how to access the output of the command in Python with the answer to this question:
How can I perform a ping or traceroute in python, accessing the output as it is produced?
Now I need to find a way to get this information to the browser as it is being generated. I was planning on using jQuery's loadContent() to load the output of a script into a . The problem is that Pylons controllers use return so the output has to be complete before Pylons renders the page and the web server responds to the client with the content.
Is there any way to have a page display content as it is generated within Pylons or will this have to be done with scripting outside of Pylons?
Basically, I'm trying to do something like this:
http://network-tools.com/default.asp?prog=trace&host=www.bbc.co.uk
pexpect will let you get the output as it comes, with no buffering.
To update info promptly on the user's browser, you need javascript on that browser sending appropriate AJAX requests to your server (dojo or jquery will make that easier, though they're not strictly required) and updating the page as new responses come -- without client-side cooperation (and JS + AJAX is the simplest way to get that cooperation), there's no sensible way to do it on the server side alone.
So the general approach is: send AJAX query from browser, have server respond as soon as it has one more line, JS on the browser updates contents then immediately sends another query, repeat until server responds with an "I'm all done" marker (e.g. an "empty" response may work for that purpose).
You may want to look at this faq entry. Then with JS, you always clear the screen before writing new stuff.
I haven't tried it with pylons, but you could try to show the output of the slow component in an iframe on the page (using mime type text/plain) and yield each chunk to the iframe as it is generated. For fun I just put this together as a
WHIFF demo. Here is the slowly generated web content wsgi application:
import time
def slow(env, start_response):
start_response("200 OK", [('Content-Type', 'text/plain')])
return slow_generator()
def slow_generator():
yield "slowly generating 20 timestamps\n"
for i in range(20):
yield "%s: %s\n" % (i, time.ctime())
time.sleep(1)
yield "done!"
__wsgi__ = slow
This file is deployed on my laptop at: http://aaron.oirt.rutgers.edu/myapp/root/misc/slow.
Here is the WHIFF configuration template which includes the slow page in an
iframe:
{{env whiff.content_type: "text/html"/}}
Here is an iframe with slowly generated content:
<hr>
<iframe frameborder="1" height="300px" width="300px" scrolling="yes"
style="background-color:#99dddd;"
src="slow"
></iframe>
<hr>
Isn't that cool?
This is deployed on my laptop at http://aaron.oirt.rutgers.edu/myapp/root/misc/showSlowly.
hmmm. I just tried the above link in safari and it didn't work right... apparently there are some browser differences... Seems to work on Firefox at least...

Categories