Adding new app in appengine - python

3 days ago I started using google appengine to see how it works.
I have set the basic app that prints "hello". Nothing complicated. I updated the simple app thet prints "hello" app and it worked perfectly.
Then I wanted to experiment a little more:
First I did is that I uploaded new program (i didint change app.yaml, just main.py)
Then i got a empty screen.
Then I uploaded a new version of app (changed version in app.yaml, and main.py), I have changed a versions in admin in backend. I still had a empty screen.
Thanks.
This is my code:
main.py
#!/usr/bin/env python
print 'Hello, World!'
app.yaml:
application: searchbarrel
version: 2
runtime: python
api_version: 1
handlers:
- url: /.*
script: main.py
The app works good on localhost

Your CGI script needs to print at least one blank line before outputting text, since the browser will take the first line to be an HTTP header:
#!/usr/bin/env python
print '\nHello, World!'
will work fine.
Ideally, you should print actual valid HTTP headers before your blank line.
dev_appserver acts a bit differently than the production servers, which is why you're seeing output running locally.
(It is a good idea to use a WSGI framework, however.)

this can't work. its not enough to write a file that has a print hello inside. you need to create a WSGI app and make a RequestHandler that processes your request and writes the hello world out.
main.py
import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello, World')
app = webapp2.WSGIApplication([('/', MainPage)])
def main():
application = webapp.WSGIApplication([("/", MainPage)], debug=True)
run_wsgi_app(application)
if __name__ == "__main__":
main()
you should read the getting started guide first.
https://cloud.google.com/appengine/docs/standard/python/

Related

arduino yun uhttpd flask setup

I'm trying to set up python and flask on the arduino yun. I've managed to run python files via the /etc/config/uhttpd configuration file:
...
list interpreter ".py=/usr/bin/python"
...
The default path for the website's root is: /www in which I've placed a soft link (apps) to the sd card. So now I can run python programs: http://[ip arduino]/apps/helloworld.py
And when I make my first helloflask.py program and run that via python helloflask.py I can see the result at: http://[ip arduino]:5000
But now I want to configure the uhttpd mini webserver (which is capable to exchange information via CGI) to use the flask setup. The URI: http://flask.pocoo.org/docs/deploying/cgi/#server-setup shows some instructions... but I just don't get it. I've made a directory ../apps/uno in which I've placed a __init__.py file with the following content:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "He Flask!"
In the apps dir I've put a file: cgi.py with this content:
from wsgiref.handlers import CGIHandler
from uno import app
CGIHandler().run(app)
Now I when I browse: http://[ip arduino]/cgi.py get a server error occured, contact the administrator (I think this is the CGI interface from uhttpd).
I just don't grasp the CGI configuration for Flask/uhttpd
I looked into this too and got a little further, I was able to setup a simple hello world but once I tried to do something non-trivial I ran into a big issue that uhttpd doesn't support URL rewriting/aliasing. This means your flask app can only be served at the URL of its .py file instead of at a root like http:// (arduino IP) /flaskapp/. None of the routes inside the app will be visible and makes the whole thing unusable.
However, instead of trying to force flask into uhttpd I had great success running the built in server that flask provides. Take a look at this guide I wrote up that uses flask to serve data from a Yun: https://learn.adafruit.com/smart-measuring-cup/overview
The thing to do is add a call to app.run when the script is run, for example make your flask app look like:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello Flask!"
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True, threaded=True)
Then log in to the Yun and run the script using python. Flask's built in server should start serving the app on http:// (arduino IP) :5000/. Make sure to include the host='0.0.0.0' as it's required to listen on the Yun's external network interface. You probably also want debug=True so there are better error messages (and live reloading of the server when the code changes), and I found threaded=True helps because the default server only handles one connection at a time. The Yun is a relatively slow processor so don't expect to service a lot of concurrent requests, however it's quite capable for providing a simple REST API or web application for a few users.
If you want this server to always run on bootup, edit the /etc/rc.local file to include a call to python and your script.

Script Handler for Google App Engine

I am trying to study using Python in Google App Engine and can't get the tutorial to work. But ultimately, I would want to write a Python script that would return list of files in a folder in server to JavaScript.
Here's what I currently have:
+MainProj
+ static
+scripts
. __init__.py
. helloworld.py
. app.yaml
In helloworld.py
import webapp2
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write('Hello, webapp2 World!')
app = webapp2.WSGIApplication([('/.*', MainPage)], debug=True)
In app.yaml
application: applicationname
version: 1
runtime: python27
api_version: 1
threadsafe: yes
handlers:
- url: /.*
script: static.scripts.helloworld.app
I am getting a server error
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
Anyone can help what's wrong with my setup?
Every folder in your package path ('static.scripts.helloworld.app') needs to have __init__.py in it to import properly, so either add one to 'static' or (more sensibly, in my opinion) move helloworld.py up to the top, and use 'helloworld.app' in your app.yaml.
All you need in your app.yaml handler is:
- url: /.*
script: static.scripts.helloworld.py
And make sure you also have in your helloworld.py at the bottom code to actually start the application and listener:
from google.appengine.ext.webapp import util
# granted, you might want to replace "webapp" with "webapp2" here
def main():
util.run_wsgi_app(app)
if __name__ == '__main__':
main()

Exclude One URL From Python URL Masking

I have never written a python script in my life, but I have a question that can hopefully be solved pretty quickly...
I'm using Google App Engine and Dropbprox. The script uses a custom domain to point to your public DropBox folder for better DropBox URLs. I'd like to be able to redirect users to my main site (jacob.bearce.me) if they visit my dropbox url (dl.bearce.me).
The problems that I'm having:
I've never used GAE or Python before, so I have no idea where to even begin
Putting a index.html file in my GAE project didn't fix it (I was hoping it'd just default to that if there was no filename specified, like it would on a normal site, but no cigar.)
Just a simple redirect if a users visits the main URL is all I'm after, nothing fancy.
My Python file: http://dl.bearce.me/mirror.py
Here's a main.py that issues a redirect for all requests, using the Python 2.5 runtime environment:
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
class MainHandler(webapp.RequestHandler):
def get(self):
self.redirect('http://jacob.bearce.me/')
application = webapp.WSGIApplication([('/.*', MainHandler)],
debug=True)
def main():
util.run_wsgi_app(application)
if __name__ == '__main__':
main()
And here's the app.yaml file you need to route URLs to this handler:
application: myapp
version: 1
runtime: python
api_version: 1
handlers:
- url: .*
script: main.py
(Replace myapp with your actual app ID.)
For more information about creating and uploading an App Engine app in Python, see the Getting Started tutorial.

Google App Engine Python Cron

I've been trying for a few days now to get Google App Engine to run a cron Python script which will simply execute a script hosted on a server of mine.
It doesn't need to post any data to the page, simply open a connection, wait for it to finish then email me.
The code I've previously written has logged as "successful" but I never got an email, nor did I see any of the logging.info code I added to test things.
Ideas?
The original and wrong code that I originally wrote can be found at Google AppEngine Python Cron job urllib - just so you know I have attempted this before.
Mix of weird things was happening here.
Firstly, app.yaml I had to place my /cron handler before the root was set:
handlers:
- url: /cron
script: assets/backup/main.py
- url: /
static_files: assets/index.html
upload: assets/index.html
Otherwise I'd get crazy errors about not being able to find the file. That bit actually makes sense.
The next bit was the Python code. Not sure what was going on here, but in the end I managed to get it working by doing this:
#!/usr/bin/env python
# import logging
from google.appengine.ext import webapp
from google.appengine.api import mail
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import urlfetch
import logging
class CronMailer(webapp.RequestHandler):
def get(self):
logging.info("Backups: Started!")
urlStr = "http://example.com/file.php"
rpc = urlfetch.create_rpc()
urlfetch.make_fetch_call(rpc, urlStr)
mail.send_mail(sender="example#example.com",
to="email#example.co.uk",
subject="Backups complete!",
body="Daily backups have been completed!")
logging.info("Backups: Finished!")
application = webapp.WSGIApplication([('/cron', CronMailer)],debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
Whatever it was causing the problems, it's now fixed.

Regex match of hexdigest in google app engine webapp WSGIApplication

application = webapp.WSGIApplication(
[(r'/main/profile/([a-f0-9]{40})', ProfileHandler)],
debug=True)
The regex in the above parameter will not recognize a 40 hex long hexdigest in Google App Engine.
I'm getting 404s instead of ProfileHandler being passed the matching 40 hex long profile ID. My app.yaml passes everything /main/.* to the correct python script so that's not the issue. The regex looks sane and resembles the example regex in GAE docs. What is wrong with this regex?
I can not reproduce your problem. Here is an exact code I have:
index.py
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class ProfileHandler(webapp.RequestHandler):
def get(self, *ar, **kw):
self.response.out.write("PROFILE IS:" + ar[0])
run_wsgi_app(webapp.WSGIApplication(
[(r'/main/profile/([a-f0-9]{40})', ProfileHandler),],
debug=True))
app.yaml
application: someapp
version: 1
runtime: python
api_version: 1
handlers:
- url: /main/.*
script: index.py
Application is listening on port 8082
GET: http://localhost:8082/main/profile/4c4f630aef49c0065c22eb3dd35a00f5787f4816
RESPONSE: PROFILE IS:4c4f630aef49c0065c22eb3dd35a00f5787f4816
I have no experience with the Google App Engine, but:
what happens if you change ([a-f0-9]{40}) in to ([a-fA-F0-9]{40})
are you sure group $1 is used and not the entire match (including /main/profile/)?

Categories