Info 253: Web Architecture
Kay Ashaolu
From end of Lecture 12 example
curl -v "http://localhost:3000/?day_of_week=sunday"
* Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 3000 (#0) > GET /?day_of_week=sunday HTTP/1.1 > Host: localhost:3000 > User-Agent: curl/7.47.0 > Accept: */*
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 856
< ETag: W/"358-OO441lqfknOF3OaKpKfZqQ"
< Date: Fri, 07 Oct 2016 01:06:25 GMT
< Connection: keep-alive
<
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/css/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<p>The quote for sunday is: Life is about making an impact,
not making an income. –Kevin Kruse</p>
Building on end of Lecture 12 example
Building on end of Lecture 12 example
webserver.py (1/3)
import requests
import os
from flask import Flask, request, render_template
app = Flask(__name__, static_url_path="/static")
@app.route('/email', methods=['GET'])
def show_email_page():
return render_template("email.html", notifications=[])
webserver (2/3)
@app.route('/email', methods=['POST'])
def send_email():
message = request.form.get("message")
notifications = []
data = {
'from': os.environ["INFO253_MAILGUN_FROM_EMAIL"],
'to': os.environ["INFO253_MAILGUN_TO_EMAIL"],
'subject': "You just was sent a message",
'text': message,
}
webserver (3/3)
auth = (os.environ["INFO253_MAILGUN_USER"], os.environ["INFO253_MAILGUN_PASSWORD"])
r = requests.post(
'https://api.mailgun.net/v3/{}/messages'.format(os.environ["INFO253_MAILGUN_DOMAIN"]),
auth=auth,
data=data)
if r.status_code == requests.codes.ok:
notifications.append("Your email was sent")
else:
notifications.append("You email was not sent. Please try again later")
return render_template("email.html", notifications=notifications)
templates/email.html
<html>
<head>
<title>Email</title>
<link rel='stylesheet' href='/static/style.css' />
</head>
<body>
<ul>
{% for notification in notifications %}
<li>{{ notification }}</li>
{% endfor %}
</ul>
<h1>Email me!</h1>
<form action="/email" method="POST">
<textarea name="message"></textarea>
<input type="submit" value="Send me an email!" />
</form>
</body>
</html>
curl -v --data message=this%20is%20cool localhost:3000/email
* Trying 127.0.0.1... * Connected to localhost (127.0.0.1) port 3000 (#0) > POST /email HTTP/1.1 > Host: localhost:3000 > User-Agent: curl/7.47.0 > Accept: */* > Content-Length: 24 > Content-Type: application/x-www-form-urlencoded message=this%20is%20cool
* upload completely sent off: 24 out of 24 bytes
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Content-Type: text/html; charset=utf-8
< Content-Length: 346
< ETag: W/"15a-/UO9Judtk8xIMgUMpeBmQg"
< Date: Fri, 07 Oct 2016 05:53:06 GMT
< Connection: keep-alive
<
<html>
<head>
<title>Email</title>
<link rel='stylesheet' href='/static/style.css' />
</head>
<body>
...