Skip to content

Commit

Permalink
Add C5M3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
marga-google committed Dec 18, 2019
1 parent 978215f commit fce5976
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Course5/Lab3/hello_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""A simple Hello World type app which can serve on port 8000.
Optionally, a different port can be passed.
The code was inspired by:
https://gist.github.com/davidbgk/b10113c3779b8388e96e6d0c44e03a74
"""
import http
import http.server
import socket
import socketserver
import sys

# TCP port for listening to connections, if no port is received
DEFAULT_PORT=8000

class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(http.HTTPStatus.OK)
self.end_headers()
# Hello message
self.wfile.write(b'Hello Cloud')
# Now get the hostname and IP and print that as well.
hostname = socket.gethostname()
host_ip = socket.gethostbyname(hostname)
self.wfile.write(
'\n\nHostname: {} \nIP Address: {}'.format(
hostname, host_ip).encode())


def main(argv):
port = DEFAULT_PORT
if len(argv) > 1:
port = int(argv[1])

web_server = socketserver.TCPServer(('', port), Handler)
print("Listening for connections on port {}".format(port))
web_server.serve_forever()


if __name__ == "__main__":
main(sys.argv)
8 changes: 8 additions & 0 deletions Course5/Lab3/hello_cloud.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Unit]
After=network.target

[Service]
ExecStart=/usr/local/bin/hello_cloud.py 80

[Install]
WantedBy=default.target

0 comments on commit fce5976

Please sign in to comment.