Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First changes #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions server - Copy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import os
import http.server
import socketserver

from http import HTTPStatus


class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
msg = 'Hello! you requested %s' % (self.path)
self.wfile.write(msg.encode())


port = int(os.getenv('PORT', 80))
print('Listening on port %s' % (port))
httpd = socketserver.TCPServer(('', port), Handler)
httpd.serve_forever()
62 changes: 48 additions & 14 deletions server.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,53 @@
import os
import http.server
import socketserver
from flask import Flask, request, jsonify
app = Flask(__name__)

from http import HTTPStatus

@app.route('/getmsg/', methods=['GET'])
def respond():
# Retrieve the name from the url parameter /getmsg/?name=
name = request.args.get("name", None)

class Handler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(HTTPStatus.OK)
self.end_headers()
msg = 'Hello! you requested %s' % (self.path)
self.wfile.write(msg.encode())
# For debugging
print(f"Received: {name}")

response = {}

port = int(os.getenv('PORT', 80))
print('Listening on port %s' % (port))
httpd = socketserver.TCPServer(('', port), Handler)
httpd.serve_forever()
# Check if the user sent a name at all
if not name:
response["ERROR"] = "No name found. Please send a name."
# Check if the user entered a number
elif str(name).isdigit():
response["ERROR"] = "The name can't be numeric. Please send a string."
else:
response["MESSAGE"] = f"Welcome {name} to our awesome API!"

# Return the response in json format
return jsonify(response)


@app.route('/post/', methods=['POST'])
def post_something():
param = request.form.get('name')
print(param)
# You can add the test cases you made in the previous function, but in our case here you are just testing the POST functionality
if param:
return jsonify({
"Message": f"Welcome {name} to our awesome API!",
# Add this option to distinct the POST request
"METHOD": "POST"
})
else:
return jsonify({
"ERROR": "No name found. Please send a name."
})


@app.route('/')
def index():
# A welcome message to test our server
return "<h1>Welcome to our medium-greeting-api!</h1>"


if __name__ == '__main__':
# Threaded option to enable multiple instances for multiple user access support
app.run(threaded=True, port=5000)