-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpython模块介绍- SocketServer 网络服务框架 - 磁针石的个人空间 - 开源中国社区.html
1569 lines (1516 loc) · 97.3 KB
/
python模块介绍- SocketServer 网络服务框架 - 磁针石的个人空间 - 开源中国社区.html
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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='zh-CN' xml:lang='zh-CN' xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta http-equiv="Content-Language" content="zh-CN"/>
<meta name="robots" content="index, follow" />
<link rel="shortcut icon" type="image/x-icon" href="/img/favicon.ico?t=1408501845000" />
<title>python模块介绍- SocketServer 网络服务框架 - 磁针石的个人空间 - 开源中国社区</title>
<meta name="Keywords" content="python,模块,SocketServer,网络服务,框架"/>
<meta name="Description" content="python模块介绍- SocketServer 网络服务框架:SocketServer简化了网络服务器的编写。它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatag..."/>
<link rel="stylesheet/less" href="http://my.oschina.net/u/1433482/styles.less?ver=20140612" type="text/css" media="screen" />
<link rel="stylesheet" href="/js/2012/poshytip/tip-yellowsimple/tip-yellowsimple.css?t=1377425775000" type="text/css" />
<link rel="stylesheet" type="text/css" href="/js/2011/fancybox/jquery.fancybox-1.3.4.css?t=1377425774000" media="screen" />
<script type="text/javascript" src="/js/2012/jquery-1.7.1.min.js?t=1377425776000"></script>
<script type="text/javascript" src="/js/2012/jquery.form.js?t=1377425776000"></script>
<script type="text/javascript" src="/js/2011/fancybox/jquery.fancybox-1.3.4.pack.js?t=1395032412000?2014031702"></script>
<script type="text/javascript" src="/js/2012/poshytip/jquery.poshytip.min.js?t=1377425776000"></script>
<script type="text/javascript" src="/js/2011/oschina.js?t=1377425774000"></script>
<script type="text/javascript" src="/js/2012/less-1.3.0.min.js?t=1377425776000"></script>
<script type="text/javascript" src="/js/scrolltopcontrol.js?t=1377425784000"></script>
<script type='text/javascript' src='/js/jquery/jquery.atwho.js?t=1385352276000'></script>
<script type="text/javascript" src="/js/lib/angular/angular.min.js?t=1406837064000"></script>
<script type="text/javascript" src="/js/lib/angular/angular-sanitize.min.js?t=1406837064000"></script>
<script type="text/javascript" src="/js/lib/angular/angular-resource.min.js?t=1406837064000"></script>
<link rel="stylesheet" type="text/css" href="/js/jquery/jquery.atwho.css?t=1377425741000" />
<link rel="alternate" type="application/rss+xml" title="最新博客" href="http://my.oschina.net/u/1433482/rss" />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://my.oschina.net/action/xmlrpc/rsd?space=1433482" />
<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://my.oschina.net/action/xmlrpc/wlwmanifest?space=1433482" />
<style type="text/css">
body,table,input,textarea,select {font-family:Verdana,sans-serif,宋体;}
</style>
<script type="text/javascript">
scrolltotop.offset(100,165);
scrolltotop.init();
</script>
</head>
<body>
<div id="OSC_Screen">
<style type="text/css">
#OSC_Banner {margin: 0 auto;text-align: left;margin-bottom: 5px;height:30px;border-top:0px;}
#OSC_Banner a {text-decoration: none;color: #fff;}
#OSC_Banner .cs_content a {color: #080;}
#OSC_Banner a:hover {color: #eee;}
#OSC_Banner a.project{background-color:transparent;}
#OSC_Banner #OSC_Channel {float: left;height:30px;}
#OSC_Banner #OSC_Channel ul {line-height: 18px;}
#OSC_Banner #OSC_Channel ul li.item {float: left;margin: 0 16px 0 0;line-height: 30px;}
#OSC_Banner #OSC_Channel ul li a.hl {font-weight: bold;}
#OSC_Banner #OSC_Userbar {float: right; color: #ddd;line-height:30px;margin-right:10px;}
#OSC_Banner #OSC_Userbar em {font-style: normal;color: #fff;margin-right: 2px;}
/*下拉菜单*/
#OSC_Banner .control_select {position: relative;background: url('/img/down3.gif') no-repeat right 14px;padding-right: 8px;cursor: pointer;height: 25px;display: inline-block;z-index: 1001;}
#OSC_Banner .control_select a {position: relative;}
#OSC_Banner .control_select ul.cs_content {display: none;text-align: left;position: absolute;left: 0px;top: 25px;width: 110px;z-index: 9999;background: white;padding: 5px 0 2px 5px;font-size: 9pt;list-style-type: none;}
#OSC_Banner .control_select ul.cs_content li {border: 0;height: 16px;line-height: 16px;margin-bottom: 8px;}
#OSC_Banner .control_select ul.cs_content li a:hover {font-weight: bold;background: #fff;color: #A00}
#OSC_Banner .group_msg_count {display: inline-block; padding: 2px;border-radius: 3px;background-color: RGB(255, 113, 112);color: white;font-size: 12px;height: 10px;text-align: center;
line-height: 10px;min-width: 15px;box-shadow: 1px 1px 1px RGBA(0, 0, 0, 0.2);-webkit-box-shadow: 1px 1px 1px RGBA(0, 0, 0, 0.2);-moz-box-shadow: 1px 1px 1px RGBA(0, 0, 0, 0.2);}
.tip-yellowsimple .tip-arrow-top{left:80%;}
</style>
<script type="text/javascript">
g_user = {
id:0,
name:'',
login:false};</script>
<script type="text/javascript" src="/js/utils.js?t=1377425784000"></script>
<script type="text/javascript" src="/js/channel.js?t=1393367280000?2014022501"></script>
<div id='OSC_Banner'>
<div id="OSC_Channel" style="float:left;margin-left:10px;">
<ul>
<li class="item"><a href="http://www.oschina.net/" class='home'>首页</a></li> <li class="item control_select"><a href="http://www.oschina.net/project" class='project'>开源项目</a>
<ul class="cs_content">
<li><a href="http://www.oschina.net/project/lang/19/java">Java 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/194/csharp">C# 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/22/php">PHP 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/21/c">C/C++ 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/26/ruby">Ruby 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/25/python">Python 开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/358/go">Go开源软件</a></li>
<li><a href="http://www.oschina.net/project/lang/28/javascript">JS开源软件</a></li>
</ul>
</li>
<li class="item control_select">
<a href="http://www.oschina.net/question" class='question'>问答</a>
<ul class="cs_content">
<li><a href="http://www.oschina.net/question?catalog=1"> 技术问答 » </a></li>
<li><a href="http://www.oschina.net/question?catalog=2"> 技术分享 » </a></li>
<li><a href="http://www.oschina.net/question?catalog=3"> IT大杂烩 » </a></li>
<li><a href="http://www.oschina.net/question?catalog=100"> 职业生涯 » </a></li>
<li><a href="http://www.oschina.net/question?catalog=4"> 站务/建议 » </a></li>
<li><a href="http://www.oschina.net/alipay"> 支付宝专区 » </a></li>
<li><a href="http://www.oschina.net/hardware"> 开源硬件专区 » </a></li>
</ul>
</li>
<li class="item"><a href="http://www.oschina.net/code/list" class='code'>代码</a></li>
<li class="item"><a href="http://www.oschina.net/blog" class='blog'>博客</a></li>
<li class="item"><a href="http://www.oschina.net/translate" class='tran'>翻译</a></li>
<li class="item"><a href="http://www.oschina.net/news" class='news'>资讯</a></li>
<li class="item control_select">
<a href="http://www.oschina.net/android" class='mobile'>移动开发</a>
<ul class="cs_content cs_mobile">
<li class="android_"><a href="http://www.oschina.net/android">Android开发专区</a></li>
<li class="ios_"><a href="http://www.oschina.net/ios/home">iOS开发专区</a></li>
<li class="ios_"><a href="http://www.oschina.net/ios/codingList">iOS代码库</a></li>
<li class="wp7_"><a href="http://www.oschina.net/wp">Windows Phone</a></li>
</ul>
</li>
<li class="item t_job"><a href="http://www.oschina.net/job" class='job'>招聘</a></li>
<li class="item control_select">
<a href="http://city.oschina.net/" id="MyCities" title="城市圈">城市圈</a>
</li>
</ul>
</div>
<div id="OSC_Userbar">
当前访客身份:游客 [ <a href="http://www.oschina.net/home/login?goto_page=http%3A%2F%2Fmy.oschina.net%2Fu%2F1433482%2Fblog%2F190612">登录</a> | <a href="http://www.oschina.net/home/reg">加入开源中国</a> ]
</div>
<div class='clear'></div>
</div> <div id="OSC_Topbar">
<div id="VisitorInfo">
当前访客身份:
游客 [ <a href="http://www.oschina.net/home/login?goto_page=http%3A%2F%2Fmy.oschina.net%2Fu%2F1433482">登录</a> | <a href="http://www.oschina.net/home/reg">加入开源中国</a> ]
</div>
<div id="SearchBar">
<form action="http://www.oschina.net/search">
<input type='hidden' name='user' value='1433482'/>
<span class="ipt f_l">
<input type='text' id='txt_q' name='q' class='SERACH' value='在 34907 款开源软件中搜索' onblur="(this.value=='')?this.value='在 34907 款开源软件中搜索':this.value" onfocus="if(this.value=='在 34907 款开源软件中搜索'){this.value='';};this.select();"/>
</span>
<div class="search-by selectbox">
<span class="hide">
<select name='scope'>
<option value='project' selected>软件</option>
<option value='code'>代码</option>
<option value='bbs'>讨论区</option>
<option value='news'>新闻</option>
<option value='blog'>博客</option>
</select>
</span>
<div class="search_on" id="search-item"><span class="text">软件</span></div>
<ul class="search_list">
<li class="search-item"><a href="#1">软件</a></li>
<li><a href="#2">代码</a></li>
<li><a href="#3">讨论区</a></li>
<li><a href="#4">新闻</a></li>
<li><a href="#5">博客</a></li>
</ul>
</div>
<input type='submit' value='搜索' class='bnt f_r'/>
</form>
</div>
<div class='clear'></div>
</div>
<div id="OSC_Content">
<style>
.icon{width:20px;height:20px;vertical-align: middle;display:inline-block;font-size:14px;text-align:center;color:#FFF;font-weight: bold;}
.catalogs{padding:0px;margin:0px;position:relative;display:inline-block;}
.catalogs ul{padding:0px;margin:0px;position:absolute;left:-8px;top: -21px;clear:left;width:120px;height:14px;overflow:hidden;background:#FFF;z-index:1000;padding:5px;border:1px solid #FFF;}
.catalogs ul:hover{height:auto;background:#fff;border:1px solid #4466bb;}
.catalogs ul li a:hover{color:#A00;font-weight: bold;}
.BlogStat a:hover{background:#FFF;}
.BlogStat{font-size:12px;line-height:20px;}
.BlogStat .g{color:#40AA53}
.BlogTags{margin-top:8px;}
.BlogAbstracts{}
</style>
<div id='SpaceLeft'>
<div class='Owner'>
<a href="http://my.oschina.net/u/1433482" class='Img'><img src="http://static.oschina.net/uploads/user/716/1433482_100.jpg?t=1389046338000" align="absmiddle" alt="磁针石" title="磁针石" class="LargePortrait"/></a>
<span class='U'>
<a href="http://my.oschina.net/u/1433482" class='Name' title='男'>磁针石</a>
<span class='opts'>
<img src="/img/2012/men.png" align='absmiddle' title='男'/>
<script type='text/javascript'>
var nologin = "<a href='javascript:_follow_nologin()' title='成为TA的粉丝'>关注此人</a>"
var follow0 = "<a href='javascript:_follow_user(1433482,true)' title='成为TA的粉丝'>关注此人</a>";
var follow1 = "已关注 | <a href='javascript:_follow_user(1433482,false)' title='取消关注后,此人的更新信息将不会出现在你的首页'>取消</a>";
var follow2 = "互相关注 | <a href='javascript:_follow_user(1433482,false)' title='取消关注后,此人的更新信息将不会出现在你的首页'>取消</a>";
var follow4 = "<a href='javascript:_follow_user(1433482,true)' title='成为TA的粉丝'>+关注此人</a>";
document.write("<span id='_follow_1433482_action_'>");
document.write(nologin);
document.write("</span>");
function _follow_nologin(){
if(confirm('必须登录后才能关注,是否现在登录?')){
location.href="http://www.oschina.net/home/login?goto_page="+location.href;
}
}
function _follow_user(uid, follow){
var action = follow?"/action/user/follow":"/action/user/unfollow";
ajax_post(action,"id="+uid+"&user=${g_user.id}",function(result){
var act = $('#_follow_1433482_action_');
if(result == '0')
_follow_nologin();
else if(result == '1')
act.html(follow0);
else if(result == '2')
act.html(follow1);
else if(result == '3')
act.html(follow2);
else if(result == '4')
act.html(follow4);
});
}
</script> </span>
</span>
<div class='clear'></div>
<div class='stat'>
<a href="http://my.oschina.net/u/1433482/fellow">关注<span>(5)</span></a>
<a href="http://my.oschina.net/u/1433482/fans">粉丝<span>(19)</span></a>
<a href="http://www.oschina.net/question/3307_20931" title="查看OSCHINA积分规则">积分<span>(69)</span></a>
</div>
</div> <style type="text/css">
#Medals {margin-left: 3px; height: 28px;}
#Medals li {float: left;margin: 3px 5px;line-height: 24px;}
.tip-yellowsimple-medal {
z-index: 1000;
text-align: left;
border: 1px solid #E6DEB6;
border-radius: 4px;
-moz-border-radius: 4px;
-webkit-border-radius: 4px;
padding: 6px 8px;
min-width: 50px;
max-width: 300px;
color: #000;
background-color: #FFFEF2;
}
.tip-yellowsimple-medal .tip-arrow-left {
visibility: inherit;
margin-top: -4px;
margin-left: -6px;
top: 15px;
left: 0;
width: 6px;
height: 9px;
background: url(/js/2012/poshytip/tip-yellowsimple/tip-yellowsimple_arrows.gif?t=1377425775000) no-repeat -27px 0;
}
div.tip-yellowsimple-medal div.tip-arrow {
visibility: hidden;
position: absolute;
overflow: hidden;
font: 1px/1px sans-serif;
}
</style>
<script type="text/javascript">
$(function(){
$('#Medals a').poshytip({
className: 'tip-yellowsimple-medal',
alignTo: 'target',
alignX: 'right',
alignY: 'bottom',
offsetX: 10,
offsetY: -30,
fade: false,
slide: false,
content: function(updateCallback) {
ajax_get("/action/ajax/get_medal_detail?owner=1433482&medalId="+$(this).attr('medalId')+"&time="+new Date().getTime(),false,function(html){
updateCallback(html);
if($('.tip-arrow.tip-arrow-left').last().offset().top >= temp.offset().top) {
$('.tip-arrow.tip-arrow-left').last().css({top : "15px"});
}
else {
$('.tip-arrow.tip-arrow-left').last().css({top : "135px"});
}
});
temp = $(this);
return "<div style='height:100px;'>Loading...</div>";
}
});
});
</script>
<style>
#MyResume textarea {width:170px;height:60px;font-size:9pt;}
</style>
<div class='Resume' id='MyResume'>
这个人很懒,啥也没写</div>
<div class='Opts clearfix'>
<a href="javascript:sendmsg(1433482, 0)" class='a1 sendmsg'><i>.</i><span>发送留言</span></a>
<a href="http://www.oschina.net/question/ask?user=1433482" class='a2 ask'><i>.</i><span>请教问题</span></a>
</div><div class="Mod" id="BlogCatalogs">
<strong> 博客分类</strong>
<ul>
<li><a href="http://my.oschina.net/u/1433482/blog?catalog=430934">工作日志</a><span>(13)</span></li>
<li><a href="http://my.oschina.net/u/1433482/blog?catalog=430935">日常记录</a><span>(0)</span></li>
<li><a href="http://my.oschina.net/u/1433482/blog?catalog=430936">转贴的文章</a><span>(0)</span></li>
</ul>
</div><div class="Mod" id="HotBlogs">
<strong>阅读排行</strong>
<ol>
<li><a href="http://my.oschina.net/u/1433482/blog/190612">1. python模块介绍- SocketServer 网络服务框架</a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/191211">2. python模块介绍- select 等待I/0完成</a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/190913">3. python模块介绍-asynchat 异步socket命令/响应处理器</a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/192562">4. python模块介绍-gevent介绍:基于协程的网络库 </a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/192253">5. python模块介绍- time 时间访问和转换 </a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/193462">6. python模块介绍-httplib:HTTP协议客户端</a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/190696">7. python模块介绍-asyncore 异步socket处理器</a></li>
<li><a href="http://my.oschina.net/u/1433482/blog/199165">8. python模块介绍-ftplib:FTP协议客户端</a></li>
</ol>
</div>
<div class="Mod" id="BlogReplies">
<strong> 最新评论</strong>
<ul>
<li>
<a href="http://my.oschina.net/leeyd">@死磕的小蚂蚁</a>:mark
<a href="/action/tweet/go?obj=283619715&type=18&user=1395186">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/u/940775">@yang88</a>:666
<a href="/action/tweet/go?obj=281965017&type=18&user=940775">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/u/940775">@yang88</a>:#此处输入软件名#
<a href="/action/tweet/go?obj=281965014&type=18&user=940775">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/tcstory">@花瓣奶牛</a>:虽然是python2的文档但是对我还是很有用,
<a href="/action/tweet/go?obj=281461562&type=18&user=1039981">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/tcstory">@花瓣奶牛</a>:这个文档不错不错
<a href="/action/tweet/go?obj=281461476&type=18&user=1039981">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/kisscb">@ben.</a>:收藏!好文章
<a href="/action/tweet/go?obj=279910273&type=18&user=914949">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/linuxhitlover">@叶秀兰</a>:引用来自“磁针石”的评论 请去原文地址 http:/...
<a href="/action/tweet/go?obj=278451498&type=18&user=865233">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/u/1433482">@磁针石</a>:请去原文地址 http://automationtesting.sinaapp...
<a href="/action/tweet/go?obj=278450926&type=18&user=1433482">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/linuxhitlover">@叶秀兰</a>:代码好像有些太长看不到
<a href="/action/tweet/go?obj=278440087&type=18&user=865233">查看»</a>
</li>
<li>
<a href="http://my.oschina.net/u/1433482">@磁针石</a>:是的,在准备试gevent,greenlet,twisted等...
<a href="/action/tweet/go?obj=277867941&type=18&user=1433482">查看»</a>
</li>
</ul>
</div>
<div class='Mod' id='Stat'>
<strong>访客统计</strong>
<ul>
<li><label>今日访问:</label>4</li>
<li><label>昨日访问:</label>69</li>
<li><label>本周访问:</label>73</li>
<li><label>本月访问:</label>1395</li>
<li><label>所有访问:</label>52568</li>
</ul>
</div></div>
<div class='SpaceList'>
<div class='TopBar'>
<div class='NavPath'>
<a href='http://my.oschina.net/u/1433482/home'>空间</a> » <a href='http://my.oschina.net/u/1433482/blog'>博客</a>
»
<span class="catalogs">
<ul>
<li ><a href="http://my.oschina.net/u/1433482/blog?catalog=430934">工作日志</a></li>
<li ><a href="http://my.oschina.net/u/1433482/blog?catalog=430935">日常记录</a></li> <li ><a href="http://my.oschina.net/u/1433482/blog?catalog=430936">转贴的文章</a></li> <li><a href="http://my.oschina.net/u/1433482/blog">所有分类</a></li>
</ul>
</span>
</div>
</div>
<div class='BlogEntity'>
<div class='BlogTitle'>
<h1><span class="icon" style="background:#44ac57;" title="原创博客">原</span> <span class="icon" style="background:#fd9c47;" title="首页推荐过的博客">荐</span> python模块介绍- SocketServer 网络服务框架</h1>
<div class='BlogStat'>
发表于1年前(2014-01-06 07:04)
阅读(<span class="g">16673</span>) | 评论(<a href="#comments"><span class="g">3</span></a>)
<span class='admin'>
<style>
#TagsSwitcher{color:#A00;}
#RecTags,#MyTags{margin:5px 0;}
#RecTags a.tag{line-height:24px;}
#favor_form p:first-child{margin:5px 0;}
#favor_form{width:200px;}
#favor_form p {color:#666;}
#favor_form form{min-height: 85px;width:200px;}
#favor_form form ._favor_input{display:block;margin:2px 0;width:199px;}
#favor_form form ._favor_button{float:left;padding:2px 5px;}
.favor_ok {text-align:center;font-size:10.5pt;width:199px;height:60px;margin-top:10px;}
#TagsSwitcher{cursor:pointer;float:right;margin-top:10px;}
#MyTags{display:none;width:199px;}
#MyTags a.tag {float:left; background-color: #E0EAF1;border-bottom: 1px solid #3E6D8E;border-right: 1px solid #7F9FB6;color: #3E6D8E;font-size: 8pt;line-height: 16px;margin: 2px 2px 2px 0;padding: 2px 4px;text-decoration: none;white-space: nowrap;}
#MyTags a.tag:hover{background-color: #3E6D8E;color: #FFF;}
</style>
<script type='text/javascript'>
var favor_add = "<a href='javascript:add_to_favor(190612,3)' id='favor_trigger' title='添加到收藏夹'>我要收藏</a>";
var favor_del = "<a href='javascript:delete_favor(190612,3)' id='favor_trigger' style='color:#a00;'>取消收藏</a>";
var favor_ok = "<p class='favor_ok'>已成功添加到收藏夹<br/><br/> <a href='null/favorites?type=3'>我的收藏夹</a> | <a href='javascript:close_favor()'>关闭</a></p>";
function delete_favor(obi_id, obj_type){
$('#attention_it').html(favor_add);
$("#p_attention_count").load("/action/favorite/cancel?type="+obj_type+"&id="+obi_id, {user: ''});
}
function add_to_favor(obj_id, obj_type){
if(confirm('必须登录后才能收藏,是否现在登录?')){
location.href="http://www.oschina.net/home/login?goto_page="+encodeURIComponent(location.href);
}
}
function close_favor(elem_id){
$('#favor_trigger').poshytip('destroy');
}
function setTag(tv){
var t = $("._favor_input");
var value = t.val();
var tags = value.replace(/,/ig,',').split(',');
var exist = false;
value = $.map(tags,function(v,i){
if($.trim(tv) == $.trim(v)){
exist = true;
return undefined;
}
return v;
}).join(',');
if(exist){
t.val(value);
return;
}
if(value!="")
t.val(value+","+tv);
else
t.val(tv)
}
function ltrim(str){
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(0)) != -1){
var j=0, i = s.length;
while (j < i && whitespace.indexOf(s.charAt(j)) != -1){
j++;
}
s = s.substring(j, i);
}
return s;
}
function rtrim(str){
var whitespace = new String(" \t\n\r");
var s = new String(str);
if (whitespace.indexOf(s.charAt(s.length-1)) != -1){
var i = s.length - 1;
while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1){
i--;
}
s = s.substring(0, i+1);
}
return s;
}
function trim(str){
return rtrim(ltrim(str));
}
</script>
<em id='p_attention_count'>0</em>人收藏此文章,
<span id='attention_it'>
<script type='text/javascript'>document.write(favor_add);</script>
</span>
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url:"/action/favorite/attentionUsersCount",
type:"get",
data:"id="+190612+"&type="+3,
success:function(msg){
$("#p_attention_count").html(msg);
}
});
});
</script> </span>
</div>
<div class="tvote" onclick="vote(190612)" title="都有3个人赞过了,你也来赞一下吧!不会扣积分哦!"><em class="vote">赞</em><em class="num vote_count">3</em></div>
</div>
<p style='margin:0 0 10px 0;'><a style="color:#A00;font-weight:bold;" href="http://www.shilipai.net/jumpbro" target="_blank">如何快速提高你的薪资?-实力拍“跳槽吧兄弟”梦想活动即将开启</a></p> <a name="ueditor" ></a>
<div class="BlogAbstracts">
<em class="corner">摘要</em>
<span>SocketServer简化了网络服务器的编写。它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer。这4个类是同步进行处理的,另外通过ForkingMixIn和ThreadingMixIn类来支持异步。
创建服务器的步骤。首先,你必须创建一个请求处理类,它是BaseRequestHandler的子类并重载其handle()方法。其次,你必须实例化一个服务器类,传入服务器的地址和请求处理程序类。最后,调用handle_request()(一般是调用其他事件循环或者使用select())或serve_forever()...</span>
<div class='BlogTags'>
<a href="http://www.oschina.net/search?scope=blog&q=python" class="tag">python</a>
<a href="http://www.oschina.net/search?scope=blog&q=%E6%A8%A1%E5%9D%97" class="tag">模块</a>
<a href="http://www.oschina.net/search?scope=blog&q=SocketServer" class="tag">SocketServer</a>
<a href="http://www.oschina.net/search?scope=blog&q=%E7%BD%91%E7%BB%9C%E6%9C%8D%E5%8A%A1" class="tag">网络服务</a>
<a href="http://www.oschina.net/search?scope=blog&q=%E6%A1%86%E6%9E%B6" class="tag">框架</a>
</div>
</div><br />
<div class="BlogAnchor">
<p><em class="corner" id="AnchorContentToggle" title="点击收起目录" style="cursor:pointer;">目录[-]</em></p>
<div class="AnchorContent" id="AnchorContent"><li class='osc_h2'><a href='#OSC_h2_1'>服务器类型</a></li><li class='osc_h2'><a href='#OSC_h2_2'>服务器对象</a></li><li class='osc_h2'><a href='#OSC_h2_3'>请求处理器</a></li><li class='osc_h2'><a href='#OSC_h2_4'>Echo实例</a></li><li class='osc_h3'><a href='#OSC_h3_5'>TCPServer</a></li><li class='osc_h3'><a href='#OSC_h3_6'>UDPServer</a></li><li class='osc_h3'><a href='#OSC_h3_7'>异步</a></li><li class='osc_h2'><a href='#OSC_h2_8'>本文地址</a></li><li class='osc_h2'><a href='#OSC_h2_9'>参考资料</a></li></div>
</div>
<script>
$(function(){
$("#AnchorContentToggle").click(function(){
var text = $(this).html();
if(text=="目录[-]"){
$(this).html("目录[+]");
$(this).attr({"title":"展开"});
}else{
$(this).html("目录[-]");
$(this).attr({"title":"收起"});
}
$("#AnchorContent").toggle();
});
});
</script>
<div class='BlogContent'><p>SocketServer简化了网络服务器的编写。它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer。这4个类是同步进行处理的,另外通过ForkingMixIn和ThreadingMixIn类来支持异步。</p>
<p>创建服务器的步骤。首先,你必须创建一个请求处理类,它是BaseRequestHandler的子类并重载其handle()方法。其次,你必须实例化一个服务器类,传入服务器的地址和请求处理程序类。最后,调用handle_request()(一般是调用其他事件循环或者使用select())或serve_forever()。</p>
<p>集成ThreadingMixIn类时需要处理异常关闭。daemon_threads指示服务器是否要等待线程终止,要是线程互相独立,必须要设置为True,默认是False。</p>
<p>无论用什么网络协议,服务器类有相同的外部方法和属性。</p>
<p>该模块在python3中已经更名为socketserver。</p>
<span id="OSC_h2_1"></span>
<h2>服务器类型<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#服务器类型" rel="nofollow"></a></h2>
<p>5种类型:BaseServer,TCPServer,UnixStreamServer,UDPServer,UnixDatagramServer。 注意:BaseServer不直接对外服务。</p>
<span id="OSC_h2_2"></span>
<h2>服务器对象<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#服务器对象" rel="nofollow"></a></h2>
<ul>
<li><p>class SocketServer.BaseServer:这是模块中的所有服务器对象的超类。它定义了接口,如下所述,但是大多数的方法不实现,在子类中进行细化。</p></li>
</ul>
<ul>
<li><p>BaseServer.fileno():返回服务器监听套接字的整数文件描述符。通常用来传递给select.select(), 以允许一个进程监视多个服务器。</p></li>
</ul>
<ul>
<li><p>BaseServer.handle_request():处理单个请求。处理顺序:get_request(), verify_request(), process_request()。如果用户提供handle()方法抛出异常,将调用服务器的handle_error()方法。如果self.timeout内没有请求收到, 将调用handle_timeout()并返回handle_request()。</p></li>
</ul>
<ul>
<li><p>BaseServer.serve_forever(poll_interval=0.5): 处理请求,直到一个明确的shutdown()请求。每poll_interval秒轮询一次shutdown。忽略self.timeout。如果你需要做周期性的任务,建议放置在其他线程。</p></li>
</ul>
<ul>
<li><p>BaseServer.shutdown():告诉serve_forever()循环停止并等待其停止。python2.6版本。</p></li>
</ul>
<ul>
<li><p>BaseServer.address_family: 地址家族,比如socket.AF_INET和socket.AF_UNIX。</p></li>
</ul>
<ul>
<li><p>BaseServer.RequestHandlerClass:用户提供的请求处理类,这个类为每个请求创建实例。</p></li>
</ul>
<ul>
<li><p>BaseServer.server_address:服务器侦听的地址。格式根据协议家族地址的各不相同,请参阅socket模块的文档。</p></li>
</ul>
<ul>
<li><p>BaseServer.socketSocket:服务器上侦听传入的请求socket对象的服务器。</p></li>
</ul>
<p>服务器类支持下面的类变量:</p>
<ul>
<li><p>BaseServer.allow_reuse_address:服务器是否允许地址的重用。默认为false ,并且可在子类中更改。</p></li>
</ul>
<ul>
<li><p>BaseServer.request_queue_size</p></li>
</ul>
<p>请求队列的大小。如果单个请求需要很长的时间来处理,服务器忙时请求被放置到队列中,最多可以放request_queue_size个。一旦队列已满,来自客户端的请求将得到 “Connection denied”错误。默认值通常为5 ,但可以被子类覆盖。</p>
<ul>
<li><p>BaseServer.socket_type:服务器使用的套接字类型; socket.SOCK_STREAM和socket.SOCK_DGRAM等。</p></li>
</ul>
<ul>
<li><p>BaseServer.timeout:超时时间,以秒为单位,或 None表示没有超时。如果handle_request()在timeout内没有收到请求,将调用handle_timeout()。</p></li>
</ul>
<p>下面方法可以被子类重载,它们对服务器对象的外部用户没有影响。</p>
<ul>
<li><p>BaseServer.finish_request():实际处理RequestHandlerClass发起的请求并调用其handle()方法。 常用。</p></li>
</ul>
<ul>
<li><p>BaseServer.get_request():接受socket请求,并返回二元组包含要用于与客户端通信的新socket对象,以及客户端的地址。</p></li>
</ul>
<ul>
<li><p>BaseServer.handle_error(request, client_address):如果RequestHandlerClass的handle()方法抛出异常时调用。默认操作是打印traceback到标准输出,并继续处理其他请求。</p></li>
</ul>
<ul>
<li><p>BaseServer.handle_timeout():超时处理。默认对于forking服务器是收集退出的子进程状态,threading服务器则什么都不做。</p></li>
</ul>
<ul>
<li><p>BaseServer.process_request(request, client_address) :调用finish_request()创建RequestHandlerClass的实例。如果需要,此功能可以创建新的进程或线程来处理请求,ForkingMixIn和ThreadingMixIn类做到这点。常用。</p></li>
</ul>
<ul>
<li><p>BaseServer.server_activate():通过服务器的构造函数来激活服务器。默认的行为只是监听服务器套接字。可重载。</p></li>
</ul>
<ul>
<li><p>BaseServer.server_bind():通过服务器的构造函数中调用绑定socket到所需的地址。可重载。</p></li>
</ul>
<ul>
<li><p>BaseServer.verify_request(request, client_address):返回一个布尔值,如果该值为True ,则该请求将被处理,反之请求将被拒绝。此功能可以重写来实现对服务器的访问控制。默认的实现始终返回True。client_address可以限定客户端,比如只处理指定ip区间的请求。 常用。</p></li>
</ul>
<span id="OSC_h2_3"></span>
<h2>请求处理器<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#请求处理器" rel="nofollow"></a></h2>
<p>处理器接收数据并决定如何操作。它负责在socket层之上实现协议(i.e., HTTP, XML-RPC, or AMQP),读取数据,处理并写反应。可以重载的方法如下:</p>
<ul>
<li><p>setup(): 准备请求处理. 默认什么都不做,StreamRequestHandler中会创建文件类似的对象以读写socket.</p></li>
<li><p>handle(): 处理请求。解析传入的请求,处理数据,并发送响应。默认什么都不做。常用变量:self.request,self.client_address,self.server。</p></li>
<li><p>finish(): 环境清理。默认什么都不做,如果setup产生异常,不会执行finish。</p></li>
</ul>
<p>通常只需要重载handle。self.request的类型和数据报或流的服务不同。对于流服务,self.request是socket 对象;对于数据报服务,self.request是字符串和socket 。可以在子类StreamRequestHandler或DatagramRequestHandler中重载,重写setup()和finish() ,并提供self.rfile和self.wfile属性。 self.rfile和self.wfile可以读取或写入,以获得请求数据或将数据返回到客户端。</p>
<span id="OSC_h2_4"></span>
<h2>Echo实例<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#Echo实例" rel="nofollow"></a></h2>
<span id="OSC_h3_5"></span>
<h3>TCPServer<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#TCPServer" rel="nofollow"></a></h3>
<p>TCPServer.py</p>
<pre class="brush:python;toolbar:false;">import SocketServerclass MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data # just send back the same data, but upper-cased
self.request.sendall(self.data.upper())if __name__ == "__main__":
HOST, PORT = "localhost", 9999
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()</pre>
<p>另外一种方式是使用流,一次读一行。</p>
<pre class="brush:python;toolbar:false;">class MyTCPHandler(SocketServer.StreamRequestHandler):
def handle(self):
# self.rfile is a file-like object created by the handler;
# we can now use e.g. readline() instead of raw recv() calls
self.data = self.rfile.readline().strip()
print "{} wrote:".format(self.client_address[0])
print self.data # Likewise, self.wfile is a file-like object used to write back
# to the client
self.wfile.write(self.data.upper())</pre>
<p>客户端:</p>
<pre class="brush:python;toolbar:false;">import socketimport sysHOST, PORT = "localhost", 9999data = " ".join(sys.argv[1:])# Create a socket (SOCK_STREAM means a TCP socket)sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)try:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(data + "\n")
# Receive data from the server and shut down
received = sock.recv(1024)finally:
sock.close()print "Sent: {}".format(data)print "Received: {}".format(received)</pre>
<p>《The Python Standard Library by Example 2011》有更详细的echo实例,参见11.3.5部分。 执行结果:</p>
<pre class="brush:shell;toolbar:false;"># python TCPServer.py
127.0.0.1 wrote:
hello world with TCP
127.0.0.1 wrote:
python is nice# python TCPClient.py
Sent:
Received:
# python TCPClient.py hello world with TCPSent: hello world with TCP
Received: HELLO WORLD WITH TCP# python TCPClient.py python is niceSent: python is nice
Received: PYTHON IS NICE</pre>
<span id="OSC_h3_6"></span>
<h3>UDPServer<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#UDPServer" rel="nofollow"></a></h3>
<p>UDPServer.py</p>
<pre class="brush:python;toolbar:false;">import SocketServerclass MyUDPHandler(SocketServer.BaseRequestHandler):
"""
This class works similar to the TCP handler class, except that
self.request consists of a pair of data and client socket, and since
there is no connection the client address must be given explicitly
when sending data back via sendto().
"""
def handle(self):
data = self.request[0].strip()
socket = self.request[1]
print "{} wrote:".format(self.client_address[0])
print data
socket.sendto(data.upper(), self.client_address)if __name__ == "__main__":
HOST, PORT = "localhost", 9999
server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
server.serve_forever()</pre>
<p>UDPClient.py</p>
<pre class="brush:python;toolbar:false;">import socketimport sysHOST, PORT = "localhost", 9999data = " ".join(sys.argv[1:])# SOCK_DGRAM is the socket type to use for UDP socketssock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)# As you can see, there is no connect() call; UDP has no connections.# Instead, data is directly sent to the recipient via sendto().sock.sendto(data + "\n", (HOST, PORT))received = sock.recv(1024)print "Sent: {}".format(data)print "Received: {}".format(received)</pre>
<p>执行和UDP类似。</p>
<span id="OSC_h3_7"></span>
<h3>异步<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#异步" rel="nofollow"></a></h3>
<p>ThreadingMixIn的例子:</p>
<pre class="brush:python;toolbar:false;">import socketimport threadingimport SocketServerclass ThreadedTCPRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(1024)
cur_thread = threading.current_thread()
response = "{}: {}".format(cur_thread.name, data)
self.request.sendall(response)class ThreadedTCPServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
passdef client(ip, port, message):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((ip, port))
try:
sock.sendall(message)
response = sock.recv(1024)
print "Received: {}".format(response)
finally:
sock.close()if __name__ == "__main__":
# Port 0 means to select an arbitrary unused port
HOST, PORT = "localhost", 0
server = ThreadedTCPServer((HOST, PORT), ThreadedTCPRequestHandler)
ip, port = server.server_address # Start a thread with the server -- that thread will then start one
# more thread for each request
server_thread = threading.Thread(target=server.serve_forever)
# Exit the server thread when the main thread terminates
server_thread.daemon = True
server_thread.start()
print "Server loop running in thread:", server_thread.name
client(ip, port, "Hello World 1")
client(ip, port, "Hello World 2")
client(ip, port, "Hello World 3")
server.shutdown()</pre>
<p>执行结果:</p>
<pre class="brush:shell;toolbar:false;">$ python ThreadedTCPServer.py
Server loop running in thread: Thread-1
Received: Thread-2: Hello World 1
Received: Thread-3: Hello World 2
Received: Thread-4: Hello World 3</pre>
<p>ForkingMixIn的使用方法类似,只不过是用进程代替了线程。《The Python Standard Library by Example 2011》中有相关实例。</p>
<span id="OSC_h2_8"></span>
<h2>本文地址<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#本文地址" rel="nofollow"></a></h2>
<ul>
<li><p><a href="http://automationtesting.sinaapp.com/blog/m_socket_connection" rel="nofollow">http://automationtesting.sinaapp.com/blog/m_socket_connection</a></p></li>
<li><p>本站地址:python自动化测试<a href="http://automationtesting.sinaapp.com/" rel="nofollow">http://automationtesting.sinaapp.com</a> python开发自动化测试群113938272和开发测试群6089740 微博 <a href="http://weibo.com/cizhenshi" rel="nofollow"><span></span>http://weibo.com/cizhenshi</a></p></li>
</ul>
<span id="OSC_h2_9"></span>
<h2>参考资料<a href="http://automationtesting.sinaapp.com/blog/m_SocketServer#参考资料" rel="nofollow"></a></h2>
<ul>
<li><p><a href="http://docs.python.org/2/library/socketserver.html" rel="nofollow"><span></span>http://docs.python.org/2/library/socketserver.html</a> 主要参考</p></li>
<li><p>《The Python Standard Library by Example 2011》</p></li>
<li><p>socket (<a href="http://docs.python.org/library/socket.html" rel="nofollow"><span></span>http://docs.python.org/library/socket.html</a>) The standard library documentation for this module.</p></li>
</ul>
<p><br></p></div>
<div class='BlogShare'>
<strong>分享到:</strong>
<a class="share_sina" title="分享到新浪微博" href="javascript:void((function(s,d,e,r,l,p,t,z,c){var%20f='http://v.t.sina.com.cn/share/share.php?appkey=858381728',u=z||d.location,p=['&url=',e(u),'&title=',e(t||d.title),'&source=',e(r),'&sourceUrl=',e(l),'&content=',c||'gb2312','&pic=',e(p||'')].join('');function%20a(){if(!window.open([f,p].join(''),'mb',['toolbar=0,status=0,resizable=1,width=440,height=430,left=',(s.width-440)/2,',top=',(s.height-430)/2].join('')))u.href=[f,p].join('');};if(/Firefox/.test(navigator.userAgent))setTimeout(a,0);else%20a();})(screen,document,encodeURIComponent,'','','','python模块介绍- SocketServer 网络服务框架: SocketServer简化了网络服务器的编写。它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer。这4个类是同步进行处理的,另外通过ForkingMixIn和Thr...','','utf-8'));">
<img src="/img/sinaweibo_fb1.png?t=1384826240000" height="29">
</a>
<a class="share_qq" title="分享到腾讯微博" href="javascript:(function(){window.open('http://v.t.qq.com/share/share.php?url='+encodeURIComponent(document.location)+'&appkey=96f54f97c4de46e393c4835a266207f4&site=&title='+encodeURIComponent(document.title)+encodeURIComponent(': SocketServer简化了网络服务器的编写。它有4个类:TCPServer,UDPServer,UnixStreamServer,UnixDatagramServer。这4个类是同步进行处理的,另外通过ForkingMixIn和Thr...'),'', 'width=450, height=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, location=yes, resizable=no, status=no');}())">
<img src="/img/tencent_weibo_fb.jpg?t=1384826240000" height="30">
</a>
<span class='BlogVote' onclick="vote(190612)" title="都有3个人赞过了,你也来赞一下吧!不会扣积分哦!">
<span class="num vote_count" >3</span><span class="vote" >赞</span>
</span>
</div>
<div class='BlogCopyright'>
声明:OSCHINA 博客文章版权属于作者,受法律保护。未经作者同意不得转载。
</div>
<div class='BlogLinks'>
<ul>
<li class='next'><a href="http://my.oschina.net/u/1433482/blog/190696" title="下一篇:python模块介绍-asyncore 异步socket处理器">下一篇 »</a></li> </ul>
</div>
</div>
<div style='margin:10px 0;'>
<script type="text/javascript" src="/js/ad/blog.js?t=1421709195000"></script>
</div>
</div>
<!-------------------------------------------------------->
<script type="text/javascript">
angular.module('blogComment', ['ngSanitize'])
.controller('ListComment',
function($scope, $http,$filter){
var url = "/action/blog/listComment?blog=190612&page=1&size=10";
$http({method: 'GET', url: url}).success(function(data) {$scope.commentjsons = data;});
$scope.totalComment = 3;
$scope.currentPage=1;
$scope.pageSize=10;
$scope.totalPage=Math.ceil($scope.totalComment/$scope.pageSize);
if($scope.currentPage>3){
$scope.beginIndex = $scope.currentPage - 3;
}else{
$scope.beginIndex = 1;
}
$scope.endIndex = $scope.beginIndex + 9;
if($scope.endIndex>$scope.totalPage){
$scope.endIndex = $scope.totalPage;
}
var pages = new Array();
var index=0;
for(var i=$scope.beginIndex;i<=$scope.endIndex;i++){
pages[index]=i;
index++;
}
$scope.pages = pages;
$scope.delete_c = function(nid,uid,cid){
delete_c(nid,uid,cid);
};
$scope.ReplyInline = function(blog,user,relpy){
ReplyInline(blog,user,relpy);
};
});
</script>
<div class='SpaceList' style='margin-top:20px;' ng-app="blogComment" ng-controller="ListComment" ng-cloak>
<div class='BlogComments'>
<h2><a name="comments"></a>评论<em ng-bind="totalComment"></em></h2>
<ul id="BlogComments">
<li ng-repeat="commentjson in commentjsons" id='cmt_{{commentjson.blogId}}_{{commentjson.userId}}_{{commentjson.commentId}}' ng-class="{'even': {{($index+1)%2}} == 0}">
<table class='ostable'>
<tr>
<td class='portrait'>
<a ng-href="http://my.oschina.net/u/{{commentjson.userId}}" target="_blank">
<img ng-src="{{commentjson.portrait}}" align="absmiddle" title="{{ commentjson.userName }}" class="SmallPortrait" user="{{ commentjson.userId }}"/>
</a>
</td>
<td class='body'>
<div class='title'>
<span ng-bind="(currentPage-1)*pageSize+$index+1"></span>楼:<a ng-href="http://my.oschina.net/u/{{commentjson.userId}}" target="_blank" name="{{ commentjson.blogId }}" class="user" ng-bind="commentjson.userName"></a>
<span ng-if="(commentjson.client_type>10000 && commentjson.appApproved)">(<a href="{{commentjson.appUrl}}" target="_blank" title="{{commentjson.appTitle}} - {{commentjson.appDescription}}" ng-bind="commentjson.appTitle"></a>)</span>
<span ng-switch="{{commentjson.client_type}}">
<span ng-if="(commentjson.client_type==2)" ">手机</span>
<span ng-if="(commentjson.client_type==3)" ">Android</span>
<span ng-if="(commentjson.client_type==4)" ">iPhone</span>
<span ng-if="(commentjson.client_type==5)" ">Windows Phone</span>
<span ng-if="(commentjson.client_type==6)" ">微信</span>
</span>
发表于 <span ng-bind="commentjson.create_time"></span>
<a href="" ng-click="delete_c(commentjson.blogId, commentjson.userId, commentjson.commentId)" ng-if="commentjson.canAdmin">删除</a>
<a href="" ng-click="ReplyInline(commentjson.blogId, commentjson.userId, commentjson.commentId)" ng-if="commentjson.canCommit">回复此评论</a>
</div>
<div class='post' ng-bind-html="commentjson.content"></div>
<div id='inline_reply_of_{{commentjson.blogId}}_{{commentjson.userId}}_{{commentjson.commentId}}' class='inline_reply'></div>
</td>
</tr>
</table>
</li> </ul>
</div>
<ul class="pager">
<li class='page prev' ng-if="(currentPage>1)"><a href="?p={{currentPage-1}}#comments"><</a></li><li class='page' ng-if="(beginIndex>1)"><a href="?p=1#comments">1</a></li><li class='page' ng-if="totalPage>1" ng-class="{current:{{currentPage==page}}}" ng-repeat="page in pages"><a href="?p={{page}}#comments" ng-bind="page"></a></li><li class='page' ng-if="(endIndex<totalPage)"><a href="?p={{totalPage}}#comments" ng-bind="(totalPage)"></a></li><li class='page next' ng-if="(currentPage<totalPage)"><a href="?p={{currentPage+1}}#comments">></a></li></ul>
</div>
<!-------------------------------------------------------->
<div id='inline_reply_editor' style='display:none;'>
<div class="BlogCommentForm">
<form id="form_inline_comment" action="/action/blog/add_comment?blog=190612" method="POST">
<input type='hidden' id='inline_reply_id' name='reply_id' value=''/>
<textarea name="content" id="ccom_content" style="width:650px;height:60px;" onkeydown="if((event.metaKey || event.ctrlKey)&&event.keyCode==13){$('#form_inline_comment').submit();}"></textarea><br/>
<p>
<span style="float:right;">
<input type="button" value="关闭" class="blg_submit_btn" id='btn_close_inline_reply' style="background: rgba(255,255,255,0);color: #555;"/>
<input type="submit" value="回复" id="btn_comment" class="blg_submit_btn"/>
</span>
插入:
<a href="javascript:;" onclick="javascript:insert_emotions('ccom_content',this);" class="blog_emotion">表情</a>
<a href="javascript:insert_projects_c();" class="blog_soft">开源软件</a>
<span class="NoData" id="ficmp_msg" style="color:#F00"></span>
</p>
</form>
</div>
</div>
<div class='SpaceList' style='margin-top:20px;'>
<a name="comments" id="postform"></a>
<div class="BlogCommentForm">
<form id="form_comment" action="/action/blog/add_comment?blog=190612" method="POST">
<div class="comment_portrait">
<img src="/img/portrait.gif?t=1377425910000" align="absmiddle" alt="磁针石" title="磁针石" class="LargePortrait"/> </div>
<div class="comment_form">
<textarea name="content" id="wmd-input" style="width:650px;height:80px;" placeholder=""></textarea>
<p>
<button type="submit" class="blg_submit_btn" style="float:right;">发表评论</button>
插入:
<a href="javascript:insert_emotions();" class="blog_emotion">表情</a>
<a href="javascript:insert_projects();" class="blog_soft">开源软件</a>
<span class="NoData" id="cmt_tip"></span>
</p>
<div id="TweetFormPopupWraper">
<div id="TweetFormPopupArrow">
<div id="TweetFormPopup">
<div id='TweetEmotions'>
<div class='TweetPopupTitle'><a href="javascript:;" onclick="$('#TweetFormPopupWraper').hide();return false;">关闭</a>插入表情</div>
<a href='javascript:;' onclick='return ins_e(0);' class='emotion' style='background-position: -0px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(1);' class='emotion' style='background-position: -24px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(2);' class='emotion' style='background-position: -48px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(3);' class='emotion' style='background-position: -72px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(4);' class='emotion' style='background-position: -96px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(5);' class='emotion' style='background-position: -120px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(6);' class='emotion' style='background-position: -144px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(7);' class='emotion' style='background-position: -168px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(8);' class='emotion' style='background-position: -192px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(9);' class='emotion' style='background-position: -216px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(10);' class='emotion' style='background-position: -240px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(11);' class='emotion' style='background-position: -264px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(12);' class='emotion' style='background-position: -288px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(13);' class='emotion' style='background-position: -312px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(14);' class='emotion' style='background-position: -336px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(15);' class='emotion' style='background-position: -360px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(16);' class='emotion' style='background-position: -384px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(17);' class='emotion' style='background-position: -408px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(18);' class='emotion' style='background-position: -432px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(19);' class='emotion' style='background-position: -456px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(20);' class='emotion' style='background-position: -480px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(21);' class='emotion' style='background-position: -504px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(22);' class='emotion' style='background-position: -528px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(23);' class='emotion' style='background-position: -552px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(24);' class='emotion' style='background-position: -576px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(25);' class='emotion' style='background-position: -600px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(26);' class='emotion' style='background-position: -624px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(27);' class='emotion' style='background-position: -648px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(28);' class='emotion' style='background-position: -672px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(29);' class='emotion' style='background-position: -696px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(30);' class='emotion' style='background-position: -720px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(31);' class='emotion' style='background-position: -744px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(32);' class='emotion' style='background-position: -768px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(33);' class='emotion' style='background-position: -792px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(34);' class='emotion' style='background-position: -816px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(35);' class='emotion' style='background-position: -840px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(36);' class='emotion' style='background-position: -864px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(37);' class='emotion' style='background-position: -888px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(38);' class='emotion' style='background-position: -912px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(39);' class='emotion' style='background-position: -936px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(40);' class='emotion' style='background-position: -960px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(41);' class='emotion' style='background-position: -984px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(42);' class='emotion' style='background-position: -1008px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(43);' class='emotion' style='background-position: -1032px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(44);' class='emotion' style='background-position: -1056px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(45);' class='emotion' style='background-position: -1080px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(46);' class='emotion' style='background-position: -1104px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(47);' class='emotion' style='background-position: -1128px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(48);' class='emotion' style='background-position: -1152px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(49);' class='emotion' style='background-position: -1176px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(50);' class='emotion' style='background-position: -1200px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(51);' class='emotion' style='background-position: -1224px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(52);' class='emotion' style='background-position: -1248px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(53);' class='emotion' style='background-position: -1272px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(54);' class='emotion' style='background-position: -1296px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(55);' class='emotion' style='background-position: -1320px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(56);' class='emotion' style='background-position: -1344px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(57);' class='emotion' style='background-position: -1368px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(58);' class='emotion' style='background-position: -1392px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(59);' class='emotion' style='background-position: -1416px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(60);' class='emotion' style='background-position: -1440px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(61);' class='emotion' style='background-position: -1464px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(62);' class='emotion' style='background-position: -1488px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(63);' class='emotion' style='background-position: -1512px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(64);' class='emotion' style='background-position: -1536px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(65);' class='emotion' style='background-position: -1560px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(66);' class='emotion' style='background-position: -1584px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(67);' class='emotion' style='background-position: -1608px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(68);' class='emotion' style='background-position: -1632px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(69);' class='emotion' style='background-position: -1656px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(70);' class='emotion' style='background-position: -1680px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(71);' class='emotion' style='background-position: -1704px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(72);' class='emotion' style='background-position: -1728px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(73);' class='emotion' style='background-position: -1752px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(74);' class='emotion' style='background-position: -1776px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(75);' class='emotion' style='background-position: -1800px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(76);' class='emotion' style='background-position: -1824px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(77);' class='emotion' style='background-position: -1848px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(78);' class='emotion' style='background-position: -1872px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(79);' class='emotion' style='background-position: -1896px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(80);' class='emotion' style='background-position: -1920px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(81);' class='emotion' style='background-position: -1944px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(82);' class='emotion' style='background-position: -1968px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(83);' class='emotion' style='background-position: -1992px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(84);' class='emotion' style='background-position: -2016px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(85);' class='emotion' style='background-position: -2040px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(86);' class='emotion' style='background-position: -2064px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(87);' class='emotion' style='background-position: -2088px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(88);' class='emotion' style='background-position: -2112px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(89);' class='emotion' style='background-position: -2136px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(90);' class='emotion' style='background-position: -2160px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(91);' class='emotion' style='background-position: -2184px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(92);' class='emotion' style='background-position: -2208px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(93);' class='emotion' style='background-position: -2232px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(94);' class='emotion' style='background-position: -2256px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(95);' class='emotion' style='background-position: -2280px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(96);' class='emotion' style='background-position: -2304px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(97);' class='emotion' style='background-position: -2328px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(98);' class='emotion' style='background-position: -2352px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(99);' class='emotion' style='background-position: -2376px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(100);' class='emotion' style='background-position: -2400px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(101);' class='emotion' style='background-position: -2424px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(102);' class='emotion' style='background-position: -2448px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(103);' class='emotion' style='background-position: -2472px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(104);' class='emotion' style='background-position: -2496px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(105);' class='emotion' style='background-position: -2520px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(106);' class='emotion' style='background-position: -2544px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(107);' class='emotion' style='background-position: -2568px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(108);' class='emotion' style='background-position: -2592px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(109);' class='emotion' style='background-position: -2616px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(110);' class='emotion' style='background-position: -2640px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(111);' class='emotion' style='background-position: -2664px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(112);' class='emotion' style='background-position: -2688px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(113);' class='emotion' style='background-position: -2712px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(114);' class='emotion' style='background-position: -2736px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(115);' class='emotion' style='background-position: -2760px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(116);' class='emotion' style='background-position: -2784px 0px;'></a>
<a href='javascript:;' onclick='return ins_e(117);' class='emotion' style='background-position: -2808px 0px;'></a>