Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
slide 2 gaius slide 1 gaius Python can be simple Introduction to Python #!/usr/bin/python is more powerful than Tcl applicable to larger systems development print "hello world" has cleaner syntax than Perl easier to maintain does not compete head on with Java python is a scripting language whereas Java is a systems language similar to C++ slide 3 gaius Python Modules allow for problem decomposition slide 4 gaius Alternative import #!/usr/bin/python similar to Modula-2 from myfile import title print title #!/usr/bin/python title = "hello world" above is called myfile.py #!/usr/bin/python import myfile print myfile.title whereas this file is called hello2.py when run prints hello world note that all python modules need to be saved as name.py so in our example the module myfile was saved into a file called myfile.py slide 5 gaius slide 6 gaius Python builtin types Builtin objects python contains many builtin types use them.. builtin objects make simple programs easy to understand lists, dictionaries, exist, don’t reinvent the wheel numbers strings lists dictionaries tuples files 3.14159, 1234 ’spam’, "fred’s" [1, [2, ’three’], 4] {’food’:’spam’, ’taste’:’yum’} (1, ’spam’, 4, ’U’) text=open(’/etc/passwd’, ’r’).read() built in objects are more efficient than custom data types slide 7 gaius slide 8 gaius Expression operators or, and, not <, <=, >, >=, ==, <>, != x|y z&y x << y x >> y x[i] x[i:y] x.y x(y) logical operators (short circuit) comparison operators bitwise or bitsize and shift left by y bits shift right by y bits indexing slicing qualifying (imports) function calls Strings concatenation via + repeated via * #!/usr/bin/python print "hi " * 4 yields hi hi hi hi slide 9 gaius slide 10 gaius Slicing Slicing given a string in Python first character has index 0 and also -11 last character index is 10 in this example last character index is also -1 s = "hello world" you can obtain portion of string via: s[2:5] yields: llo negative values start at right and move to the left strings can be sliced using positive and negative values slide 11 gaius slide 12 gaius Statements assignment, calls, print, if/else/elif, for, while, break/continue Statements class assert try, except, raise, exec def, return function definitions and returning values del global slide 13 gaius Example 8 times table slide 14 gaius Constructing a simple client and server the following should be called client.py #!/usr/bin/python for n in range(1, 13): print n, "x 8 =", n*8 #!/usr/bin/python import sys from socket import * serverHost = ’localhost’ serverPort = 2000 # create a TCP socket s = socket(AF_INET, SOCK_STREAM) s.connect((serverHost, serverPort)) s.send(’Hello world’) data = s.recv(1024) print data slide 15 gaius server.py slide 16 gaius Run the server and client from the command terminal #!/usr/bin/python from socket import * myHost = ’’ myPort = 2000 # create a TCP socket s = socket(AF_INET, SOCK_STREAM) # bind it to the server port number s.bind((myHost, myPort)) # allow 5 pending connections s.listen(5) while True: # wait for next client to connect connection, address = s.accept() while True: data = connection.recv(1024) if data: print "received data", data connection.send(’echo -> ’ + data) else: break connection.close() start the server first $ python server.py now run the client in a different terminal: $ python client.py echo -> Hello world slide 17 gaius slide 18 gaius Python has many support modules Python has many support modules see the global module index in the python online docs 〈http://floppsie.comp.glam.ac.uk/ python/html/index.html〉 read the documentation 〈http:// docs.python.org/library/struct.html〉 and write a small program to print your username in with 20 characters take note of the struct module your program should output this module provides a list of method which performs conversions between Python values C objects 01234567890123456789 <fred > it can be very usefully employed in constructing protocols it uses format strings as compact descriptions of the layout data similar to printf and friends in C slide 19 gaius Python has many support modules slide 20 gaius Write a Python program to output your UID as two bytes in network order #!/usr/bin/python #!/usr/bin/python import struct import getpass print " 01234567890123456789" print "<", username = getpass.getuser() print username, print ">", # answer print "<", print struct.pack("20s", username), print ">", the program above pads your username with character 0’s to make up a 20 character string $ python teststruct.py 0000000 3020 3231 3433 0000020 3635 3837 0a39 0000040 203c 7266 6465 0000060 0000 0000 0000 0000071 | od 3635 203c 0000 3e20 -x 3837 3039 3231 3433 6167 7569 2073 203e 0000 0000 0000 0000 000a import struct import os print os.getuid() # answer print struct.pack("!H", os.getuid()) slide 21 gaius TFTP client implementation coursework support useful references rfc1350 〈http://www.faqs.org/rfcs/ rfc1350.html〉 python struct module 〈http:// docs.python.org/library/ struct.html〉 you will need to use the select function in Python to wait for a tftp packet to discover a timeout slide 22 gaius select look for the line of code in tftp-skel.py which matches r, w, e = select.select([sock], [], [], 5.0) it follows the behaviour found in select 〈http:// docs.python.org/library/select.html〉 when your code works change this line to r, w, e = glamnetsim.simselect([sock], [], [], 5.0) as this function behaves in exactly the same way but it introduces occasional errors slide 23 gaius Skeleton coursework code slide 24 gaius TFTP client implementation coursework support tftp-skel.py 〈tftp-skel.py〉 glamnetsim.py 〈glamnetsim.py〉 useful references rfc1350 〈http://www.faqs.org/rfcs/ rfc1350.html〉 python struct module 〈http:// docs.python.org/library/ struct.html〉 you will need to use the select function in Python to wait for a tftp packet to discover a timeout slide 25 gaius slide 26 gaius select select use this function to obtain a packet or wait for a timeout when this function returns the values of w and e will always be [] but the value of r will either be [] or [sock] r, w, e = select.select([sock], [], [], 5.0) it follows the behaviour found in select 〈http:// docs.python.org/library/select.html〉 when your code works change this line to r, w, e = glamnetsim.simselect([sock], [], [], 5.0) as this function behaves in exactly the same way but it introduces the occasional timeout event slide 27 gaius select to read the packet from the socket you can: (packet, (address, port)) = sock.recvfrom(512+4) which reads up to 516 bytes of raw data into packet and assigns the IP address and port value of the tftp server the value [] means a timeout occurred and the value [sock] means this socket has some data which is ready to be read