1 / 34

Python Client/Server

Python Client/Server. Shuai Zhao. What is Python?. Python is a object-oriented scripting language An interpreted language It has simple, clear syntax,and easy to learn. Network programming is a major use of Python

sthomason
Download Presentation

Python Client/Server

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python Client/Server Shuai Zhao

  2. What is Python? • Python is a object-oriented scripting language • An interpreted language • It has simple, clear syntax,and easy to learn. • Network programming is a major use of Python • Python standard library has wide support for network protocols, data encoding/decoding, and other things you need to make it work • Writing network programs in Python tends to be substantially easier than in C/C++

  3. Python 2.x Vs 3.x • Python 2.x is legacy • Python 3.x for the future

  4. A Simple Python Example $ cat hello.py #!/usr/bin/env python print "hello World” $ python Helloworld.py hello World

  5. Lines and Indentation • Python does not use braces {} to indicate blocks of code for class/methods/flow control • indentation is enforced • the amount of space can be changed but not be consistent ex: def function(x): if Ture: pass else: pass return

  6. Python indentation • Python functions have no explicit begin or end, and no curly braces to mark where the function code starts and stops. The only delimiter is a colon (:) and the indentation of the code itself. • def func1(): • your code starts here • def func2(): • your code starts here

  7. Standard Data types • Numbers: int, float, complex • String: ‘ ’, “ ” • List [] • Tuple () • Dictionary {}

  8. define variables • no need explicit declaration when define variables var1 = 1 var11 = 1.1 var2 = “Hello” var3 = (1,2,3) var4 = [1,2,3] var5 = {‘a’: 1, ‘b’:2}

  9. Python containers • List [] • Tuple () • dictionary {key:value}

  10. Python List list1 = [1, 2.1, ’abcde’] print list1 print list1[0] print list1[1:] print list1[1:2] list2 = [‘1’, ’100x’] print list1 + list2

  11. Python dictionary • dict1 = {‘Key’: Value} • dict1['one'] = "This is one" • dict1[2] = "This is two" • anotherDict = {'name': 'john','code':6734, 'dept': 'sales'} • print dict['one’]1 # Prints value for 'one' key • print dict[2] # Prints value for 2 key • print anotherDict # Prints complete dictionary • print anotherDict.keys() # Prints all the keys • print anotherDict .values() # Prints all the values

  12. python tuple tuple1 = (1, 2.1, ’abcde’) print tuple1 print tuple1[0] print tuple1[1:] print tuple1[1:2] tuple2 = [‘1’, ’100x’] print tuple1 + tuple2

  13. Python network address • Each endpoint of a network connection is always represented by a host and port # • In Python you write it out as a tuple (host,port) • (”www.umkc.edu",80) • (”www.google.com",443)

  14. Python Socket Basic • To create a socket import socket s = socket.socket(addr_family, type) • Address families socket.AF_INET Internet protocol (IPv4) socket.AF_INET6 Internet protocol (IPv6) • Socket types socket.SOCK_STREAM Connection based stream (TCP) socket.SOCK_DGRAM Datagrams (UDP) • Example: from socket import * s = socket(AF_INET,SOCK_STREAM)

  15. Socket Types • Almost all code will use one of following from socket import * s = socket(AF_INET, SOCK_STREAM) s = socket(AF_INET, SOCK_DGRAM)

  16. Using a Socket • Creating a socket is only the first step s = socket(AF_INET, SOCK_STREAM) • Server Listen for incoming connections • Client Make an outgoing connection

  17. A TCP Client • How to make an outgoing connection from socket import * s = socket(AF_INET,SOCK_STREAM) s.connect((”127.0.0.1”,9000)) # Connect data = s.recv(10000) s.close()

  18. A TCP Server from socket import * s = socket(AF_INET,SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind((“”,9000)) s.listen(5) while True: c,a = s.accept() print "Received connection from", a c.send("Hello %s:%s\n" % (a[0],a[1])) c.close()

  19. Partial Reads/Writes • Be aware that reading/writing to a socket may involve partial data transfer • send() returns actual bytes sent • recv() length is only a maximum limit • >>> len(data)1000000>>> s.send(data)37722 Sent partial data >>> • >>> data = s.recv(10000)>>> len(data)6420 Received less than max >>>

  20. Partial Reads/Writes • Be aware that for TCP, the data stream is continuous---no concept of records, etc. # Client ... s.send(data) s.send(moredata) ... # Server ... data = s.recv(maxsize) ... • This recv() may return data from both of the sends combined or less data than even the first send • A lot depends on OS buffers, network bandwidth, congestion, etc.

  21. Sending All Data: sendall() • To wait until all data is sent, use sendall() s.sendall(data) • Blocks until all data is transmitted • For most normal applications, this is what you should use • Exception :You don’t use this if networking is mixed in with other kinds of processing(e.g., screen updates, multitasking, etc.)

  22. End of Data • How to tell if there is no more data? • recv() will return empty string >>> s.recv(1000) “”>>> • This means that the other end of the connection has been closed (no more sends)

  23. Data Reassembly Coding template • Receivers often need to reassemble messages from a series of small chunks • Here is a programming template for that fragments = [] # List of chunks while not done: chunk = s.recv(maxsize) # Get a chunk if not chunk: break # EOF. No more data fragments.append(chunk) # Reassemble the message message = "".join(fragments) • Don't use string concat (+=). It's slow.

  24. UDP Datagrams • Data sent in discrete packets (Datagrams) • No concept of a "connection"• No reliability, no ordering of data• Datagrams may be lost, arrive in any order • Higher performance (used in games, etc.)

  25. A UDP Server from socket import * maxsize= 1000 s = socket(AF_INET,SOCK_DGRAM) s.bind(("",10000)) while True: data, addr = s.recvfrom(maxsize) print "Received From client at %s saying %s" % (addr, data) resp = "Hi!" s.sendto(resp,addr)

  26. A UDP Client from socket import * maxsize = 1000 s = socket(AF_INET,SOCK_DGRAM) msg = "Hello World" s.sendto(msg,("server.com",10000)) data, addr = s.recvfrom(maxsize) print "Reveive %s from server at %s" % (data, addr)

  27. Unix Domain Sockets • s = socket(AF_UNIX, SOCK_STREAM) • s = socket(AF_UNIX, SOCK_DGRAM) s.bind("/tmp/foo") # Server binding s.connect("/tmp/foo") # Client connection

  28. Unix Domain Sockets Server from socket import * maxsize = 1000s = socket(AF_UNIX,SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind(“/tmp/unixsocket”) s.listen(5) while True: c,a= s.accept() c.send(“Hello ”) c.close()

  29. Unix Domain Sockets Client from socket import * s = socket(AF_UNIX, SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.connect(“/tmp/unixsocket”) # Connect data = s.recv(10000) print "Received from server: ", datas.close()

  30. Concurrent Server • Each client is handled by a separate thread • Each client is handled by a separate process • Apparent concurrency server

  31. multi-thread server (concurrent_server_1.py) import threading from socket import * defhandle_client(c, a): # do you thing here print "Handling connection from %s: ", a c.send("Hello %s:%s\n" % (a[0], a[1])) c.close() return s = socket(AF_INET, SOCK_STREAM) s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) s.bind(("", 9000)) s.listen(5) while True: c, a = s.accept() print "here is new connections." t = threading.Thread(target=handle_client, args=(c, a)) t.run()

  32. multi-process server import os from socket import * s = socket(AF_INET,SOCK_STREAM) s.bind(("",9000)) s.listen(5) while True: c,a = s.accept() if os.fork() == 0: # Child process. Manage client ... c.close() os._exit(0) else: # Parent process. Clean up and go # back to wait for more connections c.close()

  33. Apparent Concurrent server import select from socket import * s = socket(AF_INET,SOCK_STREAM) clients = [] # List of all active client sockets while True: # Look for activity on any of my sockets input,output,err = select.select(s+clients, clients, clients) # Process all sockets with input for i in input: ... # Process all sockets ready for output for o in output: ...

  34. Summary • covered the basics of network support that's bundled with Python • Concurrent servers• Multiprocessing

More Related