Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import MySQLdb
- import webapp2
- CLOUDSQL_CONNECTION_NAME = os.environ.get('CLOUDSQL_CONNECTION_NAME')
- CLOUDSQL_USER = os.environ.get('CLOUDSQL_USER')
- CLOUDSQL_PASSWORD = os.environ.get('CLOUDSQL_PASSWORD')
- DB_NAME='test-db'
- def connect_to_cloudsql():
- # When deployed to App Engine, the `SERVER_SOFTWARE` environment variable
- # will be set to 'Google App Engine/version'.
- if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine/'):
- # Connect using the unix socket located at
- # /cloudsql/cloudsql-connection-name.
- cloudsql_unix_socket = os.path.join(
- '/cloudsql', CLOUDSQL_CONNECTION_NAME)
- db = MySQLdb.connect(
- unix_socket=cloudsql_unix_socket,
- user=CLOUDSQL_USER,
- passwd=CLOUDSQL_PASSWORD)
- # If the unix socket is unavailable, then try to connect using TCP. This
- # will work if you're running a local MySQL server or using the Cloud SQL
- # proxy, for example:
- #
- # $ cloud_sql_proxy -instances=your-connection-name=tcp:3306
- #
- else:
- db = MySQLdb.connect(
- host='127.0.0.1', user=CLOUDSQL_USER, passwd=CLOUDSQL_PASSWORD, db=DB_NAME)
- return db
- class MysqlPage(webapp2.RequestHandler):
- def get(self):
- """Simple request handler that shows all of the MySQL variables."""
- self.response.headers['Content-Type'] = 'text/plain'
- db = connect_to_cloudsql()
- """ Your db statement would go their later on."""
- self.response.write('It worked')
- app = webapp2.WSGIApplication([
- ('/mysql', MysqlPage),
- ], debug=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement