54
Writing Network Programs with Python Nile University, CIT-614 Network Programming and Distributed Objects, By Dr Sameh Al Ansary, modified by Ahmed Kishk

Mufix Network Programming Lecture

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Mufix Network Programming Lecture

Writing Network Programs with Python

Nile University, CIT-614 Network Programming and Distributed Objects, By Dr Sameh Al Ansary, modified by Ahmed Kishk

Page 2: Mufix Network Programming Lecture

Agenda

Talk - Are We really Engineers?Python (Programming language).

Page 3: Mufix Network Programming Lecture

Engineering?

Software: is it really engineering? If it is not engineering, then what is it?

Art? Craft? Science?

Page 4: Mufix Network Programming Lecture

Engineering?

Art: the production or expression of what is beautiful, appealing, or of more than ordinary significance.

Craft: A trade or occupation requiring special skill, especially manual skill.

Science: The systematic study of man and his environment based on the deductions and inferences that can be made, and the general laws that can be formulated, from reproducible observations and measurements of events and parameters within the universe.

Page 5: Mufix Network Programming Lecture

Engineering?

Engineering: The art of making practical application of the knowledge of pure science.

Simply it is a profession.

Page 6: Mufix Network Programming Lecture

Engineering?

The profession in which a knowledge of the mathematical and natural sciences gained by study, experience, and practice is applied with judgment to develop ways to utilize, economically, the materials and forces of nature for the benefit of mankind.

Accreditation board for Engineering & Technology, 1996

Page 7: Mufix Network Programming Lecture

Talk

SEI (Software Engineering Institute)Software Product Line (Development, Testing and

Production Environment)Skill y0ur self with Software Engineering

professionalism (Testing -Architecture Design and Documentation)

Doing Unit Testing, Regression Testing, Integration Testing, System Testing and Smoke Testing.

Web Application EngineeringYou have to have critical thinking.Read Code Complete

Page 8: Mufix Network Programming Lecture

To Find More…

Page 9: Mufix Network Programming Lecture

Let’s Start our Lecture

Page 10: Mufix Network Programming Lecture

Python

Python is a programming language that lets you work more quickly and integrate your systems more effectively.

Download the compiler from http://www.python.org/

PyDev is a plugin for python in Eclipse IDE.You can find installation steps at below link

http://pydev.org/manual_101_install.html

Page 11: Mufix Network Programming Lecture

Python

no explicit variable declarations

Variables spring into existence by being assigned a value

automatically destroyed when they go out of scope.

Page 12: Mufix Network Programming Lecture

Python

Run: Python >>>print “Hello MUFIX”

Weak typed language: >>> x = 5 >>>x 5 >>>type(x) <type 'int'> >>>x = “MUFIX” >>>x MUFIX >>>type(x) <type 'str'>

Page 13: Mufix Network Programming Lecture

Python

.py file is called a module.To declare a function use “def:”Args separated by “,”No return typesuse the “return” keyword to return values

Page 14: Mufix Network Programming Lecture

Python Indenting Code

no explicit begin or end, and no curly braces.

Page 15: Mufix Network Programming Lecture

Python Lists

Like java ArrayList but more powerful.>>> list = [“a”, “b”, “c”, “d”]>>>list[“a”, “b”, “c”, “d”]>>> List[0]“a”>>> List[3]“d”

Page 16: Mufix Network Programming Lecture

Python Lists

Negative List indices>>>list[“a”, “b”, “c”, “d”]>>>list[-1]“d”>>>list[-3]“b”

Page 17: Mufix Network Programming Lecture

Python Lists

Slicing a list>>>list[“a”, “b”, “c”, “d”]>>>list[1:3][“b”, “c”]>>>list[1, -1][“b”, “c”]>>>list[:3][“a”, “b”, “c”]>>>list[:][“a”, “b”, “c”, “d”]

Page 18: Mufix Network Programming Lecture

Python Lists

Operations: like java ArrayList methods

List.append()List.insert()List.extend()List.index()List.pop()

Page 19: Mufix Network Programming Lecture

Python Dictionaries

Like Java HashMap but a simple one.A key / value pairs

>>> Info = {“name”:”Ahmed”, “job”:”Software Engineer”}

>>>Info[“name”]“Ahmed”>>>Info[“College”] = “MUFIC”>>>Info{“name”:”Ahmed”, “job”:”Software Engineer”,

“College”:”MUFIC”}

Page 20: Mufix Network Programming Lecture

Python Dictionaries

• len(d)• d[key]• d[key] = value• del d[key]• key in d• key not in d• clear()• copy()• items()• keys()• values()

Page 21: Mufix Network Programming Lecture

Tuples

>>> T = (“a”, “b”, “c”, “d”)>>> T(“a”, “b”, “c”, “d”)

Read-Only ListsHave no methodsLike java Enums

Page 22: Mufix Network Programming Lecture

Python Strings

>>> names = “C++, Java, Python, Ruby, C#”>>>list = names.split(“,”)['C++', ' Java', ' Python', ' Ruby', ' C#']>>>names = ” ”.join(list)>>>names”C++ Java Python Ruby C#”

Page 23: Mufix Network Programming Lecture

Python and Files

Page 24: Mufix Network Programming Lecture

To Find More…

Page 25: Mufix Network Programming Lecture

Network Programming

Browsers, chat clients, Downloads…..etc

Page 26: Mufix Network Programming Lecture

Internet Socket and TCP/IP

an Internet socket or network socket is an endpoint of a bidirectional inter-process communication flow across an Internet Protocol-based computer network, such as the Internet.

Internet sockets is also used as a name for an (API) for the TCP/IP protocol stack

provided by the operating system. Internet sockets delivers incoming data packets to

the appropriate application process or thread, based on a combination of local and remote IP addresses and port numbers.

Page 27: Mufix Network Programming Lecture

Socket types

Datagram sockets, also known as connectionless sockets, which use User Datagram Protocol (UDP)

Stream sockets, also known as connection-oriented sockets, which use Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP).

Raw sockets (or Raw IP sockets), typically available in routers and other network equipment. Here the transport layer is bypassed, and the packet headers are not stripped off, but are accessible to the application. Application examples are Internet Control Message Protocol (ICMP, best known for the Ping sub operation), Internet Group Management Protocol (IGMP), and Open Shortest Path First (OSPF).

Page 28: Mufix Network Programming Lecture

Connection-Oriented Services

socket()

bind()

listen()

accept()

read()

write()

[blocked]

socket()

connect()

write()

read()

[blocked]

[blocked]

Server Client

When interaction is over, server loops to accept a new connection

Page 29: Mufix Network Programming Lecture

First Network Program – Echo Server

Page 30: Mufix Network Programming Lecture

Client

Page 31: Mufix Network Programming Lecture

File Like Objects

Page 32: Mufix Network Programming Lecture

HTTP RFC 2616

Page 33: Mufix Network Programming Lecture

HTTP Request and Response

Page 34: Mufix Network Programming Lecture

HTTP Request

• The format of the initial line for requests andresponses is different• The Request line contains:– Method:• GET: The most common• Others: POST, HEAD, PUT,…– Request-URI• Exampe: bla/directory17/hello.html– HTTP-Version• Example The string: “HTTP/1.1”

Page 35: Mufix Network Programming Lecture

HTTP Request

Page 36: Mufix Network Programming Lecture

Request Methods

Page 37: Mufix Network Programming Lecture

Response Initial line

• The Response initial (Called Status line): – HTTP-Version: – Status-Code: three digits, e.g 200, 404 – Reason-Phrase: OK, Not found

• Examples: – 200 OK – 404 Not Found • (The requested resource doesn't exist.)

Page 38: Mufix Network Programming Lecture

Response status codes

Page 39: Mufix Network Programming Lecture

Minimal HTTP Client

Page 40: Mufix Network Programming Lecture

HTTP Connection Management

Serial connection:

Page 41: Mufix Network Programming Lecture

Parallel connection

Each transaction opens/closes a new connection, costing time and bandwidth.

Each new connection has reduced performance because of TCP slow start.

There is a practical limit on the number of open parallel connections.

Page 42: Mufix Network Programming Lecture

Persistent Connections

TCP connections that are kept open after transactions complete are called persistent connections

Page 43: Mufix Network Programming Lecture

Pipelined Connections

Multiple requests can be enqueued before the responses arrive

Page 44: Mufix Network Programming Lecture

To Find More…

http://www.w3.org/Protocols/rfc2616/rfc2616.html

Page 45: Mufix Network Programming Lecture

YouTube Downloader

http://www.youtube.com/watch?v=QvsQ9hYKq7c

Page 46: Mufix Network Programming Lecture

Proxy Demo

http://www.youtube.com/watch?v=QvsQ9hYKq7c

Page 47: Mufix Network Programming Lecture

Non Blocking Sockets

socket()

bind()

listen()

accept()

read()

write()

[blocked]

socket()

connect()

write()

read()

[blocked]

[blocked]

Server Client

When interaction is over, server loops to accept a new connection

Page 48: Mufix Network Programming Lecture

Non Blocking Sockets

socket.setblocking(0) Simply In Python, you use to make it non-blocking.

send, recv, connect and accept can return without having done anything.

You can check return code and error codes and generally drive yourself crazy.

Your app will grow large, buggy.

Page 49: Mufix Network Programming Lecture

let’s skip the brain-dead solutions and do it right.

Use select.

Page 50: Mufix Network Programming Lecture

Multiplexing Servers with select

Let’s pick one of the previous solustion (Threads)

threads and processes don't really run in parallel.

your operating system divides the computer's processing power among all active tasks.

This process of switching between tasks is sometimes called multiplexing.

And this is the idea behind SELECT

Page 51: Mufix Network Programming Lecture

Multiplexing Servers with select (CONTIUNED)

Servers can apply this technique without Threading nor forking.

By multiplexing client connections and the main dispatcher with the select system call, a single event loop can process clients and accept new ones in parallel .

the magic behind this server structure is the operating system select call

select is asked to monitor a list of input sources, output sources, and exceptional condition sources and tells us which sources are ready for processing

Page 52: Mufix Network Programming Lecture

select-based echo server

Page 53: Mufix Network Programming Lecture

select-based echo server (continued)

Page 54: Mufix Network Programming Lecture

Questions