App Engine: Qwik Start - Python









App Engine allows developers to focus on doing what they do best, writing code, and not what it runs on. Developers upload their apps to App Engine, and Google Cloud takes care of the rest. The notion of servers, virtual machines, and instances have been abstracted away, with App Engine providing all the compute necessary. Developers don't have to worry about operating systems, web servers, logging, monitoring, load-balancing, system administration, or scaling, as App Engine takes care of all that. Developers only need to focus on building solutions for their organizations or their users.

The App Engine standard environment provides application-hosting services supporting the following languages: Python, Java, PHP, Go, Node.js, and Ruby). The App Engine flexible environment provides even more flexibility by supporting custom runtimes, however it is out-of-scope for this lab.


App Engine is Google Cloud's original serverless runtime, and since its original launch in 2008, has been joined by:








Cloud Functions, great for situations where you don't have an entire app, have broken up a larger, monolithic app into multiple microservices, or have short event-driven tasks that execute based on user activity.



Cloud Run, the serverless container-hosting service similar to App Engine but more accurately reflects the state of software development today.





In this lab, you'll learn how to deploy a basic app to App Engine, but we invite you to also explore Cloud Functions and Cloud Run. App Engine makes it easy to build and deploy an application that runs reliably even under heavy load and with large amounts of data. (Cloud Functions and Cloud Run do the same.)

App Engine apps can access numerous additional Cloud or other Google services for use in their applications:

NoSQL database: Cloud Datastore, Cloud Firestore, Cloud BigTable
Relational database: Cloud SQL or Cloud AlloyDB, Cloud Spanner
File/object storage: Cloud Storage, Cloud Filestore, Google Drive
Caching: Cloud Memorystore (Redis or memcached)
Task execution: Cloud Tasks, Cloud Pub/Sub, Cloud Scheduler, Cloud Workflows
User authentication: Cloud Identity Platform, Firebase Auth, Google Identity Services

Applications run in a secure, sandboxed environment, allowing App Engine standard environment to distribute requests across multiple servers, and scaling servers to meet traffic demands. Your application runs within its own secure, reliable environment that is independent of the hardware, operating system, or physical location of the server

In this lab you'll do the following with a Python app:

Clone/download/Write

Test

Update

Test

Deploy

ketan_patel@cloudshell:~/python-docs-samples/appengine/standard/hello_world (svo-mvp)$ ls -l

total 12
-rw-r--r-- 1 ketan_patel ketan_patel  91 Aug 18 20:54 app.yaml
-rw-r--r-- 1 ketan_patel ketan_patel 271 Aug 18 20:54 main.py
-rw-r--r-- 1 ketan_patel ketan_patel 194 Aug 18 20:54 main_test.py


ketan_patel@cloudshell:~/python-docs-samples/appengine/standard/hello_world (svo-mvp)$ more *

::::::::::::::

app.yaml

::::::::::::::
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app

::::::::::::::

main.py

::::::::::::::
import webapp2


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers["Content-Type"] = "text/plain"
        self.response.write("Hello, World!")


app = webapp2.WSGIApplication(
    [
        ("/", MainPage),
    ],
    debug=True,
)
::::::::::::::

main_test.py

::::::::::::::
import webtest

import main


def test_get():
    app = webtest.TestApp(main.app)

    response = app.get("/")

    assert response.status_int == 200
    assert response.body == "Hello, World!"


ketan_patel@cloudshell:~/python-docs-samples/appengine/standard/hello_world (svo-mvp)$ 




Task 3. Test the application

Test the application using the Google Cloud development server (dev_appserver.py), which is included with the preinstalled App Engine SDK.



ketan_patel@cloudshell:~/python-docs-samples/appengine/standard/hello_world (svo-mvp)$ dev_appserver.py app.yaml 

INFO     2023-08-18 21:11:14,796 devappserver2.py:321] Skipping SDK update check.
INFO     2023-08-18 21:11:14,944 <string>:398] Starting API server at: http://localhost:33331
INFO     2023-08-18 21:11:14,948 dispatcher.py:276] Starting module "default" running at: http://localhost:8080
INFO     2023-08-18 21:11:14,949 admin_server.py:70] Starting admin server at: http://localhost:8000
INFO     2023-08-18 21:11:17,970 instance.py:294] Instance PID: 1582
INFO     2023-08-18 21:11:39,076 module.py:862] default: "GET /?authuser=0&redirectedPreviously=true HTTP/1.1" 200 13
INFO     2023-08-18 21:11:39,246 module.py:862] default: "GET /favicon.ico HTTP/1.1" 404 154
INFO     2023-08-18 21:11:42,958 instance.py:294] Instance PID: 1595



CLICK "OPEN EDITOR" 

View the results by clicking the Web preview (web preview icon) > Preview on port 8080.








^CINFO     2023-08-18 21:19:57,686 shutdown.py:50] Shutting down.
INFO     2023-08-18 21:19:57,688 stub_util.py:360] Applying all pending transactions and saving the datastore
INFO     2023-08-18 21:19:57,688 stub_util.py:363] Saving search indexes
ketan_patel@cloudshell:~/python-docs-samples/appengine/standard/hello_world (svo-mvp)$ 




Task 5. Deploy your app

To deploy your app to App Engine, run the following command from within the root directory of your application where the app.yaml file is located

ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$ cat main.py 

# [START gae_python38_app]
# [START gae_python3_app]
from flask import Flask


# If `entrypoint` is not defined in app.yaml, App Engine will look for an app
# called `app` in `main.py`.
app = Flask(__name__)


@app.route("/")
def hello():
    """Return a friendly HTTP greeting.

    Returns:
        A string with the words 'Hello World!'.
    """
    return "Hello Ketan Super World!"


if __name__ == "__main__":
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. You
    # can configure startup instructions by adding `entrypoint` to app.yaml.
    app.run(host="127.0.0.1", port=8080, debug=True)
# [END gae_python3_app]
# [END gae_python38_app]
ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$ 

ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$ gcloud app deploy

Services to deploy:

descriptor:                  [/home/ketan_patel/python-docs-samples/appengine/standard_python3/hello_world/app.yaml]
source:                      [/home/ketan_patel/python-docs-samples/appengine/standard_python3/hello_world]
target project:              [svo-mvp]
target service:              [default]
target version:              [20230819t011945]
target url:                  [https://svo-mvp.uw.r.appspot.com]
target service account:      [svo-mvp@appspot.gserviceaccount.com]


Do you want to continue (Y/n)?  Y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
Uploading 6 files to Google Cloud Storage
17%
33%
50%
67%
83%
100%
100%
File upload done.
Updating service [default]...done.                                                                    
Setting traffic split for service [default]...done.                                                   
Deployed service [default] to [https://svo-mvp.uw.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse
ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$




ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$ gcloud app browse

Did not detect your browser. Go to this link to view your app:
https://svo-mvp.uw.r.appspot.com

ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$




 


FROM GCP PERSONAL VM


ketan_patel@ketanvm:~/.ssh$ curl https://svo-mvp.uw.r.appspot.com/

Hello Ketan Super World!ketan_patel@ketanvm:~/.ssh$ 
ketan_patel@ketanvm:~/.ssh$ 




FROM CLOUDSHELL

ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$ curl https://svo-mvp.uw.r.appspot.com


<html><head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<title>403 Forbidden</title>
</head>
<body text=#000000 bgcolor=#ffffff>
<h1>Error: Forbidden</h1>
<h2>Access is forbidden.</h2>
<h2></h2>
</body></html>
ketan_patel@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (svo-mvp)$






  503  mkdir appengine
  504  cd appengine
  505  vi app.yaml
  506  vi main.py
  507  cat main.py
  508  vi main.py
  509  vi main_test.py
  510  cd appengine/
  511  ls
  512  ls -l
  513  more *
  514  vi main_test.py
  515  cat main_test.py 
  516  gcloud app deploy
  517  gcloud app browse
  518  history
ketan_patel@cloudshell:~/appengine (new-user-learning)$ more *
::::::::::::::
app.yaml
::::::::::::::
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /.*
  script: main.app
::::::::::::::
main.py
::::::::::::::
import webapp2


class MainPage(webapp2.RequestHandler):
    def get(self):
        self.response.headers["Content-Type"] = "text/plain"
        self.response.write("Hello, World!")


app = webapp2.WSGIApplication(
    [
        ("/", MainPage),
    ],
    debug=True,
)
::::::::::::::
main.pyc
::::::::::::::

!c@sGddlZdejfdYZejdefgdeZdS(iNMainPagecBseZdZRS(cCs$d|jjd<|jjddS(Ns
text/plains^LContent-Types
__module__R(((s#/home/ketan_patel/appengine/main.pyRst/tdebug(twebapp2tRequestHandlerRtWSGIApplicationtTruetapp(((s#/home/ketan_patel/appengine/main.py<module>^L
::::::::::::::
main_test.py
::::::::::::::
import webtest

import main


def test_get():
    app = webtest.TestApp(main.app)

    response = app.get("/")

    assert response.status_int == 200
    assert response.body == "Hello, World of Storage!"

ketan_patel@cloudshell:~/appengine (new-user-learning)$


No comments:

Post a Comment

AppEngine - Python

tudent_04_347b5286260a@cloudshell:~/python-docs-samples/appengine/standard_python3/hello_world (qwiklabs-gcp-00-88834e0beca1)$ sudo apt upda...