FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford...

31
FLASK-SOCKET.IO Web Sockets Made Simple

Transcript of FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford...

Page 1: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FLASK-SOCKET.IOWeb Sockets Made Simple

Page 2: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

WEB SOCKETS!

Page 3: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

OK. BUT,

WHY?

Page 4: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FIRST THERE WAS THE INTERNET

Page 5: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

AND THEN THERE WAS TCP/IP

Page 6: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

TCP/IP SOCKETS

import socketsock = socket.create_connection(('24.244.4.54', 80), timeout=30) try: # Send the request sock.sendall("Really important stuff") # Get the response response = sock.recv(1024) # Bytesfinally: sock.close()

Page 7: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

TCP/IP SOCKETS

➤ Real time ➤ Reliable delivery ➤ No standards for delivery

➤ … just bits on the wire

➤ Requires persistent connection

Page 8: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

BUT…

I HAVE A 90 MHZ PROCESSOR AND 16 MB OF RAM

Page 9: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181
Page 10: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

“From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification

Page 11: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

➤ 4 74181 ALUs (~45 MHz)

➤ 128-512 KB of RAM

➤ 2.5 MB Single Platter Storage Cartridge

Page 12: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

HTTP

Page 13: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

HTTP

➤ Request / Response

➤ Disconnect TCP socket after response

➤ Meta-data

➤ Information about the payload

➤ Date/time sent

➤ Caching

➤ etc…

➤ Lighter on server resources

➤ The Web!

Page 14: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

HTTP Request

POST /example?key=value&something=other HTTP 1.1

Host: www.example.com

Accept: application/json, application/xml

Accept-Language: EN-US

Accept-Encoding: gzip, deflate

User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 4.0)

Connection: Keep-Alive

{ "Content": "Adding my content", "Other": [2, 4, 6, 8] }

http://www.example.com/example?key=value&something=other

Page 15: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

HTTP (W/ REQUESTS)

import requestsresponse = requests.get('http://www.google.ca')

Page 16: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

UH, SO…

SECURITY?

Page 17: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

WHAT

ABOUT

IP

ADDRESSES?

Page 18: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

BRAVE NEW WORLD (OF NETWORKS)

➤ Network Address Translation (NAT)

➤ Closed ports (pretty much web only)

➤ Security first

➤ ….

Page 19: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

HTTP 1.1

Page 20: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

WEB SOCKETS!

Page 21: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

WEB SOCKETS

➤ Upgrade from a standard HTTP request

➤ Can navigate the modern NAT

➤ Can be authenticated

➤ More secure

Page 22: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

GET /chat HTTP/1.1

Host: server.example.com

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Key: x3JJHMbDL1EzLkh9GBhXDw==

Sec-WebSocket-Protocol: chat, superchat

Sec-WebSocket-Version: 13

Origin: http://example.com

HTTP/1.1 101 Switching Protocols

Upgrade: websocket

Connection: Upgrade

Sec-WebSocket-Accept: HSmrc0sMlYUkAGmm5OPpG2HaGWk=

Sec-WebSocket-Protocol: chat

Page 23: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FLASK-SOCKET.IOWeb Sockets Made Simple

Page 24: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

OH, BUT FIRST LETS CHECK OUT

FLASK

Page 25: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FLASK

from flask import Flaskapp = Flask(__name__)@app.route("/") def hello(): return "Hello World!"if __name__ == "__main__": app.run()

Page 26: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

AND

SOCKET.IO

TOO

Page 27: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

SOCKET.IO CLIENT

<script src="/socket.io/socket.io.js"></script> <script> var socket = io('http://localhost'); socket.on('news', function (data) { console.log(data); socket.emit('my other event', { my: 'data' }); });</script>

Page 28: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

SOCKET.IO

➤ Channels

➤ Namespaces

➤ Rooms (server-side)

Page 29: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FLASK-SOCKET.IOWeb Sockets Made Simple

Page 30: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

FLASK-SOCKET.IO

from flask import Flaskfrom flask_socketio import SocketIOapp = Flask(__name__)app.config['SECRET_KEY'] = 'secret!'socketio = SocketIO(app)@socketio.on('channel') def handle_message(message): print('received message: ' + message)if __name__ == '__main__': socketio.run(app)

Page 31: FLASK-SOCKET · 22.03.2016  · From 1973 to 1974, Cerf's networking research group at Stanford worked out details of the idea, resulting in the first TCP specification 4 74181

DEMO