Presentation is loading. Please wait.

Presentation is loading. Please wait.

 socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g.,

Similar presentations


Presentation on theme: " socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g.,"— Presentation transcript:

1

2  socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g., socket address) become return values  Socket address is a tuple: (host, port)  sendall == writen() in UNP

3 주의 : File 은 Windows 에서 select() 할 수 없다.

4

5 IterativeMulti-threadsProcesses BaseServerThreadingMixInForkingMixIn TCPServerThreadingTCPServerForkingTCPServer UnixStreamServerThreadingUnixStreamServerForkingUnixStreamServer UDPServerThreadingUDPServerForkingUDPServer UnixDatagramServerThreadingUnixDatagramServerForkingUnixDatagramServer Class Inheritance BaseRequestHandler StreamRequestHandlerSupport file-like methods -self.rfile: buffered -self.wfile: unbuffered DatagramRequestHandlerSupport StringIO - self.rfile and self.wfil Server Classes Inheritance Handler Classes Inheritance server = ThreadingTCPServer((‘’, port)), StreamRequestHandler) server.serve_forever() handle() method 만 작성 하면 충분 !! Running Server

6

7

8

9  thread module (_thread in Python 3) ◦ Low-level thread  use threading module  threading module: object-oriented way on the thread module ◦ Define target function ◦ or write subclass of Thread overriding run() method

10 import time from threading import Thread def sleeper(i): print "thread %d sleeps for 5 seconds" % i time.sleep(5) print "thread %d woke up" % i for i in range(10): t = Thread(target=sleeper, args=(i,)) t.start() import threading, zipfile class AsyncZip(threading.Thread): def __init__(self, infile, outfile): threading.Thread.__init__(self) self.infile = infile self.outfile = outfile def run(self): f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED) f.write(self.infile) f.close() print 'Finished background zip of: ', self.infile background = AsyncZip('mydata.txt', 'myarchive.zip') background.start() print 'The main program continues to run in foreground.' background.join() # Wait for the background task to finish print 'Main program waited until background was done.' Override run() method

11

12 import threading some_lock = threading.Lock() with some_lock: print "some_rlock is locked while this executes"

13 def worker(): while True: item = q.get() do_work(item) q.task_done() q = Queue() for i in range(num_worker_threads): t = Thread(target=worker) t.daemon = True t.start() for item in source(): q.put(item) q.join() # block until all tasks are done

14 >>> import urllib2 >>> for line in urllib2.urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):... if 'EST' in line or 'EDT' in line: # look for Eastern Time... print line Nov. 25, 09:43:32 PM EST >>> import smtplib >>> server = smtplib.SMTP('localhost') >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',... """To: jcaesar@example.org... From: soothsayer@example.org...... Beware the Ides of March.... """) >>> server.quit()

15 import struct source_ip = '192.168.1.101' dest_ip = '192.168.1.1' # or socket.gethostbyname('www.google.com') # ip header fields ip_ihl = 5 ip_ver = 4 ip_tos = 0 ip_tot_len = 0 # kernel will fill the correct total length ip_id = 54321 #Id of this packet ip_frag_off = 0 ip_ttl = 255 ip_proto = socket.IPPROTO_TCP ip_check = 0 # kernel will fill the correct checksum ip_saddr = socket.inet_aton ( source_ip ) #Spoof the source ip address if you want to ip_daddr = socket.inet_aton ( dest_ip ) ip_ihl_ver = (version << 4) + ihl # the ! in the pack format string means network order ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)


Download ppt " socket.socket() returns socket object: _socketobject  Most of socket API are methods on socket objects or functions  Value result arguments (e.g.,"

Similar presentations


Ads by Google