Skip to content

Instantly share code, notes, and snippets.

@Fkawala
Created October 21, 2016 16:07
Show Gist options
  • Save Fkawala/1f25aa71b5d01465512f0e3223c90f40 to your computer and use it in GitHub Desktop.
Save Fkawala/1f25aa71b5d01465512f0e3223c90f40 to your computer and use it in GitHub Desktop.
from webapp2 import Route, WSGIApplication, RequestHandler
import ujson
import urllib
import base64
import logging
class Handler(RequestHandler):
def post(self):
message = ujson.decode(urllib.unquote(self.request.body).rstrip('='))['message']
attributes = message.get('attributes', {})
data = message.get('data', {})
try:
print "data >>", data
print "b64decode >>", base64.b64decode(str(message['data']))
print "+" * 72
except Exception as e:
print logging.exception(e)
def get(self):
return self.post()
application = WSGIApplication(debug=True, routes=[Route('/', Handler)])
# Dockerfile extending the generic Python image with application files for a single application.
FROM ubuntu:latest
# First we install https transportation for APT. Indeed skewed.de forces https.
RUN apt-get update
RUN apt-get install -y \
python-pip
ADD requirements /tmp/requirements
RUN pip install --upgrade pip
RUN pip install -r /tmp/requirements
ENV GCLOUD_PROJECT my-project
ENV GOOGLE_APPLICATION_CREDENTIALS my-cred.json
ADD . /app
WORKDIR /app
CMD gunicorn -b :8080 consumer:application
# Dockerfile extending the generic Python image with application files for a single application.
FROM ubuntu:latest
# First we install https transportation for APT. Indeed skewed.de forces https.
RUN apt-get update
RUN apt-get install -y \
python-pip
ADD requirements /tmp/requirements
RUN pip install --upgrade pip
RUN pip install -r /tmp/requirements
ENV GCLOUD_PROJECT my-project
ENV GOOGLE_APPLICATION_CREDENTIALS my-cred.json
ADD . /app
WORKDIR /app
CMD python producer.py
from gcloud import pubsub
import ujson
if __name__ == '__main__':
client = pubsub.Client()
topic = client.topic('debug-topic')
if not topic.exists():
topic.create()
subscription = pubsub.subscription.Subscription(name='debug-subscription', topic=topic, push_endpoint='https://my-tunnel.ngrok.io/')
if not subscription.exists():
subscription.create()
batch = topic.batch()
for i in range(10):
batch.publish(ujson.encode({'a_string': 'my-val', 'an_int': i}), an_attr=str('a value'))
batch.commit()
print('topic {0}'.format(topic))
print('subscription {0}'.format(subscription))
print('message_ids {0}'.format(batch.message_ids))
gcloud==0.18.3
gunicorn==19.4.5
webapp2==2.5.2
WebOb==1.5.1
Paste==2.0.3
ujson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment