an introduction to twisted

41
An Introduction to Twisted Stacey Sern [email protected] @staceysern

Upload: sdsern

Post on 08-May-2015

215 views

Category:

Software


0 download

DESCRIPTION

An introduction to Python's Twisted event-driven, networking library (PyCon 2014).

TRANSCRIPT

Page 1: An Introduction to Twisted

An Introduction to Twisted !

Stacey Sern

[email protected] @staceysern

Page 2: An Introduction to Twisted

Web Server

twistd web --port tcp:8080 --path .

(HTTP)

Page 3: An Introduction to Twisted

Chat Server

twistd words --irc-port tcp:6667 --auth file:passwords.txt --group channel

(IRC)

Page 4: An Introduction to Twisted

Secure Shell Server

sudo twistd conch -p tcp:2222

(SSH)

Page 5: An Introduction to Twisted

Name Server

twistd dns -p 5553 --hosts-file=hosts

(DNS)

Page 6: An Introduction to Twisted

File Server

twistd ftp --port 2121 --root .

(FTP)

Page 7: An Introduction to Twisted

BitTorrent Client

https://github.com/staceysern/bittorrent

Page 8: An Introduction to Twisted

TwistedAn event-driven, networking engine

Page 9: An Introduction to Twisted

TwistedAn event-driven, networking engine

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

Page 10: An Introduction to Twisted

TwistedAn event-driven, networking engine

Page 11: An Introduction to Twisted

TwistedAn event-driven, networking engine

Page 12: An Introduction to Twisted

time

task 1

task 3

task 2

multi-threaded synchronous

single-threaded asynchronous

single-threaded synchronous

event-driven

Page 13: An Introduction to Twisted

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

TwistedAn event-driven, networking engine

Page 14: An Introduction to Twisted

ReactorTwisted’s event loop

Page 15: An Introduction to Twisted

ReactorTwisted’s event loop

Interface

server & client ! run() stop() callLater()

server !listenTCP() listenUDP() listenSSL()

client !connectTCP() connectSSL()

Page 16: An Introduction to Twisted

Reactor Code

callback Application Code

Page 17: An Introduction to Twisted

Deferred

Callback Chain Errback Chain

An abstraction for managing callbacks

...

Callback 2

Callback 1

Callback 3

Errback 2

...

Errback 1

Errback 3

Page 18: An Introduction to Twisted

Synchronoustry: p = getpage_sync()except Exception: unavailable()else: t = translate(p) display(t)finally: cleanup()

d = getpage_async()d.addCallbacks(translate, unavailable)d.addCallback(display)d.addBoth(cleanup)

Asynchronous

display

cleanup

unavailable

cleanup

translate

failure from getpage_async()

successful result from getpage_async()

Page 19: An Introduction to Twisted

cleanup()

getpage_sync()

translate()

display()

Synchronous

Page 20: An Introduction to Twisted

Asynchronous

translate()display()

cleanup()getpage_async()

addCallbacks()

addCallback()

addBoth()

Page 21: An Introduction to Twisted

callback2

both4

errback1

errback3

both4

callback1

successful result of asynchronous operation

failure from asynchronous operation

addCallbacks(callback1, errback1)

addCallback(callback2)

addErrback(errback3)

addBoth(both4)

Deferred

Page 22: An Introduction to Twisted

TwistedAn event-driven, networking engine

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

Page 23: An Introduction to Twisted

Application

Transport

Link

Network

HTTP, FTP, SMTP, POP, IMAP, DNS, IRC

TCP, UDP, SSL/TLS

IP

Ethernet

Internet Protocol Suite

Page 24: An Introduction to Twisted

Application

Transport

Link

Network

Client

Application

Transport

Link

Network

Server

0100100011001010010111

GET /index.html HTTP/1.1

Page 25: An Introduction to Twisted

Application

Transport

Link

Network

Internet

Protocol

Transport

OS

Twisted

Page 26: An Introduction to Twisted

ProtocolRepresents one side of an application layer protocol which determines the format and meaning of data sent and received over a Transport

TransportRepresents one end of a connection between two endpoints over which data can be sent and received

ProtocolFactory Used to create the appropriate Protocol when a new connection is established

Page 27: An Introduction to Twisted

Transport

OS

poll/event notification API

sockets API

write()writeSequence()

loseConnection()

connectionMade()dataReceived()connectionLost()

Protocol

Reactor

Page 28: An Introduction to Twisted

TwistedAn event-driven, networking engine

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

Page 29: An Introduction to Twisted

from twisted.internet import protocol, reactor!class EchoServer(protocol.Protocol): def dataReceived(self, data): self.transport.write(data)!class EchoServerFactory(protocol.Factory): def buildProtocol(self, addr): return EchoServer()!reactor.listenTCP(8000, EchoServerFactory())reactor.run()

Echo Server

Page 30: An Introduction to Twisted

from twisted.internet import protocol, reactor!class EchoClient(protocol.Protocol): def connectionMade(self): self.transport.write(b’Hello, world!’)! def dataReceived(self, data): print(data) self.transport.loseConnection()!class EchoClientFactory(protocol.ClientFactory): def buildProtocol(self, addr): return EchoClient()!reactor.connectTCP(’10.0.1.56’, 8000, EchoClientFactory())reactor.run()

Echo Client

Page 31: An Introduction to Twisted

Transport

OS

poll/event notification API

sockets API

write()writeSequence()

loseConnection()

connectionMade()dataReceived()connectionLost()

Protocol

Reactor

Page 32: An Introduction to Twisted

TwistedAn event-driven, networking engine

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

Page 33: An Introduction to Twisted

SMTPTrying 173.194.68.26...Connected to aspmx.l.google.com.Escape character is '^]'.220 mx.google.com ESMTP ew5si11028094qab.7 - gsmtp

$

HELO250 mx.google.com at your serviceMAIL FROM:<[email protected]>250 2.1.0 OK ew5si11028094qab.7 - gsmtpRCPT TO:<[email protected]>250 2.1.5 OK ew5si11028094qab.7 - gsmtpDATA354 Go ahead ew5si11028094qab.7 - gsmtpFrom: Sender <[email protected]>To: Recipient <[email protected]>Subject: This is a test!This is only a test..250 2.0.0 OK 1392752225 ew5si11028094qab.7 - gsmtp

telnet aspmx.l.google.com 25

Page 34: An Introduction to Twisted

SMTPClientA Protocol which implements the client side of the SMTP protocol

API

getMailFrom() getMailTo() getData() sentMail()

Page 35: An Introduction to Twisted

class SingleMessageSender(smtp.SMTPClient): def __init__(self, from_addr, to_addr, data): smtp.SMTPClient.__init__(self, None) self.from_addr = from_addr self.to_addr = to_addr self.data = data self.done = False! def getMailFrom(self): if not self.done: self.done = True return self.from_addr! def getMailTo(self): return [self.to_addr]! def getMailData(self): return self.data! def sentMail(self, code, resp, numOk, addresses, log): pass

Page 36: An Introduction to Twisted

• SMTPClient• SMTPSender• SMTPSenderFactory• sendmail()• Extended SMTP (ESMTP) equivalents

Twisted MailClient Building Blocks

Page 37: An Introduction to Twisted

• SMTP• ESMTP• twistd mail

Twisted MailServer Building Blocks

Page 38: An Introduction to Twisted

TwistedAn event-driven, networking engine

• Event-driven, networking engine• Event-driven programming abstractions• Networking abstractions• Low-level APIs• High-level APIs• Applications

Page 39: An Introduction to Twisted

twistd

• Starting and stopping• Logging• Daemonizing• Custom reactor• Profiling

A cross-platform utility for deploying Twisted applications

Page 40: An Introduction to Twisted

Twisted

• Event-driven, networking engine (building blocks)• Event-driven programming abstractions (Reactor, Deferred)• Networking abstractions (Transport, Protocol, ProtocolFactory)• Low-level APIs (TCP, UDP, SSL/TLS)• High-level APIs (HTTP, SMTP, FTP, SSH, IRC, DNS)• Applications (twistd)

An event-driven, networking engine

Page 41: An Introduction to Twisted

Resources

• An Introduction to Asynchronous Programming and Twisted (http://krondo.com/?p=1327)

• Twisted Network Programming Essentials - Jessica McKellar & Abe Fettig

• Twisted Website (https://twistedmatrix.com)