Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a24558f

Browse files
engelkeTakashi Matsuo
andauthored
feat(appengine): Pub/Sub sample for both Python 2.7 and 3 (GoogleCloudPlatform#4489)
* Make sample work in both Python 2.7 and 3 environments * Lint fix Co-authored-by: Takashi Matsuo <[email protected]>
1 parent 6dfc849 commit a24558f

File tree

4 files changed

+51
-10
lines changed

4 files changed

+51
-10
lines changed

appengine/standard_python3/pubsub/README.md

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
# Python 3 Google Cloud Pub/Sub sample for Google App Engine Standard Environment
1+
# Python Google Cloud Pub/Sub sample for Google App Engine Standard Environment
22

33
[![Open in Cloud Shell][shell_img]][shell_link]
44

55
[shell_img]: http://gstatic.com/cloudssh/images/open-btn.png
66
[shell_link]: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=appengine/standard/pubsub/README.md
77

8-
This demonstrates how to send and receive messages using [Google Cloud Pub/Sub](https://cloud.google.com/pubsub) on [Google App Engine Standard Environment](https://cloud.google.com/appengine/docs/standard/).
8+
This demonstrates how to send and receive messages using [Google Cloud Pub/Sub](https://cloud.google.com/pubsub) on [Google App Engine Standard Environment](https://cloud.google.com/appengine/docs/standard/) for
9+
Python 2.7 or for Python 3. See instructions below for deploying in either
10+
of those two environments.
911

1012
## Setup
1113

@@ -30,7 +32,7 @@ Before you can run or deploy the sample, you will need to do the following:
3032
--push-auth-service-account=[your-service-account] \
3133
--push-auth-token-audience=example.com
3234

33-
1. Update the environment variables in ``app.yaml``.
35+
1. Update the environment variables in ``app.yaml`` for Python 3, or ``app27.yaml`` for Python 2.7.
3436

3537
## Running locally
3638

@@ -78,8 +80,17 @@ The simulated push request fails because it does not have a Cloud Pub/Sub-genera
7880

7981
Note: Not all the files in the current directory are needed to run your code on App Engine. Specifically, `main_test.py` and the `data` directory, which contains a mocked private key file and a mocked public certs file, are for testing purposes only. They SHOULD NOT be included when deploying your app. When your app is up and running, Cloud Pub/Sub's push servers create tokens using a private key, then the Google Auth Python library takes care of verifying and decoding the token using Google's public certs, to confirm that the push requests indeed come from Cloud Pub/Sub.
8082

81-
In the current directory, deploy using `gcloud`:
83+
In the current directory, deploy using `gcloud`. For Python 2.7 you must first
84+
install the required libraries in the `lib` folder:
8285

83-
$ gcloud app deploy app.yaml
86+
$ pip -t lib -r requirements.txt
87+
$ gcloud app deploy app27.yaml
8488

85-
You can now access the application at `https://[your-app-id].appspot.com`. You can use the form to submit messages, but it's non-deterministic which instance of your application will receive the notification. You can send multiple messages and refresh the page to see the received message.
89+
For Python 3, you can simply run the deploy command:
90+
91+
$ gcloud app deploy app27.yaml
92+
93+
You can now access the application using the `gcloud app browse` command. You
94+
can use the form to submit messages, but it's non-deterministic which instance
95+
of your application will receive the notification. You can send multiple
96+
messages and refresh the page to see the received message.

appengine/standard_python3/pubsub/app.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ runtime: python38
22

33
#[START env]
44
env_variables:
5-
PUBSUB_TOPIC: your-topic
6-
# This token is used to verify that requests originate from your
7-
# application. It can be any sufficiently random string.
8-
PUBSUB_VERIFICATION_TOKEN: 1234abc
5+
PUBSUB_TOPIC: '<YOUR_TOPIC>'
6+
# This token is used to verify that requests originate from your
7+
# application. It can be any sufficiently random string.
8+
PUBSUB_VERIFICATION_TOKEN: '<YOUR_VERIFICATION_TOKEN>'
99
#[END env]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
runtime: python27
2+
api_version: 1
3+
threadsafe: yes
4+
5+
handlers:
6+
- url: .*
7+
script: main.app
8+
9+
libraries:
10+
- name: setuptools
11+
version: 36.6.0
12+
- name: grpcio
13+
version: 1.0.0
14+
- name: ssl
15+
version: latest
16+
17+
env_variables:
18+
PUBSUB_TOPIC: '<YOUR_TOPIC>'
19+
PUBSUB_VERIFICATION_TOKEN: '<YOUR_VERIFICATION_TOKEN>'
20+
GOOGLE_CLOUD_PROJECT: '<YOUR_PROJECT_ID>'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from google.appengine.ext import vendor
2+
import pkg_resources
3+
4+
5+
# Set path to your libraries folder.
6+
path = 'lib'
7+
# Add libraries installed in the path folder.
8+
vendor.add(path)
9+
# Add libraries to pkg_resources working set to find the distribution.
10+
pkg_resources.working_set.add_entry(path)

0 commit comments

Comments
 (0)