CS/COE 1520 Jarrett Billingsley

Slides:



Advertisements
Similar presentations
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
Advertisements

Sockets Tutorial Ross Shaull cs146a What we imagine Network request… response… The packets that comprise your request are orderly.
PHP syntax basics. Personal Home Page This is a Hypertext processor It works on the server side It demands a Web-server to be installed.
COEN 445 Communication Networks and Protocols Lab 4
A CHAT CLIENT-SERVER MODULE IN JAVA BY MAHTAB M HUSSAIN MAYANK MOHAN ISE 582 FALL 2003 PROJECT.
How Clients and Servers Work Together. Objectives Learn about the interaction of clients and servers Explore the features and functions of Web servers.
Chapter 23: ARP, ICMP, DHCP IS333 Spring 2015.
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Networking with Java. Basic Concepts A Network exists when two or more computers are connected such that they can communicate data back and forth. There.
Web server and web browser It’s a take and give policy in between client and server through HTTP(Hyper Text Transport Protocol) Server takes a request.
J2EE Web Fundamentals Lesson 1 Introduction and Overview
Socket Programming -What is it ? -Why bother ?. Basic Interface for programming networks at transport level It is communication end point Used for inter.
FTP (File Transfer Protocol) & Telnet
1 Chapter Client-Server Interaction. 2 Functionality  Transport layer and layers below  Basic communication  Reliability  Application layer.
Web application architecture
XHTML Introductory1 Linking and Publishing Basic Web Pages Chapter 3.
Integrating with UCSF’s Shibboleth system
Jozef Goetz, Application Layer PART VI Jozef Goetz, Position of application layer The application layer enables the user, whether human.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
1 Welcome to CSC 301 Web Programming Charles Frank.
Saving State on the WWW. The Issue  Connections on the WWW are stateless  Every time a link is followed is like the first time to the server — it has.
1 Client-Server Interaction. 2 Functionality Transport layer and layers below –Basic communication –Reliability Application layer –Abstractions Files.
Fall 2000C.Watters1 World Wide Web and E-Commerce Servers & Server Side Processing.
CSI 3125, Preliminaries, page 1 Networking. CSI 3125, Preliminaries, page 2 Networking A network represents interconnection of computers that is capable.
Cloud Computing Computer Science Innovations, LLC.
A PC Wakes Up A STORY BY VICTOR NORMAN. Once upon a time…  a PC (we’ll call him “H”) is connected to a network and turned on. Aside: The network looks.
Intro Web Applications Andrew Benson – ScottyLabs – CrashCourse F14.
Internet Flow By: Terry Hernandez. Getting from the customers computer onto the internet Internet Browser
COMP2322 Lab 4 Socket Programming Toby Lam March 2, 2016.
IN THIS LESSON WE WILL REVIEW THE STRUCTURE OF THE INTERNET AND HOW BROWSERS ASSEMBLE WEBSITES BASED ON INSTRUCTIONS THEY RECEIVE FROM SERVERS. Internet.
COOKIES AND SESSIONS.
Fall 2000C.Watters1 World Wide Web and E-Commerce Servers & Server Side Processing.
1 Network Communications A Brief Introduction. 2 Network Communications.
Ch. 23, 25 Q and A (NAT and UDP) Victor Norman IS333 Spring 2015.
Authentication & Authorisation Is the user allowed to access the site?
COMP2322 Lab 4 Socket Programming
Tonga Institute of Higher Education IT 141: Information Systems
Echo Networking COMP
Node.Js Server Side Javascript
HTTP AND ABSTRACTION ON THE INTERNET
The Linux Operating System
How to use the internet safely and How to protect my personal data?
Vocabulary Prototype: A preliminary sketch of an idea or model for something new. It’s the original drawing from which something real might be built or.
MCA – 405 Elective –I (A) Java Programming & Technology
E-commerce | WWW World Wide Web - Concepts
E-commerce | WWW World Wide Web - Concepts
Due: a start of class Oct 26
Networking for Home and Small Businesses – Chapter 6
Form Validation and AJAX
Vocabulary Prototype: A preliminary sketch of an idea or model for something new. It’s the original drawing from which something real might be built or.
TCP/IP Networking An Example
Networking for Home and Small Businesses – Chapter 6
Node.Js Server Side Javascript
CS222 Web Programming Course Outline
CISC103 Web Development Basics: Web site:
Topic 5: Communication and the Internet
WEB API.
TCP/IP Networking An Example
Tonga Institute of Higher Education IT 141: Information Systems
Issues in Client/Server Programming
Module P3 Practical: Building a webapp in nodejs and
HTTP and Abstraction on the Internet / The Need for DNS
Tonga Institute of Higher Education IT 141: Information Systems
CSCD 330 Network Programming
Planning and Storyboarding a Web Site
Networking for Home and Small Businesses – Chapter 6
HTTP Hypertext Transfer Protocol
Web Application Development Using PHP
Exceptions and networking
CS/COE 1520 Jarrett Billingsley
Presentation transcript:

CS/COE 1520 Jarrett Billingsley Flask CS/COE 1520 Jarrett Billingsley

Today: Server-side programming concepts Flask

Server-side programming

server needs to "answer the phone" How can I help you today? from now on we'll focus on the other side of the connection. Server Browser Request server needs to "answer the phone" and then… do something Response and send a page back.

it also does stuff on top of the application layer… Remember this? it might help to know where we'll be working. this is where server-side programming happens. the server software decodes requests, encodes responses, and manages sessions with the client. the OS does this. these are handled by the networking hardware. it also does stuff on top of the application layer… - ok, it gets a little fuzzy there around the transport/network layers. - some things might be software, some might be hardware - some things might be done in the kernel, some things in userspace - regardless, unless you're doing extremely high-scale stuff, you're not likely to need to dip below those top few layers.

Ports (part of TCP/UDP) one server can have multiple capabilities. to access a particular capability, you use the server's IP address and a port number. 93.184.216.34 22 SSH there are some well-known port numbers for common things. 80 HTTP port numbers range from 0 to 65535. 443 HTTPS written as an address, we put a colon between the IP address and port: 93.184.216.34:80 - it's like calling a number and then dialing an extension to get to a particular phone - port 8080 is sometimes used as a fallback for when port 80 is blocked or something - port 666 is what the original Doom uses for netplay ;o

Sockets Client Browser Client OS Server software Server OS 80 the OS abstracts network connections with sockets. socket! Client Browser Client OS connect with TCP to 93.184.216.34:80 socket! Server software Server OS 80 the Berkeley Sockets interface is the typical way to use sockets. they present a very file-like interface. - you can look up the Berkeley Sockets docs – though most languages other than C provide a nicer library around them.

the OS makes a new socket for each client. Waiting for a call the server tells the OS, "I'll wait for connections on this port." now, whenever a client connects on port 80, the software gets notified! I'm listening on port 80. the OS makes a new socket for each client. Server software Server OS socket! 80 Client 1 a port can support many connections, not just one. it's a logical address, not physical. socket! 80 Client 2 socket! 80 Client 3 - there are limits, of course, but it's not unusual for a server to handle hundreds or thousands of clients at once.

And now that we're finally talking… now the server finally gets the client's request. it's an HTTP request! and all the server is required to do is send a response. what happens in between… is up to you! so to summarize: the server listens on a port the client connects to that port with a socket the server OS creates a socket for that connection the server software is notified of the connection the server software reads the request and responds the connection is closed! and this is the step that Flask focuses on.

NEVER. TRUST. THE CLIENT. Oh, and one more thing when developing server-side software… NEVER. TRUST. THE CLIENT.

Flask Basics

What is it? Server software Flask Your code! a framework for writing server software it eliminates much of the boilerplate, so you just write the cool bits Server software Flask setting up sockets Your code! spawning threads rendering pages decoding requests encoding responses logins using databases maintaining sessions the actual logic of your app a bunch of useful functionality - if you haven't done framework-style programming before, it's a lot like GUI programming - you don't write the main loop; you just fill in the important functionality - callbacks! callbacks everywhere!

How do you get it? it's easy to install with pip, Python's package manager. on the command line, pip install flask once it's installed, you use it like a library. you're also given the flask command-line tool. see the Flask docs on the various ways you can run your server! - there'll be more detailed instructions laterrrr, don't worryyyyyy - and you can just look at the Flask docs too

Looking at a simple example this is what's in fl1_hello.py: this creates a Flask object. it's sort of our gateway into the library. from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return "Hello World!" if __name__ == "__main__": app.run() this decorator associates hello() with the root directory of the server. and when you run this file, this starts a server on localhost.

The call is coming from inside the house sockets are an abstraction, and can connect anywhere… including to your own computer! localhost, whose IP is 127.0.0.1, means "the current computer." Your Browser Your OS connect with TCP to 127.0.0.1:5000 in this case, the OS is using sockets as a form of IPC – inter-process communication. python fl1_hello.py 5000 - ehhhh? 449? ehhhhhhhh??????????? - why is 449 not a prereq of this course, again? this is super useful for doing server development. you can serve and test on the same machine, no internet needed!

Routes http://example.com/some/path.html remember the path part of a URL: this doesn't have to be a literal file path. http://example.com/some/path.html a Flask route associates a path with a function. when that path gets a request, the function is called. let's look at fl2_hello_foo.py, which has multiple routes. note that /funny redirects us to /bar instead.

Trailing slash or no? http://example.com/contact in that example, /foo has no trailing slash, while /bar/ does. why? well, it's more of a conceptual difference… "slashless" names are for pages that stand on their own. http://example.com/contact use a trailing slash for pages with "sub-pages." http://example.com/user/ maybe /user/ gives me my user page (once logged in)… http://example.com/user/3390 …but the same route with a number after it gives me a particular user's page. - in this example, if the browser tries to access "/user" with no trailing slash, flask will automatically redirect it to "/user/". - but the reverse is not true – "/contact/" will give a 404, not a redirect.

Client-server communication

GET vs POST remember these methods? when the user fills out a form element and submits it… the server gets a POST request, where the body contains the data from the form. POST / HTTP/1.1 Host: 127.0.0.1 User-Agent: Mozilla/5.0 Accept: */* Content-Length: 26 Content-Type: application/x-www-form-urlencoded anumber=12345&astring=test let's look at fl3_form.py.

NEVER. TRUST. THE CLIENT. Being sneaky the curl command-line tool lets us send HTTP requests manually. curl --data "anumber=12345&astring=test" 127.0.0.1:5000/ but what if we sent bogus data? curl --data "ohno=1337" 127.0.0.1:5000/ in this case, Flask helps us. when we try to access "anumber" or "astring", we get a KeyError. Flask catches that, and responds with a 400 Bad Request. but what if we try entering not-a-number in the number field? even in the browser? huh… no checking. you have no guarantees that the client is going to send you valid, well-formed data. NEVER. TRUST. THE CLIENT.

"Logging in" the idea behind logging in is simple: the user knows some secret that the server also knows. the user proves to the server that they are who they say they are. in fl4_login.py… we have a dict of users and passwords. the only way we can view our profile (curProfile) is by POSTing our username and password. what happens if we give a wrong username? well, that's our fault, really :^) what if we have multiple pages that need the user to be logged in? this seems like a really cumbersome way of doing it…