Your SlideShare is downloading. ×
0
Writing Network Programs with Python Nile University, CIT-614 Network Programming and Distributed Objects, By  Dr Sameh Al...
Agenda <ul><li>Talk  - Are We really Engineers? </li></ul><ul><li>Python (Programming language). </li></ul>
Engineering? <ul><li>Software: is it really engineering? </li></ul><ul><ul><ul><li>If it is not engineering, then what is ...
Engineering? <ul><li>Art:   the production or expression of what is beautiful, appealing, or of more than ordinary signifi...
Engineering? <ul><li>Engineering:   The art of making practical application of the knowledge of pure science. </li></ul><u...
Engineering? <ul><li>The profession in which a knowledge of the mathematical and natural sciences gained by study, experie...
Talk <ul><li>SEI (Software Engineering Institute) </li></ul><ul><li>Software Product Line (Development, Testing and Produc...
To Find More…
<ul><li>Let’s Start our Lecture </li></ul>
Python <ul><li>Python is a programming language that lets you work more quickly and integrate your systems more effectivel...
Python <ul><li>no explicit variable declarations </li></ul><ul><li>Variables spring into existence by being assigned a val...
Python <ul><li>Run: </li></ul><ul><ul><li>Python </li></ul></ul><ul><ul><li>>>>print “Hello MUFIX” </li></ul></ul><ul><li>...
Python <ul><li>.py file is called a module. </li></ul><ul><li>To declare a function use “def:” </li></ul><ul><li>Args sepa...
Python Indenting Code <ul><li>no explicit  begin or end,  and no curly  braces. </li></ul>
Python Lists <ul><li>Like java ArrayList but more powerful. </li></ul><ul><li>>>> list = [“a”, “b”, “c”, “d”] </li></ul><u...
Python Lists <ul><li>Negative List indices </li></ul><ul><li>>>>list </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul><ul>...
Python Lists <ul><li>Slicing a list </li></ul><ul><li>>>>list </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul><ul><li>>>>...
Python Lists <ul><li>Operations: like java ArrayList methods </li></ul><ul><li>List.append() </li></ul><ul><li>List.insert...
Python Dictionaries <ul><li>Like Java HashMap but a simple one. </li></ul><ul><li>A key / value pairs </li></ul><ul><li>>>...
Python Dictionaries <ul><li>•  len(d) </li></ul><ul><li>•  d[key] </li></ul><ul><li>•  d[key] = value </li></ul><ul><li>• ...
Tuples <ul><li>>>> T = (“a”, “b”, “c”, “d”) </li></ul><ul><li>>>> T </li></ul><ul><li>(“a”, “b”, “c”, “d”) </li></ul><ul><...
Python Strings <ul><li>>>> names = “C++, Java, Python, Ruby, C#” </li></ul><ul><li>>>>list = names.split(“,”) </li></ul><u...
Python and Files
To Find More…
Network Programming <ul><li>Browsers, chat clients, Downloads…..etc </li></ul>
Internet Socket and TCP/IP <ul><li>an  Internet socket  or  network socket  is an endpoint of a bidirectional inter-proces...
Socket types <ul><li>Datagram sockets , also known as connectionless sockets, which use User Datagram Protocol (UDP) </li>...
Connection-Oriented Services [blocked] [blocked] [blocked] Server Client When interaction is over, server loops to accept ...
First Network Program – Echo Server
Client
File Like Objects
HTTP RFC 2616
HTTP Request and Response
HTTP Request <ul><li>•  The format of the initial line for requests and </li></ul><ul><li>responses is different </li></ul...
HTTP Request
Request Methods
Response Initial line <ul><li>•  The Response initial (Called  Status line): </li></ul><ul><ul><li>–  HTTP-Version: </li><...
Response status codes
Minimal HTTP Client
HTTP Connection Management <ul><li>Serial connection: </li></ul>
Parallel connection <ul><li>Each transaction opens/closes a new connection, costing time and bandwidth.  </li></ul><ul><li...
Persistent Connections <ul><li>TCP connections that are kept open after transactions complete are called  persistent  conn...
Pipelined Connections <ul><li>Multiple requests can be enqueued before the responses arrive </li></ul>
To Find More… <ul><li>http://www.w3.org/Protocols/rfc2616/rfc2616.html </li></ul>
<ul><li>YouTube Downloader </li></ul><ul><li>http://www.youtube.com/watch?v=QvsQ9hYKq7c </li></ul>
<ul><li>Proxy Demo </li></ul><ul><li>http://www.youtube.com/watch?v=QvsQ9hYKq7c </li></ul>
Non Blocking Sockets [blocked] [blocked] [blocked] Server Client When interaction is over, server loops to accept a new co...
Non Blocking Sockets <ul><li>socket.setblocking(0)   Simply In Python, you use to make it non-blocking. </li></ul><ul><li>...
let’s skip the brain-dead solutions and do it right. <ul><li>  Use select. </li></ul>
Multiplexing Servers with select <ul><li>Let’s pick one of the previous solustion (Threads)  </li></ul><ul><li>threads and...
Multiplexing Servers with select (CONTIUNED) <ul><li>Servers can apply this technique without Threading nor forking. </li>...
select-based echo server
select-based echo server (continued)
Questions
Upcoming SlideShare
Loading in...5
×

Mufix Network Programming Lecture

929

Published on

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
929
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Python Network Programming LinuxWorld, New York, January 20, 2004 Steve Holden, Holden Web LLC A connection-oriented server creates a socket, binds it to one or more local ports on which it will listen for connections, and then puts the socket into the listening state to wait for incoming connections. At this point the server process blocks until a connection request arrives. A client creates its own socket, usually without specifying any particular port number, and then connects to the endpoint the server is listening on. The server’s accept() call returns a new socket that the server can use to send data across this particular connection.. The two parties then exchange data using read() and write() calls. The major limitation of this structure is the non-overlapped nature of the request handling in the server. Theoretically it&apos;s possible for the server to use its original socket to listen for further requests while the current request is being handled, but that isn&apos;t shown here. You will learn how to overcome this limitation using standard library classes.
  • Python Network Programming LinuxWorld, New York, January 20, 2004 Steve Holden, Holden Web LLC A connection-oriented server creates a socket, binds it to one or more local ports on which it will listen for connections, and then puts the socket into the listening state to wait for incoming connections. At this point the server process blocks until a connection request arrives. A client creates its own socket, usually without specifying any particular port number, and then connects to the endpoint the server is listening on. The server’s accept() call returns a new socket that the server can use to send data across this particular connection.. The two parties then exchange data using read() and write() calls. The major limitation of this structure is the non-overlapped nature of the request handling in the server. Theoretically it&apos;s possible for the server to use its original socket to listen for further requests while the current request is being handled, but that isn&apos;t shown here. You will learn how to overcome this limitation using standard library classes.
  • Transcript of "Mufix Network Programming Lecture"

    1. 1. Writing Network Programs with Python Nile University, CIT-614 Network Programming and Distributed Objects, By Dr Sameh Al Ansary , modified by Ahmed Kishk
    2. 2. Agenda <ul><li>Talk - Are We really Engineers? </li></ul><ul><li>Python (Programming language). </li></ul>
    3. 3. Engineering? <ul><li>Software: is it really engineering? </li></ul><ul><ul><ul><li>If it is not engineering, then what is it? </li></ul></ul></ul><ul><ul><ul><ul><li>Art? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Craft? </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Science? </li></ul></ul></ul></ul>
    4. 4. Engineering? <ul><li>Art: the production or expression of what is beautiful, appealing, or of more than ordinary significance. </li></ul><ul><li>Craft: A trade or occupation requiring special skill, especially manual skill. </li></ul><ul><li>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. </li></ul>
    5. 5. Engineering? <ul><li>Engineering: The art of making practical application of the knowledge of pure science. </li></ul><ul><li>Simply it is a profession. </li></ul>
    6. 6. Engineering? <ul><li>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. </li></ul><ul><ul><ul><li>Accreditation board for Engineering & Technology, 1996 </li></ul></ul></ul>
    7. 7. Talk <ul><li>SEI (Software Engineering Institute) </li></ul><ul><li>Software Product Line (Development, Testing and Production Environment) </li></ul><ul><li>Skill y0ur self with Software Engineering professionalism (Testing -Architecture Design and Documentation) </li></ul><ul><li>Doing Unit Testing, Regression Testing, Integration Testing, System Testing and Smoke Testing. </li></ul><ul><li>Web Application Engineering </li></ul><ul><li>You have to have critical thinking. </li></ul><ul><li>Read Code Complete </li></ul>
    8. 8. To Find More…
    9. 9. <ul><li>Let’s Start our Lecture </li></ul>
    10. 10. Python <ul><li>Python is a programming language that lets you work more quickly and integrate your systems more effectively. </li></ul><ul><li>Download the compiler from http://www.python.org/ </li></ul><ul><li>PyDev is a plugin for python in Eclipse IDE. </li></ul><ul><li>You can find installation steps at below link http://pydev.org/manual_101_install.html </li></ul>
    11. 11. Python <ul><li>no explicit variable declarations </li></ul><ul><li>Variables spring into existence by being assigned a value </li></ul><ul><li>automatically destroyed when they go out of scope. </li></ul>
    12. 12. Python <ul><li>Run: </li></ul><ul><ul><li>Python </li></ul></ul><ul><ul><li>>>>print “Hello MUFIX” </li></ul></ul><ul><li>Weak typed language: </li></ul><ul><ul><li>>>> x = 5 </li></ul></ul><ul><ul><li>>>>x </li></ul></ul><ul><ul><li>5 </li></ul></ul><ul><ul><li>>>>type(x) </li></ul></ul><ul><ul><li><type 'int'> </li></ul></ul><ul><ul><li>>>>x = “MUFIX” </li></ul></ul><ul><ul><li>>>>x </li></ul></ul><ul><ul><li>MUFIX </li></ul></ul><ul><ul><li>>>>type(x) </li></ul></ul><ul><ul><li><type 'str'> </li></ul></ul>
    13. 13. Python <ul><li>.py file is called a module. </li></ul><ul><li>To declare a function use “def:” </li></ul><ul><li>Args separated by “,” </li></ul><ul><li>No return types </li></ul><ul><li>use the “return” keyword to return values </li></ul>
    14. 14. Python Indenting Code <ul><li>no explicit begin or end, and no curly braces. </li></ul>
    15. 15. Python Lists <ul><li>Like java ArrayList but more powerful. </li></ul><ul><li>>>> list = [“a”, “b”, “c”, “d”] </li></ul><ul><li>>>>list </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul><ul><li>>>> List[0] </li></ul><ul><li>“ a” </li></ul><ul><li>>>> List[3] </li></ul><ul><li>“ d” </li></ul>
    16. 16. Python Lists <ul><li>Negative List indices </li></ul><ul><li>>>>list </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul><ul><li>>>>list[-1] </li></ul><ul><li>“ d” </li></ul><ul><li>>>>list[-3] </li></ul><ul><li>“ b” </li></ul>
    17. 17. Python Lists <ul><li>Slicing a list </li></ul><ul><li>>>>list </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul><ul><li>>>>list[1:3] </li></ul><ul><li>[“b”, “c”] </li></ul><ul><li>>>>list[1, -1] </li></ul><ul><li>[“b”, “c”] </li></ul><ul><li>>>>list[:3] </li></ul><ul><li>[“a”, “b”, “c”] </li></ul><ul><li>>>>list[:] </li></ul><ul><li>[“a”, “b”, “c”, “d”] </li></ul>
    18. 18. Python Lists <ul><li>Operations: like java ArrayList methods </li></ul><ul><li>List.append() </li></ul><ul><li>List.insert() </li></ul><ul><li>List.extend() </li></ul><ul><li>List.index() </li></ul><ul><li>List.pop() </li></ul>
    19. 19. Python Dictionaries <ul><li>Like Java HashMap but a simple one. </li></ul><ul><li>A key / value pairs </li></ul><ul><li>>>> Info = {“name”:”Ahmed”, “job”:”Software Engineer”} </li></ul><ul><li>>>>Info[“name”] </li></ul><ul><li>“ Ahmed” </li></ul><ul><li>>>>Info[“College”] = “MUFIC” </li></ul><ul><li>>>>Info </li></ul><ul><li>{“name”:”Ahmed”, “job”:”Software Engineer”, “College”:”MUFIC”} </li></ul>
    20. 20. Python Dictionaries <ul><li>• len(d) </li></ul><ul><li>• d[key] </li></ul><ul><li>• d[key] = value </li></ul><ul><li>• del d[key] </li></ul><ul><li>• key in d </li></ul><ul><li>• key not in d </li></ul><ul><li>• clear() </li></ul><ul><li>• copy() </li></ul><ul><li>• items() </li></ul><ul><li>• keys() </li></ul><ul><li>• values() </li></ul>
    21. 21. Tuples <ul><li>>>> T = (“a”, “b”, “c”, “d”) </li></ul><ul><li>>>> T </li></ul><ul><li>(“a”, “b”, “c”, “d”) </li></ul><ul><li>Read-Only Lists </li></ul><ul><li>Have no methods </li></ul><ul><li>Like java Enums </li></ul>
    22. 22. Python Strings <ul><li>>>> names = “C++, Java, Python, Ruby, C#” </li></ul><ul><li>>>>list = names.split(“,”) </li></ul><ul><li>['C++', ' Java', ' Python', ' Ruby', ' C#'] </li></ul><ul><li>>>>names = ” ”.join(list) </li></ul><ul><li>>>>names </li></ul><ul><li>” C++ Java Python Ruby C#” </li></ul>
    23. 23. Python and Files
    24. 24. To Find More…
    25. 25. Network Programming <ul><li>Browsers, chat clients, Downloads…..etc </li></ul>
    26. 26. Internet Socket and TCP/IP <ul><li>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. </li></ul><ul><li>Internet sockets  is also used as a name for an (API) for the TCP/IP protocol stack </li></ul><ul><li>provided by the operating system. </li></ul><ul><li>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. </li></ul>
    27. 27. Socket types <ul><li>Datagram sockets , also known as connectionless sockets, which use User Datagram Protocol (UDP) </li></ul><ul><li>Stream sockets , also known as connection-oriented sockets, which use Transmission Control Protocol (TCP) or Stream Control Transmission Protocol (SCTP). </li></ul><ul><li>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). </li></ul>
    28. 28. Connection-Oriented Services [blocked] [blocked] [blocked] Server Client When interaction is over, server loops to accept a new connection socket() bind() listen() accept() read() write() socket() connect() write() read()
    29. 29. First Network Program – Echo Server
    30. 30. Client
    31. 31. File Like Objects
    32. 32. HTTP RFC 2616
    33. 33. HTTP Request and Response
    34. 34. HTTP Request <ul><li>• The format of the initial line for requests and </li></ul><ul><li>responses is different </li></ul><ul><li>• The Request line contains: </li></ul><ul><li>– Method: </li></ul><ul><li>• GET: The most common </li></ul><ul><li>• Others: POST, HEAD, PUT,… </li></ul><ul><li>– Request-URI </li></ul><ul><li>• Exampe: bla/directory17/hello.html </li></ul><ul><li>– HTTP-Version </li></ul><ul><li>• Example The string: “HTTP/1.1” </li></ul>
    35. 35. HTTP Request
    36. 36. Request Methods
    37. 37. Response Initial line <ul><li>• The Response initial (Called Status line): </li></ul><ul><ul><li>– HTTP-Version: </li></ul></ul><ul><ul><li>– Status-Code: three digits, e.g 200, 404 </li></ul></ul><ul><ul><li>– Reason-Phrase: OK, Not found </li></ul></ul><ul><li>• Examples: </li></ul><ul><ul><li>– 200 OK </li></ul></ul><ul><ul><li>– 404 Not Found </li></ul></ul><ul><ul><li>• (The requested resource doesn't exist.) </li></ul></ul>
    38. 38. Response status codes
    39. 39. Minimal HTTP Client
    40. 40. HTTP Connection Management <ul><li>Serial connection: </li></ul>
    41. 41. Parallel connection <ul><li>Each transaction opens/closes a new connection, costing time and bandwidth. </li></ul><ul><li>Each new connection has reduced performance because of TCP slow start. </li></ul><ul><li>There is a practical limit on the number of open parallel connections. </li></ul>
    42. 42. Persistent Connections <ul><li>TCP connections that are kept open after transactions complete are called persistent connections </li></ul>
    43. 43. Pipelined Connections <ul><li>Multiple requests can be enqueued before the responses arrive </li></ul>
    44. 44. To Find More… <ul><li>http://www.w3.org/Protocols/rfc2616/rfc2616.html </li></ul>
    45. 45. <ul><li>YouTube Downloader </li></ul><ul><li>http://www.youtube.com/watch?v=QvsQ9hYKq7c </li></ul>
    46. 46. <ul><li>Proxy Demo </li></ul><ul><li>http://www.youtube.com/watch?v=QvsQ9hYKq7c </li></ul>
    47. 47. Non Blocking Sockets [blocked] [blocked] [blocked] Server Client When interaction is over, server loops to accept a new connection socket() bind() listen() accept() read() write() socket() connect() write() read()
    48. 48. Non Blocking Sockets <ul><li>socket.setblocking(0) Simply In Python, you use to make it non-blocking. </li></ul><ul><li>send ,  recv ,  connect  and  accept  can return without having done anything. </li></ul><ul><li>You can check return code and error codes and generally drive yourself crazy. </li></ul><ul><li>Your app will grow large, buggy. </li></ul>
    49. 49. let’s skip the brain-dead solutions and do it right. <ul><li> Use select. </li></ul>
    50. 50. Multiplexing Servers with select <ul><li>Let’s pick one of the previous solustion (Threads) </li></ul><ul><li>threads and processes don't really run in parallel. </li></ul><ul><li>your operating system divides the computer's processing power among all active tasks. </li></ul><ul><li>This process of switching between tasks is sometimes called multiplexing . </li></ul><ul><li>And this is the idea behind SELECT </li></ul>
    51. 51. Multiplexing Servers with select (CONTIUNED) <ul><li>Servers can apply this technique without Threading nor forking. </li></ul><ul><li>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 . </li></ul><ul><li>the magic behind this server structure is the operating system select call </li></ul><ul><li>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 </li></ul>
    52. 52. select-based echo server
    53. 53. select-based echo server (continued)
    54. 54. Questions
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×