forked from cewing/training.python_web
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweek02.html
More file actions
652 lines (649 loc) · 21.8 KB
/
Copy pathweek02.html
File metadata and controls
652 lines (649 loc) · 21.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.9.1: http://docutils.sourceforge.net/" />
<meta name="version" content="S5 1.1" />
<title>Internet Programming with Python</title>
<link rel="stylesheet" href="ui/uw_pce_theme/pretty.css" type="text/css" />
<!-- configuration parameters -->
<meta name="defaultView" content="slideshow" />
<meta name="controlVis" content="hidden" />
<!-- style sheet links -->
<script src="ui/uw_pce_theme/slides.js" type="text/javascript"></script>
<link rel="stylesheet" href="ui/uw_pce_theme/slides.css"
type="text/css" media="projection" id="slideProj" />
<link rel="stylesheet" href="ui/uw_pce_theme/outline.css"
type="text/css" media="screen" id="outlineStyle" />
<link rel="stylesheet" href="ui/uw_pce_theme/print.css"
type="text/css" media="print" id="slidePrint" />
<link rel="stylesheet" href="ui/uw_pce_theme/opera.css"
type="text/css" media="projection" id="operaFix" />
</head>
<body>
<div class="layout">
<div id="controls"></div>
<div id="currentSlide"></div>
<div id="header">
</div>
<div id="footer">
<h1>Internet Programming with Python</h1>
<h2><a class="reference external" href="http://github.com/cewing/training.python_web">View document source</a>.
</h2>
</div>
</div>
<div class="presentation">
<div class="slide" id="slide0">
<h1 class="title">Internet Programming with Python</h1>
<img alt="img/protocol.png" class="align-left" src="img/protocol.png" style="width: 45%;" />
<p>Week 2: Web Protocols</p>
<p class="intro-blurb">Wherein we learn about the languages that the internet speaks and how to
choose the right one for our message</p>
</div>
<div class="slide" id="but-first">
<h1>But First</h1>
<p class="big-centered">Review from the Assignment</p>
</div>
<div class="slide" id="and-second">
<h1>And Second</h1>
<p class="big-centered">Questions from the Reading?</p>
</div>
<div class="slide" id="and-now">
<h1>And Now...</h1>
<img alt="img/protocol_sea.png" class="align-center" src="img/protocol_sea.png" style="width: 48%;" />
<p class="image-credit">image exerpted from: <a class="reference external" href="http://xkcd.com/802/">http://xkcd.com/802/</a></p>
</div>
<div class="slide" id="what-is-a-protocol">
<h1>What is a Protocol?</h1>
<p class="incremental big-centered">a set of rules or conventions</p>
<p class="incremental big-centered">governing communications</p>
</div>
<div class="slide" id="protocols-irl">
<h1>Protocols IRL</h1>
<p>Life has lots of sets of rules for how to do things.</p>
<ul class="incremental simple">
<li>What do you do on a first date?</li>
<li>What do you do in a job interview?</li>
<li>What do (and don't) you talk about at a dinner party?</li>
<li>...?</li>
</ul>
</div>
<div class="slide" id="id1">
<h1>Protocols IRL</h1>
<img alt="img/icup.png" class="align-center" src="img/icup.png" style="width: 60%;" />
<p class="image-credit"><a class="reference external" href="http://blog.xkcd.com/2009/09/02/urinal-protocol-vulnerability/">http://blog.xkcd.com/2009/09/02/urinal-protocol-vulnerability/</a></p>
</div>
<div class="slide" id="protocols-in-computers">
<h1>Protocols In Computers</h1>
<p>Digital life has lots of rules too:</p>
<ul class="incremental simple">
<li>how to identify yourself</li>
<li>how to find a partner</li>
<li>how to ask for information</li>
<li>how to provide answers</li>
<li>how to say goodbye</li>
</ul>
</div>
<div class="slide" id="real-protocol-examples">
<h1>Real Protocol Examples</h1>
<p class="big-centered">What does this look like in practice?</p>
</div>
<div class="slide" id="id2">
<h1>Real Protocol Examples</h1>
<ul class="incremental simple">
<li>SMTP (Simple Message Transfer Protocol)
<a class="reference external" href="http://tools.ietf.org/html/rfc5321#appendix-D">http://tools.ietf.org/html/rfc5321#appendix-D</a></li>
<li>POP3 (Post Office Protocol)
<a class="reference external" href="http://www.faqs.org/docs/artu/ch05s03.html">http://www.faqs.org/docs/artu/ch05s03.html</a></li>
<li>IMAP (Internet Message Access Protocol)
<a class="reference external" href="http://www.faqs.org/docs/artu/ch05s03.html">http://www.faqs.org/docs/artu/ch05s03.html</a></li>
<li>HTTP (Hyper-Text Transfer Protocol)
<a class="reference external" href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol</a></li>
</ul>
</div>
<div class="slide" id="what-does-smtp-look-like">
<h1>What does SMTP look like?</h1>
<p>SMTP (Identify yourself and find a partner):</p>
<pre class="literal-block">
S: 220 foo.com Simple Mail Transfer Service Ready
C: EHLO bar.com
S: 250-foo.com greets bar.com
S: 250-8BITMIME
S: 250-SIZE
S: 250-DSN
S: 250 HELP
</pre>
</div>
<div class="slide" id="id3">
<h1>What does SMTP look like?</h1>
<p>SMTP (Ask for information, provide answers):</p>
<pre class="literal-block">
C: MAIL FROM:<Smith@bar.com>
S: 250 OK
C: RCPT TO:<Jones@foo.com>
S: 250 OK
C: RCPT TO:<Green@foo.com>
S: 550 No such user here
C: DATA
S: 354 Start mail input; end with <CRLF>.<CRLF>
C: Blah blah blah...
C: ...etc. etc. etc.
C: .
S: 250 OK
</pre>
</div>
<div class="slide" id="id4">
<h1>What does SMTP look like?</h1>
<p>SMTP (Say goodbye):</p>
<pre class="literal-block">
C: QUIT
S: 221 foo.com Service closing transmission channel
</pre>
</div>
<div class="slide" id="what-does-pop3-look-like">
<h1>What does POP3 look like?</h1>
<p>POP3 (Identify yourself and find a partner):</p>
<pre class="literal-block">
C: <client connects to service port 110>
S: +OK POP3 server ready <1896.6971@mailgate.dobbs.org>
C: USER bob
S: +OK bob
C: PASS redqueen
S: +OK bob's maildrop has 2 messages (320 octets)
</pre>
</div>
<div class="slide" id="id5">
<h1>What does POP3 look like?</h1>
<p>POP3 (Ask for information, provide answers):</p>
<pre class="literal-block">
C: STAT
S: +OK 2 320
C: LIST
S: +OK 2 messages (320 octets)
S: 1 120
S: 2 200
S: .
</pre>
</div>
<div class="slide" id="id6">
<h1>What does POP3 look like?</h1>
<p>POP3 (Ask for information, provide answers):</p>
<pre class="literal-block">
C: RETR 1
S: +OK 120 octets
S: <the POP3 server sends the text of message 1>
S: .
C: DELE 1
S: +OK message 1 deleted
C: RETR 2
S: +OK 200 octets
S: <the POP3 server sends the text of message 2>
S: .
C: DELE 2
S: +OK message 2 deleted
</pre>
</div>
<div class="slide" id="id7">
<h1>What does POP3 look like?</h1>
<p>POP3 (Say goodbye):</p>
<pre class="literal-block">
C: QUIT
S: +OK dewey POP3 server signing off (maildrop empty)
C: <client hangs up>
</pre>
</div>
<div class="slide" id="what-does-imap-look-like">
<h1>What does IMAP look like?</h1>
<p>IMAP (Identify yourself and find a partner):</p>
<pre class="literal-block">
C: <client connects to service port 143>
S: * OK example.com IMAP4rev1 v12.264 server ready
C: A0001 USER "frobozz" "xyzzy"
S: * OK User frobozz authenticated
</pre>
</div>
<div class="slide" id="id8">
<h1>What does IMAP look like?</h1>
<p>IMAP (Ask for information, provide answers [connect to an inbox]):</p>
<pre class="literal-block">
C: A0002 SELECT INBOX
S: * 1 EXISTS
S: * 1 RECENT
S: * FLAGS (\Answered \Flagged \Deleted \Draft \Seen)
S: * OK [UNSEEN 1] first unseen message in /var/spool/mail/esr
S: A0002 OK [READ-WRITE] SELECT completed
</pre>
</div>
<div class="slide" id="id9">
<h1>What does IMAP look like?</h1>
<p>IMAP (Ask for information, provide answers [Get message sizes]):</p>
<pre class="literal-block">
C: A0003 FETCH 1 RFC822.SIZE
S: * 1 FETCH (RFC822.SIZE 2545)
S: A0003 OK FETCH completed
</pre>
</div>
<div class="slide" id="id10">
<h1>What does IMAP look like?</h1>
<p>IMAP (Ask for information, provide answers [Get first message header]):</p>
<pre class="literal-block">
C: A0004 FETCH 1 BODY[HEADER]
S: * 1 FETCH (RFC822.HEADER {1425}
<server sends 1425 octets of message payload>
S: )
S: A0004 OK FETCH completed
</pre>
</div>
<div class="slide" id="id11">
<h1>What does IMAP look like?</h1>
<p>IMAP (Ask for information, provide answers [Get first message body]):</p>
<pre class="literal-block">
C: A0005 FETCH 1 BODY[TEXT]
S: * 1 FETCH (BODY[TEXT] {1120}
<server sends 1120 octets of message payload>
S: )
S: * 1 FETCH (FLAGS (\Recent \Seen))
S: A0005 OK FETCH completed
</pre>
</div>
<div class="slide" id="id12">
<h1>What does IMAP look like?</h1>
<p>IMAP (Say goodbye):</p>
<pre class="literal-block">
C: A0006 LOGOUT
S: * BYE example.com IMAP4rev1 server terminating connection
S: A0006 OK LOGOUT completed
C: <client hangs up>
</pre>
</div>
<div class="slide" id="notice-any-difference">
<h1>Notice Any Difference?</h1>
<p>POP3 Commands:</p>
<ul class="incremental simple">
<li>STAT</li>
<li>LIST</li>
<li>RETR 1</li>
<li>DELE 1</li>
<li>QUIT</li>
</ul>
</div>
<div class="slide" id="id13">
<h1>Notice Any Difference?</h1>
<p>IMAP Commands:</p>
<ul class="incremental simple">
<li>A0001 USER "frobozz" "xyzzy"</li>
<li>A0002 SELECT INBOX</li>
<li>A0003 FETCH 1 RFC822.SIZE</li>
<li>A0004 FETCH 1 BODY[HEADER]</li>
<li>A0005 FETCH 1 BODY[TEXT]</li>
<li>A0006 LOGOUT</li>
</ul>
</div>
<div class="slide" id="id14">
<h1>Notice Any Difference?</h1>
<p>Sequence Identifiers allow the client to send commands without waiting for
responses.</p>
</div>
<div class="slide" id="re-ordered-imap-interaction">
<h1>Re-ordered IMAP Interaction</h1>
<pre class="literal-block">
C: A0001 USER "frobozz" "xyzzy"
S: * OK User frobozz authenticated
C: A0002 SELECT INBOX
S: ...
S: A0002 OK [READ-WRITE] SELECT completed
C: A0003 FETCH 1 RFC822.SIZE
C: A0004 FETCH 1 BODY[HEADER]
C: A0005 FETCH 1 BODY[TEXT]
S: * 1 FETCH (RFC822.SIZE 2545)
S: A0003 OK FETCH completed
...
...
C: A0006 LOGOUT
...
</pre>
</div>
<div class="slide" id="which-protocol-do-you-choose">
<h1>Which Protocol do you Choose?</h1>
<p>Stacking commands is more efficient, but would it work for POP3?</p>
<p class="incremental">Why not?</p>
</div>
<div class="slide" id="what-does-http-look-like">
<h1>What does HTTP look like?</h1>
<p>HTTP (Ask for information):</p>
<pre class="literal-block">
GET /index.html HTTP/1.1
Host: www.example.com
\r\n
</pre>
</div>
<div class="slide" id="id15">
<h1>What does HTTP look like?</h1>
<p>HTTP (Provide answers):</p>
<pre class="literal-block">
HTTP/1.1 200 OK
Date: Mon, 23 May 2005 22:38:34 GMT
Server: Apache/1.3.3.7 (Unix) (Red-Hat/Linux)
Last-Modified: Wed, 08 Jan 2003 23:11:55 GMT
Etag: "3f80f-1b6-3e1cb03b"
Accept-Ranges: none
Content-Length: 438
Connection: close
Content-Type: text/html; charset=UTF-8
\r\n
<438 bytes of content>
</pre>
</div>
<div class="slide" id="protocols-in-python">
<h1>Protocols in Python</h1>
<p class="big-centered">Let's try this out for ourselves!</p>
</div>
<div class="slide" id="id16">
<h1>Protocols in Python</h1>
<p class="big-centered">Fire up a Python interpreter</p>
</div>
<div class="slide" id="smtp-in-python">
<h1>SMTP in Python</h1>
<p>Start by importing smtplib (part of the standard library):</p>
<pre class="literal-block">
>>> import smtplib
>>> dir(smtplib)
['CRLF', 'LMTP', 'LMTP_PORT', 'OLDSTYLE_AUTH',
'SMTP', 'SMTPAuthenticationError', 'SMTPConnectError',
'SMTPDataError', 'SMTPException', 'SMTPHeloError',
'SMTPRecipientsRefused', 'SMTPResponseException',
'SMTPSenderRefused', 'SMTPServerDisconnected',
'SMTP_PORT', 'SMTP_SSL', 'SMTP_SSL_PORT', 'SSLFakeFile',
'__all__', '__builtins__', '__doc__', '__file__',
'__name__', '__package__', '_have_ssl', 'base64', 'email',
'encode_base64', 'hmac', 'quoteaddr', 'quotedata', 're',
'socket', 'ssl', 'stderr']
</pre>
</div>
<div class="slide" id="id17">
<h1>SMTP in Python</h1>
<p>Let's make a connection to a server. We'll use one I've set up in advance to
avoid needing to create one of our own:</p>
<pre class="literal-block">
>>> server = smtplib.SMTP('smtp.webfaction.com', 587)
>>> server.set_debuglevel(True) # to see interaction
>>> server.ehlo()
send: 'ehlo heffalump.local\r\n'
reply: '250-smtp.webfaction.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 20971520\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-STARTTLS\r\n'
...
</pre>
</div>
<div class="slide" id="id18">
<h1>SMTP in Python</h1>
<p>Does our server support TLS (secure transmissions?):</p>
<pre class="literal-block">
>>> server.has_extn('STARTTLS')
True
</pre>
<p>What other extensions are available?:</p>
<pre class="literal-block">
>>> server.esmpt_features.keys()
['enhancedstatuscodes', 'etrn', 'starttls',
'auth', 'dsn', '8bitmime', 'pipelining',
'size', 'vrfy']
</pre>
</div>
<div class="slide" id="id19">
<h1>SMTP in Python</h1>
<p>Some SMTP servers require authentication. This is one such server. Before
passing our username and password, though, we should turn on TLS for the sake
of security:</p>
<pre class="literal-block">
>>> server.starttls()
>>> server.ehlo() # re-identify after TLS begins
>>> server.login(username, password)
</pre>
</div>
<div class="slide" id="id20">
<h1>SMTP in Python</h1>
<p>Let's prepare a message to be sent to our server:</p>
<pre class="literal-block">
>>> from_addr = "YOUR NAME <fill in this address>"
>>> to_addrs = "demo@crisewing.com"
>>> subject = "this is a test"
>>> message = "a message from python smtplib"
</pre>
</div>
<div class="slide" id="id21">
<h1>SMTP in Python</h1>
<p>Email sent via SMTP requires certain formatting. It's part of the Protocol. In
particular, note that the headers are separated by CRLF sequences. This is
very common across internet protocols:</p>
<pre class="literal-block">
>>> template = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
>>> headers = template % (from_addr, to_addrs, subject)
</pre>
</div>
<div class="slide" id="id22">
<h1>SMTP in Python</h1>
<p>A message is the headers, plus the body of the message:</p>
<pre class="literal-block">
>>> email_body = headers + message
</pre>
<p>Sending the email is accomplished by calling the <tt class="docutils literal">sendmail</tt> method on our
server object, after which we should close the connection:</p>
<pre class="literal-block">
>>> server.sendmail(from_addr, [to_addrs, ], email_body)
>>> server.close()
</pre>
</div>
<div class="slide" id="putting-it-all-together">
<h1>Putting it all Together</h1>
<pre class="literal-block">
>>> from_addr = "YOUR NAME <fill in this address>"
>>> to_addrs = "demo@crisewing.com"
>>> subject = "this is a test"
>>> message = "a message from python smtplib"
>>> template = "From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
>>> headers = template % (from_addr, to_addrs, subject)
</pre>
</div>
<div class="slide" id="id23">
<h1>Putting it all Together</h1>
<pre class="literal-block">
>>> server = smtplib.SMTP('smtp.webfaction.com', 587)
>>> server.set_debuglevel(True)
>>> server.ehlo()
>>> server.starttls()
>>> server.ehlo() # re-identify after TLS begins
>>> server.login(username, password)
>>> email_body = headers + message
>>> server.sendmail(from_addr, [to_addrs, ], email_body)
>>> server.close()
</pre>
</div>
<div class="slide" id="python-means-batteries-included">
<h1>Python Means Batteries Included</h1>
<p>So in fact we have a module in the standard library for email support:</p>
<pre class="literal-block">
>>> import email.utils
>>> from email.mime.text import MIMEText
>>> from_addr = "addr@host.com"
>>> to_addrs = "other@another.com"
>>> msg = MIMEText("This is an email message")
>>> msg['From'] = email.utils.formataddr(("Name", from_addr))
>>> msg['To'] = email.utils.formataddr(("Name", to_addrs))
>>> msg['Subject'] = "Simple Test"
>>> server.sendmail(from_addr, [to_addrs, ], msg.as_string())
</pre>
</div>
<div class="slide" id="imap-in-python">
<h1>IMAP in Python</h1>
<p class="big-centered">Let's read that email we just sent</p>
</div>
<div class="slide" id="id24">
<h1>IMAP in Python</h1>
<p>Again, begin by importing the module from the Python Standard Library:</p>
<pre class="literal-block">
>>> import imaplib
>>> dir(imaplib)
['AllowedVersions', 'CRLF', 'Commands',
'Continuation', 'Debug', 'Flags', 'IMAP4',
'IMAP4_PORT', 'IMAP4_SSL', 'IMAP4_SSL_PORT',
'IMAP4_stream', 'Int2AP', 'InternalDate',
'Internaldate2tuple', 'Literal', 'MapCRLF',
'Mon2num', 'ParseFlags', 'Response_code',
'Time2Internaldate', 'Untagged_response',
'Untagged_status', '_Authenticator', ...]
</pre>
</div>
<div class="slide" id="id25">
<h1>IMAP in Python</h1>
<p>We set up a client object. WebFaction requires SSL for connecting to IMAP
servers, so let's initialize an IMAP4_SSL client and authenticate:</p>
<pre class="literal-block">
>>> conn = imaplib.IMAP4_SSL('mail.webfaction.com')
57:04.83 imaplib version 2.58
57:04.83 new IMAP4 connection, tag=FNHG
>>> conn.login(username, password)
('OK', ['Logged in.'])
</pre>
</div>
<div class="slide" id="id26">
<h1>IMAP in Python</h1>
<p>Let's set up debugging here too, so that we can see the communication back and
forth between client and server:</p>
<pre class="literal-block">
>>> conn.debug = 4 # >3 prints all messages
</pre>
<p>We can start by listing the mailboxes we have on the server:</p>
<pre class="literal-block">
>>> conn.list()
00:41.91 > FNHG3 LIST "" *
00:41.99 < * LIST (\HasNoChildren) "." "INBOX"
00:41.99 < FNHG3 OK List completed.
('OK', ['(\\HasNoChildren) "." "INBOX"'])
</pre>
</div>
<div class="slide" id="id27">
<h1>IMAP in Python</h1>
<p>We can find out about the mail on our server. We do this by querying for
<cite>status</cite>. IMAP provides a few different status values, let's ask for them
all:</p>
<pre class="literal-block">
>>> vals = '(MESSAGES RECENT UIDNEXT'
>>> vals += ' UIDVALIDITY UNSEEN)'
>>> conn.status('INBOX', vals)
12:03.91 > FNHG4 STATUS INBOX (MESSAGES RECENT UIDNEXT UIDVALIDITY UNSEEN)
12:04.01 < * STATUS "INBOX" (MESSAGES 2 RECENT 0 UIDNEXT 3 UIDVALIDITY 1357449499 UNSEEN 1)
12:04.01 < FNHG4 OK Status completed.
('OK', ['"INBOX" (MESSAGES 2 RECENT 0
UIDNEXT 3 UIDVALIDITY 1357449499
UNSEEN 1)'])
</pre>
</div>
<div class="slide" id="id28">
<h1>IMAP in Python</h1>
<p>To interact with our email, we must select a mailbox from the list we received
earlier:</p>
<pre class="literal-block">
>>> conn.select('INBOX')
00:00.47 > FNHG2 SELECT INBOX
00:00.56 < * FLAGS (\Answered \Flagged \Deleted \Seen \Draft)
00:00.56 < * OK [PERMANENTFLAGS (\Answered \Flagged \Deleted \Seen \Draft \*)] Flags permitted.
00:00.56 < * 2 EXISTS
00:00.57 < * 0 RECENT
00:00.57 < * OK [UNSEEN 2] First unseen.
00:00.57 < * OK [UIDVALIDITY 1357449499] UIDs valid
00:00.57 < * OK [UIDNEXT 3] Predicted next UID
00:00.57 < FNHG2 OK [READ-WRITE] Select completed.
('OK', ['2'])
</pre>
</div>
<div class="slide" id="id29">
<h1>IMAP in Python</h1>
<p>We can search our selected mailbox for messages matching one or more criteria.
The return value is a string list of the UIDs of messages that match our
search:</p>
<pre class="literal-block">
>>> conn.search(None, '(FROM "IPIP")')
18:25.41 > FNHG5 SEARCH (FROM "IPIP")
18:25.54 < * SEARCH 1 2
18:25.54 < FNHG5 OK Search completed.
('OK', ['1 2'])
>>>
</pre>
</div>
<div class="slide" id="id30">
<h1>IMAP in Python</h1>
<p>Once we've found a message we want to look at, we can use the <tt class="docutils literal">fetch</tt>
command to read it from the server. IMAP allows fetching each part of
a message independently:</p>
<pre class="literal-block">
>>> conn.fetch('2', '(BODY[HEADER])')
...
>>> conn.fetch('2', '(BODY[TEXT])')
...
>>> conn.fetch('2', '(FLAGS)')
</pre>
</div>
<div class="slide" id="id31">
<h1>IMAP in Python</h1>
<p>It is even possible to download an entire message in raw format, and load that
into a python email message object:</p>
<pre class="literal-block">
>>> import email
>>> typ, data = conn.fetch('2', '(RFC822)')
28:08.40 > FNHG8 FETCH 2 (RFC822)
...
>>> for part in data:
... if isinstance(part, tuple):
... msg = email.message_from_string(part[1])
...
>>>
</pre>
</div>
<div class="slide" id="id32">
<h1>IMAP in Python</h1>
<p>Once we have that, we can play with the resulting email object:</p>
<pre class="literal-block">
>>> msg['to']
'demo@crisewing.com'
>>> print msg.get_payload()
This is an email message
</pre>
</div>
<div class="slide" id="id33">
<h1>IMAP in Python</h1>
<p class="big-centered">Neat, huh?</p>
</div>
<div class="slide" id="what-have-we-learned">
<h1>What Have We Learned?</h1>
<ul class="incremental">
<li><p class="first">Protocols are just a set of rules for how to communicate</p>
</li>
<li><p class="first">A given protocol has a set of commands it knows</p>
</li>
<li><p class="first">If we properly format requests to a server, we can get answers</p>
</li>
<li><p class="first">Python supports a number of these protocols</p>
<blockquote>
<ul class="simple">
<li>So we don't have to remember how to format the commands ourselves</li>
</ul>
<ul class="incremental simple">
<li>But in every case we've seen so far, we could do the same thing with a
socket and some strings</li>
</ul>
</blockquote>
</li>
</ul>
</div>
<div class="slide" id="http-in-python">
<h1>HTTP in Python</h1>
<p class="big-centered">HTTP is no different</p>
</div>
</div>
</body>
</html>