Regex match of hexdigest in google app engine webapp WSGIApplication - python

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/)?

Related

404 from cron job on google app engine django app

So, everything else works... to preface this. But, I haven't really moved outside the admin interface. I'm trying to get data from an API and insert it into the database if there's changes. I've managed to write a script that can do that (in theory... it can do it locally), but I can't get the app in the cloud to recognize its existence. I've followed Google's suggestion of adding it to the app.yaml and cron.yaml to no avail.
Do I need to add this to a urls.py? I haven't mucked with teh handlers at all thus far and I'm not sure what settings.py makes happen, what the yaml files make happen, and how much of this is pixie dust.
here are teh relevant files...
app.yaml
runtime: python
env: flex
entrypoint: gunicorn -b :$PORT mysite.wsgi
threadsafe: yes
beta_settings:
cloud_sql_instances: [redacted]
runtime_config:
python_version: 3
health_check:
enable_health_check: False
handlers:
- url: /static
static_dir: static/
- url: /run/get_data/
script: JSONdownload.app
login: admin
- url: .*
script: mysite.wsgi.application
cron.yaml
cron:
- description: "get data"
url: /run/get_data/
schedule: every 5 minutes
JSONdownload.py
#!/usr/bin/env python
# /var/spool/cron/crontabs
import json
import urllib2
from django.http import HttpResponse
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from .models import Game
from .models import Team
class JSONdownloadHandler(webapp.RequestHandler):
def get(self):
self.response.write('cron')
class MainHandler(webapp.RequestHandler):
def get(self):
self.response.write('yay')
I'm finding great resources for a basic python app... but none for this situation really... anyone know of something better than what I'm doing, PLEASE let me know!
You're mixing up elements of the flexible environment app.yaml with those of the standard environment app.yaml
In particular the script: JSONdownload.app portion is ignored. You need to add the handler for the /run/get_data/ path inside your mysite.wsgi app, maybe from there invoking the JSONdownload.py code.
Somehow related: cron job in google app engine not working.
So... it was that I didn't route to teh location. I was actually able to just set a route (with admin against it) to the URL and then able to call it via the cron.yaml.

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()

Getting Error: redirect_uri_mismatch The redirect URI in the request: http://localhost:8080/oauth2callback did not match a registered redirect URI

I'm getting this error while trying to run my application...
The redirect URI in the request: http://localhost:8080/oauth2callback did not match a registered redirect URI
In google API console i have registered my redirect urls
Redirect URIs: http://localhost:8080/
And in the client_secrets.json also i'm using the same as redirect url
I'm following this tutorial
https://developers.google.com/bigquery/articles/dashboard#addoauth2
Edit:
I just made some changes to the existing code
Now the
redirect URIs in API console is http://localhost:8080/oauth2callback
And here is my app.yaml
application: hellomydashboard
version: 1
runtime: python
api_version: 1
handlers:
- url: /favicon\.ico
static_files: favicon.ico
upload: favicon\.ico
- url: /oauth2callback
script: oauth2client/appengine.py
- url: .*
script: main.py
Now though its not showing any error but it displays a blank page.
Here is my main.py
from bqclient import BigQueryClient
import httplib2
import os
from google.appengine.api import memcache
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from oauth2client.appengine import oauth2decorator_from_clientsecrets
# Project ID for project to receive bill.
# During limited availability preview, there is no bill.
# The value should be your quoted Client ID number
# which you previously recorded from code.google.com/apis/console
# REPLACE THIS NUMBER WITH YOUR CLIENT ID
PROJECT_ID = "My Project ID" #i just replaced dat
DATASET = "samples"
TABLE = "natality"
# CLIENT_SECRETS, name of a file containing the OAuth 2.0
# information for this application.
CLIENT_SECRETS = os.path.join(os.path.dirname(__file__),
'client_secrets.json')
http = httplib2.Http(memcache)
decorator = oauth2decorator_from_clientsecrets(CLIENT_SECRETS,
'https://www.googleapis.com/auth/bigquery')
bq = BigQueryClient(http, decorator)
class MainHandler(webapp.RequestHandler):
#decorator.oauth_required
def get(self):
self.response.out.write("Hello Dashboard!\n")
application = webapp.WSGIApplication([
('/', MainHandler),
], debug=True)
def main():
run_wsgi_app(application)
if __name__ == '__main__':
main()
So according to main.py if everything is fine it must print Hello Dashboard but it isn't
You will actually need to add the following to your redirect URIs:
http://localhost:8080/oauth2callback
Also, you may need to append a trailing / if the above doesn't match:
http://localhost:8080/oauth2callback/
using google openId I configured this
Redirect URIs: http://domain.com/authenticate/google
on https://code.google.com/apis/console, if you must create a app if you don't have one, note that must match entirely the url
In main.py functions main class add (decorator.callback_path, decorator.callback_handler()), and remove
- url: /oauth2callback
script: oauth2client/appengine.py
from app.yaml.
PS: You might get DownloadError if you have some proxy-configuration/webcontent-filter. If you disable these configurations or deploy it on Google Server, it will work just fine.
seems like google tries to match url with being case-sensitve cause when i tried it with /Authorize and /authorize, it gave me redirect_uri_mismatch error for first one but worked for latter one
someone try and let me know if i m wrong
In main.py file,
in the part where you create a wsgi application
under application = webapp.wsgiapplication(
add a handler
(decorator.callback_path,decorator.callback_handler()),

Adding new app in appengine

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/

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.

Categories