linux开发专注者(坚持原创)linuxfocus.blog.chinaunix.net

多阅读,多思考,多实践,多分享!

  • 博客访问: 5183928
  • 博文数量: 238
  • 博客积分: 10424
  • 博客等级: 少将
  • 技术积分: 14180
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-14 12:45
  • 认证徽章:
个人简介

啦啦啦~~~

文章分类

全部博文(238)

文章存档

2015年(5)

2014年(2)

2013年(22)

2012年(67)

2011年(120)

2010年(22)

微信关注

IT168企业级官微



微信号:IT168qiye



系统架构师大会



微信号:SACC2013

订阅
热词专题

分类: Python/Ruby

<span><span style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;display:inline !important;float:none;">作者:gfree.wind@gmail.com</span><br style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;" /> <span style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;display:inline !important;float:none;">博客:linuxfocus.blog.chinaunix.net</span><br style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;" /> <span style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;display:inline !important;float:none;">微博:weibo.com/glinuxer</span><br style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;" /> <span style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;display:inline !important;float:none;">QQ技术群:4367710</span> <div style="color:#000000;font-family:微软雅黑;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:normal;orphans:2;text-align:-webkit-auto;text-indent:0px;text-transform:none;white-space:normal;widows:2;word-spacing:0px;-webkit-text-size-adjust:auto;-webkit-text-stroke-width:0px;font-size:medium;"> github:&nbsp;https://github.com/gfreewind </div> </span><br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 刚毕业时,一方面不知道自己的方向在哪,另一方面对什么都有兴趣,于是什么东西都学一下,当时各种脚本基本上都会写一点。后来可能做的事情越来越专了,基本上就只写C代码了,偶尔由于个人爱好,也会写一些C++。但脚本语言基本上忘光光了,比如Perl,TCL等。<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 这段时间觉得Python有点意思,上手快,库丰富,可以快速做出功能,很适合搭建原型。所以有意识的让自己多写一些python代码。前几天给一个朋友,做个小功能,一个AES加解密服务。总共用了大概一天左右的时间,基本完成了第一版。<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 因为我还是python代码的初学者,所以想把代码分享给大家,希望大家多提宝贵意见。下面是github的地址,方便大家阅读最新的代码(由于我可能会实时更新):https://github.com/gfreewind/py_aes_http_service<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 为了方便大家,也在本文中贴出一份当前的代码:<br /> <br /> <div id="codeText" class="codeText"> <ol style="margin:0 1px 0 0;padding:5px 0pt;" start="1" class="dp-css none_number"> <li> <span style="color:#000000;">#<span style="color:#0000CC;">!</span><span style="color:#0000CC;">/</span>usr<span style="color:#0000CC;">/</span>bin<span style="color:#0000CC;">/</span>python<br /> </span> </li> <li> <br /> </li> <li> __author__ <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"Feng Gao gfree.wind@gmail.com"</span><br /> </li> <li> __copyright__ <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"Copyright (C) 2015 Feng Gao"</span><br /> </li> <li> __license__ <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"GPL"</span><br /> </li> <li> __version__ <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"1.0"</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">import</span> sys<span style="color:#0000CC;">,</span> socket<span style="color:#0000CC;">,</span> select<span style="color:#0000CC;">,</span> json<br /> </li> <li> <span style="color:#0000FF;">import</span> getopt<span style="color:#0000CC;">,</span> base64<span style="color:#0000CC;">,</span> binascii<br /> </li> <li> <span style="color:#0000FF;">import</span> commands<br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">from</span> BaseHTTPServer <span style="color:#0000FF;">import</span> BaseHTTPRequestHandler<br /> </li> <li> <span style="color:#0000FF;">from</span> StringIO <span style="color:#0000FF;">import</span> StringIO<br /> </li> <li> <span style="color:#0000FF;">from</span> Crypto<span style="color:#0000CC;">.</span>Cipher <span style="color:#0000FF;">import</span> AES<br /> </li> <li> <span style="color:#0000FF;">from</span> Crypto <span style="color:#0000FF;">import</span> Random<br /> </li> <li> <br /> </li> <li> DEBUG <span style="color:#0000CC;">=</span> False<br /> </li> <li> USE_OPENSSL <span style="color:#0000CC;">=</span> False<br /> </li> <li> SAVE_DETAIL <span style="color:#0000CC;">=</span> False<br /> </li> <li> LISTEN_PORT <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>80<span style="color:#0000CC;">)</span><br /> </li> <li> ENCRYPT_COUNT <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> AES_KEY <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"1234567890abcdef"</span><br /> </li> <li> AES_BLOCK_SIZE <span style="color:#0000CC;">=</span> 16<br /> </li> <li> AES_MODE <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_CBC<br /> </li> <li> AES_MODE_STR <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"cbc"</span><br /> </li> <li> AES_IV_STR <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">""</span><br /> </li> <li> OPENSSL_AES_MODE <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"aes-256-cbc"</span><br /> </li> <li> OPENSSL_PASSWORD <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"1234567890"</span><br /> </li> <li> <br /> </li> <li> <br /> </li> <li> AES_PAD <span style="color:#0000CC;">=</span> <span style="color:#0000FF;">lambda</span> s<span style="color:#0000CC;">:</span> s <span style="color:#0000CC;">+</span> <span style="color:#0000CC;">(</span>AES_BLOCK_SIZE <span style="color:#0000CC;">-</span> len<span style="color:#0000CC;">(</span>s<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">%</span> AES_BLOCK_SIZE<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">*</span> chr<span style="color:#0000CC;">(</span>AES_BLOCK_SIZE <span style="color:#0000CC;">-</span> len<span style="color:#0000CC;">(</span>s<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">%</span> AES_BLOCK_SIZE<span style="color:#0000CC;">)</span> <br /> </li> <li> AES_UNPAD <span style="color:#0000CC;">=</span> <span style="color:#0000FF;">lambda</span> s <span style="color:#0000CC;">:</span> s<span style="color:#0000CC;">[</span><span style="color:#0000CC;">:</span><span style="color:#0000CC;">-</span>ord<span style="color:#0000CC;">(</span>s<span style="color:#0000CC;">[</span>len<span style="color:#0000CC;">(</span>s<span style="color:#0000CC;">)</span><span style="color:#0000CC;">-</span>1<span style="color:#0000CC;">:</span><span style="color:#0000CC;">]</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">]</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">def</span> ConstructErrorResponse<span style="color:#0000CC;">(</span>err<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;lenth_str <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"Content-Length: "</span><span style="color:#0000CC;">+</span>str<span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>err<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">+</span><span style="color:#FF00FF;">"\r\n\r\n"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;err_header <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"HTTP/1.1 400 \r\nContent-Type: text/html\r\n"</span><span style="color:#0000CC;">+</span>lenth_str<span style="color:#0000CC;">+</span>err<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> err_header<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> <span style="color:#0000FF;">def</span> ConstructResponse<span style="color:#0000CC;">(</span>payload<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;lenth_str <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"Content-Length: "</span><span style="color:#0000CC;">+</span>str<span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>payload<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">+</span><span style="color:#FF00FF;">"\r\n\r\n"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n"</span><span style="color:#0000CC;">+</span>lenth_str<span style="color:#0000CC;">+</span>payload<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> response<br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">class</span> AESCipher<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> <span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> key<span style="color:#0000CC;">,</span> bs<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>key <span style="color:#0000CC;">=</span> key<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>bs <span style="color:#0000CC;">=</span> bs<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> encrypt<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> text<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> USE_OPENSSL<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"echo -n \""</span><span style="color:#0000CC;">+</span>text<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">"\" | openssl enc -"</span> <span style="color:#0000CC;">+</span> OPENSSL_AES_MODE<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">" -pass pass:"</span><span style="color:#0000CC;">+</span>OPENSSL_PASSWORD<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">" | openssl base64"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> DEBUG<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> cmd<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret<span style="color:#0000CC;">,</span> enc <span style="color:#0000CC;">=</span> commands<span style="color:#0000CC;">.</span>getstatusoutput<span style="color:#0000CC;">(</span>cmd<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text <span style="color:#0000CC;">=</span> AES_PAD<span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"PAD text size is"</span><span style="color:#0000CC;">,</span> len<span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"PAD text is"</span><span style="color:#0000CC;">,</span> text<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"AES.block size is"</span><span style="color:#0000CC;">,</span> AES<span style="color:#0000CC;">.</span>block_size<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iv <span style="color:#0000CC;">=</span> AES_IV_STR<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iv <span style="color:#0000CC;">=</span> Random<span style="color:#0000CC;">.</span>new<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">.</span>read<span style="color:#0000CC;">(</span>AES<span style="color:#0000CC;">.</span>block_size<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cipher <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>new<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>key<span style="color:#0000CC;">,</span> AES_MODE<span style="color:#0000CC;">,</span> iv<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enc <span style="color:#0000CC;">=</span> base64<span style="color:#0000CC;">.</span>b64encode<span style="color:#0000CC;">(</span>cipher<span style="color:#0000CC;">.</span>encrypt<span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enc <span style="color:#0000CC;">=</span> base64<span style="color:#0000CC;">.</span>b64encode<span style="color:#0000CC;">(</span>iv <span style="color:#0000CC;">+</span> cipher<span style="color:#0000CC;">.</span>encrypt<span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> SAVE_DETAIL<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">global</span> ENCRYPT_COUNT<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENCRYPT_COUNT <span style="color:#0000CC;">=</span> ENCRYPT_COUNT<span style="color:#0000CC;">+</span>1<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> str<span style="color:#0000CC;">(</span>ENCRYPT_COUNT<span style="color:#0000CC;">)</span><span style="color:#0000CC;">+</span><span style="color:#FF00FF;">". text: "</span><span style="color:#0000CC;">+</span>text<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">" ciper: "</span><span style="color:#0000CC;">+</span>enc<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> enc<br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> decrypt<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> enc<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"enc is"</span><span style="color:#0000CC;">,</span>enc<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> USE_OPENSSL<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cmd <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"echo \""</span><span style="color:#0000CC;">+</span>enc<span style="color:#0000CC;">+</span> <span style="color:#FF00FF;">"\" | openssl base64 -d | openssl enc -"</span><span style="color:#0000CC;">+</span> OPENSSL_AES_MODE<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">" -pass pass:"</span><span style="color:#0000CC;">+</span>OPENSSL_PASSWORD<span style="color:#0000CC;">+</span><span style="color:#FF00FF;">" -d"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> DEBUG<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> cmd<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ret<span style="color:#0000CC;">,</span> text <span style="color:#0000CC;">=</span> commands<span style="color:#0000CC;">.</span>getstatusoutput<span style="color:#0000CC;">(</span>cmd<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> text<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enc <span style="color:#0000CC;">=</span> base64<span style="color:#0000CC;">.</span>b64decode<span style="color:#0000CC;">(</span>enc<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iv <span style="color:#0000CC;">=</span> AES_IV_STR<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cipher <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>new<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>key<span style="color:#0000CC;">,</span> AES_MODE<span style="color:#0000CC;">,</span> iv<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> AES_UNPAD<span style="color:#0000CC;">(</span>cipher<span style="color:#0000CC;">.</span>decrypt<span style="color:#0000CC;">(</span>enc<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iv <span style="color:#0000CC;">=</span> enc<span style="color:#0000CC;">[</span><span style="color:#0000CC;">:</span>AES<span style="color:#0000CC;">.</span>block_size<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cipher <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>new<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>key<span style="color:#0000CC;">,</span> AES_MODE<span style="color:#0000CC;">,</span> iv<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> AES_UNPAD<span style="color:#0000CC;">(</span>cipher<span style="color:#0000CC;">.</span>decrypt<span style="color:#0000CC;">(</span>enc<span style="color:#0000CC;">[</span>AES<span style="color:#0000CC;">.</span>block_size<span style="color:#0000CC;">:</span><span style="color:#0000CC;">]</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">class</span> HTTPRequest<span style="color:#0000CC;">(</span>BaseHTTPRequestHandler<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> <span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> request_text<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>rfile <span style="color:#0000CC;">=</span> StringIO<span style="color:#0000CC;">(</span>request_text<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>raw_requestline <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>rfile<span style="color:#0000CC;">.</span>readline<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>error_code <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>error_message <span style="color:#0000CC;">=</span> None<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>parse_request<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> send_error<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> code<span style="color:#0000CC;">,</span> message<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>error_code <span style="color:#0000CC;">=</span> code<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>error_message <span style="color:#0000CC;">=</span> message<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> <span style="color:#0000FF;">class</span> HTTPHeaders<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> <span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> data<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>parsed_http <span style="color:#0000CC;">=</span> HTTPRequest<span style="color:#0000CC;">(</span>data<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>uri <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">""</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>params <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">""</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'?'</span> <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>parsed_http<span style="color:#0000CC;">.</span>path<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>uri<span style="color:#0000CC;">,</span> param_str <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>parsed_http<span style="color:#0000CC;">.</span>path<span style="color:#0000CC;">.</span>split<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'?'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;params <span style="color:#0000CC;">=</span> param_str<span style="color:#0000CC;">.</span>split<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'&amp;'</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>params <span style="color:#0000CC;">=</span> <span style="color:#0000CC;">{</span><span style="color:#0000CC;">}</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">for</span> str <span style="color:#0000FF;">in</span> params<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name<span style="color:#0000CC;">,</span> value <span style="color:#0000CC;">=</span> str<span style="color:#0000CC;">.</span>split<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">'='</span><span style="color:#0000CC;">,</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>params<span style="color:#0000CC;">[</span>name<span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> value<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>uri <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>parsed_http<span style="color:#0000CC;">.</span>path<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>show_headers<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> show_headers<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parsed_http <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>parsed_http<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headers <span style="color:#0000CC;">=</span> parsed_http<span style="color:#0000CC;">.</span>headers<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Command:"</span><span style="color:#0000CC;">,</span> parsed_http<span style="color:#0000CC;">.</span>command<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Request_version:"</span><span style="color:#0000CC;">,</span> parsed_http<span style="color:#0000CC;">.</span>request_version<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Requestline:"</span><span style="color:#0000CC;">,</span> parsed_http<span style="color:#0000CC;">.</span>requestline<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Path:"</span><span style="color:#0000CC;">,</span> parsed_http<span style="color:#0000CC;">.</span>path<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"URI:"</span><span style="color:#0000CC;">,</span> self<span style="color:#0000CC;">.</span>uri<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Params:"</span><span style="color:#0000CC;">,</span> self<span style="color:#0000CC;">.</span>params<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Headers:"</span> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">for</span> <span style="color:#0000CC;">(</span>h<span style="color:#0000CC;">,</span>v<span style="color:#0000CC;">)</span> <span style="color:#0000FF;">in</span> parsed_http<span style="color:#0000CC;">.</span>headers<span style="color:#0000CC;">.</span>items<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"\t"</span><span style="color:#0000CC;">,</span> h<span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">": "</span><span style="color:#0000CC;">,</span> v<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> get_header_value<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> header_name<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;headers <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>parsed_http<span style="color:#0000CC;">.</span>headers<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#0000FF;">not</span> headers<span style="color:#0000CC;">.</span>has_key<span style="color:#0000CC;">(</span>header_name<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> None<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> headers<span style="color:#0000CC;">[</span>header_name<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> get_param_value<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> param<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> param <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>params<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> self<span style="color:#0000CC;">.</span>params<span style="color:#0000CC;">[</span>param<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> None&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> <span style="color:#0000FF;">class</span> HTTPClient<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> <span style="color:#FF0000;">__init__</span><span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> socket<span style="color:#0000CC;">,</span> addr<span style="color:#0000CC;">,</span> aes<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>header <span style="color:#0000CC;">=</span> HTTPRequest<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">""</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>socket <span style="color:#0000CC;">=</span> socket<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>addr <span style="color:#0000CC;">=</span> addr<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>aes <span style="color:#0000CC;">=</span> aes<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>fileno <span style="color:#0000CC;">=</span> socket<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>header_is_ok <span style="color:#0000CC;">=</span> False<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>request <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">""</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>data_len <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"0"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> join_epoll<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">,</span> epoll<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> self<span style="color:#0000CC;">.</span>addr<span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">"is connected"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>socket<span style="color:#0000CC;">.</span>setblocking<span style="color:#0000CC;">(</span>0<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epoll<span style="color:#0000CC;">.</span>register<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">,</span> select<span style="color:#0000CC;">.</span>EPOLLIN<span style="color:#0000CC;">|</span>select<span style="color:#0000CC;">.</span>EPOLLHUP<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>epoll <span style="color:#0000CC;">=</span> epoll<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> leave_epoll<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> self<span style="color:#0000CC;">.</span>addr<span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">"is disconnected"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>epoll<span style="color:#0000CC;">.</span>unregister<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>socket<span style="color:#0000CC;">.</span>close<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> get_fileno<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> self<span style="color:#0000CC;">.</span>fileno<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> parse_http_header<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\r\n\r\n"</span> <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sep_index <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">.</span>find<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\r\n\r\n"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sep_index <span style="color:#0000CC;">+</span><span style="color:#0000CC;">=</span> len<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\r\n\r\n"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\n\n"</span> <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sep_index <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">.</span>find<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\n\n"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sep_index <span style="color:#0000CC;">+</span><span style="color:#0000CC;">=</span> len<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\n"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Unexpected error"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit<span style="color:#0000CC;">(</span><span style="color:#0000CC;">-</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;header <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">[</span><span style="color:#0000CC;">:</span>sep_index<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>request <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">[</span>sep_index<span style="color:#0000CC;">:</span><span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>http_headers <span style="color:#0000CC;">=</span> HTTPHeaders<span style="color:#0000CC;">(</span>header<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>header_is_ok <span style="color:#0000CC;">=</span> True<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> read_data<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>socket<span style="color:#0000CC;">.</span>recv<span style="color:#0000CC;">(</span>1024<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>0 <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> len<span style="color:#0000CC;">(</span>data<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>leave_epoll<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span> <span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>request <span style="color:#0000CC;">+</span><span style="color:#0000CC;">=</span> data<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#0000FF;">not</span> self<span style="color:#0000CC;">.</span>header_is_ok<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>header_is_completed<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>parse_http_header<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>data_len <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>http_headers<span style="color:#0000CC;">.</span>get_header_value<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"Content-Length"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>None <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>data_len<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>data_len <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"0"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data_len <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>data_len<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>header_is_ok <span style="color:#0000FF;">and</span> data_len <span style="color:#0000CC;">&lt;</span><span style="color:#0000CC;">=</span> len<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json_data <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">[</span><span style="color:#0000CC;">:</span>data_len<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>request <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">[</span>data_len<span style="color:#0000CC;">:</span><span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Payload:"</span><span style="color:#0000CC;">,</span> json_data<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>data_len<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">try</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json_data <span style="color:#0000CC;">=</span> json<span style="color:#0000CC;">.</span>loads<span style="color:#0000CC;">(</span>json_data<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">except</span> <span style="color:#FF0000;">ValueError</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Invalid json data"</span><span style="color:#0000CC;">,</span> json_data<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Get action<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;action <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>http_headers<span style="color:#0000CC;">.</span>get_param_value<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"action"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"enc"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> action<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>http_headers<span style="color:#0000CC;">.</span>get_param_value<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"text"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ciper <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>aes<span style="color:#0000CC;">.</span>encrypt<span style="color:#0000CC;">(</span>text<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload <span style="color:#0000CC;">=</span> <span style="color:#0000CC;">{</span><span style="color:#0000CC;">}</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload<span style="color:#0000CC;">[</span><span style="color:#FF00FF;">"text"</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> text<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload<span style="color:#0000CC;">[</span><span style="color:#FF00FF;">"ciper"</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> ciper<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jason_data <span style="color:#0000CC;">=</span> json<span style="color:#0000CC;">.</span>dumps<span style="color:#0000CC;">(</span>payload<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> ConstructResponse<span style="color:#0000CC;">(</span>jason_data<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> ConstructErrorResponse<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"No text param"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"dec"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> action<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ciper <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>http_headers<span style="color:#0000CC;">.</span>get_param_value<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"ciper"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>ciper<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;text <span style="color:#0000CC;">=</span> self<span style="color:#0000CC;">.</span>aes<span style="color:#0000CC;">.</span>decrypt<span style="color:#0000CC;">(</span>ciper<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload <span style="color:#0000CC;">=</span> <span style="color:#0000CC;">{</span><span style="color:#0000CC;">}</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload<span style="color:#0000CC;">[</span><span style="color:#FF00FF;">"text"</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> text<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;payload<span style="color:#0000CC;">[</span><span style="color:#FF00FF;">"ciper"</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> ciper<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jason_data <span style="color:#0000CC;">=</span> json<span style="color:#0000CC;">.</span>dumps<span style="color:#0000CC;">(</span>payload<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> ConstructResponse<span style="color:#0000CC;">(</span>jason_data<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> ConstructErrorResponse<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"No ciper param"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"No supported operation"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response <span style="color:#0000CC;">=</span> ConstructErrorResponse<span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"No supported aciton or no specify action"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>socket<span style="color:#0000CC;">.</span>send<span style="color:#0000CC;">(</span>response<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self<span style="color:#0000CC;">.</span>leave_epoll<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">def</span> header_is_completed<span style="color:#0000CC;">(</span>self<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\r\n\r\n"</span> <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">)</span> <span style="color:#0000FF;">or</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"\n\n"</span> <span style="color:#0000FF;">in</span> self<span style="color:#0000CC;">.</span>request<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> True<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">return</span> False<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> <span style="color:#0000FF;">def</span> usage<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-h: Show the help"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-l: Specify listen port"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-k: Specify AES key"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-e: Specify AES encrypt mode. default is cbc"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-s: Specify AES block size"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-v: Specify AES IV. default is random string"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-o: Use openssl to encrypt or decrypt. Must specify password too"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-p: Specify password. Only used with openssl"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-m: Specify openssl enc mode. Only used with openssl"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-t: Save the encrypt count and record"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"-d: Debug mode"</span><br /> </li> <li> <br /> </li> <li> <span style="color:#0000FF;">if</span> <span style="color:#FF0000;">__name__</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"__main__"</span><span style="color:#0000CC;">:</span><br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">try</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;opts<span style="color:#0000CC;">,</span> args <span style="color:#0000CC;">=</span> getopt<span style="color:#0000CC;">.</span>getopt<span style="color:#0000CC;">(</span>sys<span style="color:#0000CC;">.</span>argv<span style="color:#0000CC;">[</span>1<span style="color:#0000CC;">:</span><span style="color:#0000CC;">]</span><span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">"hl:k:e:s:v:p:tdo"</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">except</span> getopt<span style="color:#0000CC;">.</span>GetoptError as err<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> str<span style="color:#0000CC;">(</span>err<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">for</span> o<span style="color:#0000CC;">,</span> a <span style="color:#0000FF;">in</span> opts<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-h"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;usage<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-l"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LISTEN_PORT <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>a<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-k"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_KEY <span style="color:#0000CC;">=</span> a<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_KEY<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> 16 <span style="color:#0000FF;">and</span> len<span style="color:#0000CC;">(</span>AES_KEY<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> 24 <span style="color:#0000FF;">and</span> len<span style="color:#0000CC;">(</span>AES_KEY<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> 32<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"AES key must be either 16, 24, or 32 bytes long"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-e"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"ecb"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_ECB<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"cbc"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE <span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_CBC<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"cfb"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_CFB<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"ofb"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_OFB<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span><span style="color:#FF00FF;">"ctr"</span> <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> a<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> AES<span style="color:#0000CC;">.</span>MODE_CTR<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Only support (ecb|cbc|cfb|ofb|ctr)"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_MODE_STR <span style="color:#0000CC;">=</span> a<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-s"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_BLOCK_SIZE <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>a<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-v"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;AES_IV_STR <span style="color:#0000CC;">=</span> a<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span> <span style="color:#0000CC;">!</span><span style="color:#0000CC;">=</span> 16<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"AES IV size must be 16"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-p"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OPENSSL_PASSWORD <span style="color:#0000CC;">=</span> a<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-m"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OPENSSL_AES_MODE <span style="color:#0000CC;">=</span> a<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-o"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;USE_OPENSSL <span style="color:#0000CC;">=</span> True<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-t"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SAVE_DETAIL <span style="color:#0000CC;">=</span> True<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ENCRYPT_COUNT <span style="color:#0000CC;">=</span> int<span style="color:#0000CC;">(</span>0<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>o <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">"-d"</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DEBUG <span style="color:#0000CC;">=</span> True<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">assert</span> False<span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">"unhandled option"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>USE_OPENSSL <span style="color:#0000FF;">and</span> 0 <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> len<span style="color:#0000CC;">(</span>OPENSSL_PASSWORD<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Must set password when use openssl"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys<span style="color:#0000CC;">.</span>exit<span style="color:#0000CC;">(</span>1<span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;master_socket <span style="color:#0000CC;">=</span> socket<span style="color:#0000CC;">.</span>socket<span style="color:#0000CC;">(</span>socket<span style="color:#0000CC;">.</span>AF_INET<span style="color:#0000CC;">,</span> socket<span style="color:#0000CC;">.</span>SOCK_STREAM<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;master_socket<span style="color:#0000CC;">.</span>setsockopt<span style="color:#0000CC;">(</span>socket<span style="color:#0000CC;">.</span>SOL_SOCKET<span style="color:#0000CC;">,</span> socket<span style="color:#0000CC;">.</span>SO_REUSEADDR<span style="color:#0000CC;">,</span> 1<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;master_socket<span style="color:#0000CC;">.</span>bind<span style="color:#0000CC;">(</span><span style="color:#0000CC;">(</span><span style="color:#FF00FF;">''</span><span style="color:#0000CC;">,</span> LISTEN_PORT<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;master_socket<span style="color:#0000CC;">.</span>listen<span style="color:#0000CC;">(</span>5<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;master_socket<span style="color:#0000CC;">.</span>setblocking<span style="color:#0000CC;">(</span>0<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;ass_ciper <span style="color:#0000CC;">=</span> AESCipher<span style="color:#0000CC;">(</span>AES_KEY<span style="color:#0000CC;">,</span> AES_BLOCK_SIZE<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000FF;">not</span> USE_OPENSSL<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> DEBUG<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Only support aes-128. Please use -o option to use openssl if you want to use other bits"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>DEBUG<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES server is listenning the"</span><span style="color:#0000CC;">,</span> LISTEN_PORT<span style="color:#0000CC;">,</span> <span style="color:#FF00FF;">"port now"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES key is"</span><span style="color:#0000CC;">,</span> AES_KEY<span style="color:#0000CC;">,</span> binascii<span style="color:#0000CC;">.</span>b2a_hex<span style="color:#0000CC;">(</span>AES_KEY<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES mode is"</span><span style="color:#0000CC;">,</span> AES_MODE_STR<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES block size is"</span><span style="color:#0000CC;">,</span> AES_BLOCK_SIZE<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>len<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES iv is"</span><span style="color:#0000CC;">,</span> AES_IV_STR<span style="color:#0000CC;">,</span> binascii<span style="color:#0000CC;">.</span>b2a_hex<span style="color:#0000CC;">(</span>AES_IV_STR<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"The AES iv uses random string"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>USE_OPENSSL<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Use openssl to encrypt/decrypt"</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"password is"</span><span style="color:#0000CC;">,</span> OPENSSL_PASSWORD<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"openssl aes mode is"</span><span style="color:#0000CC;">,</span> OPENSSL_AES_MODE<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> <span style="color:#0000CC;">(</span>SAVE_DETAIL<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Save details"</span><br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;epoll <span style="color:#0000CC;">=</span> select<span style="color:#0000CC;">.</span>epoll<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;epoll<span style="color:#0000CC;">.</span>register<span style="color:#0000CC;">(</span>master_socket<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">,</span> select<span style="color:#0000CC;">.</span>EPOLLIN<span style="color:#0000CC;">)</span><br /> </li> <li> <br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">try</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_clients <span style="color:#0000CC;">=</span> <span style="color:#0000CC;">{</span><span style="color:#0000CC;">}</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">while</span> <span style="color:#0000CC;">(</span>True<span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;request <span style="color:#0000CC;">=</span> <span style="color:#FF00FF;">""</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;events <span style="color:#0000CC;">=</span> epoll<span style="color:#0000CC;">.</span>poll<span style="color:#0000CC;">(</span>3<span style="color:#0000CC;">)</span> # wait 3 seconds<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">for</span> fileno<span style="color:#0000CC;">,</span> event <span style="color:#0000FF;">in</span> events<span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">if</span> fileno <span style="color:#0000CC;">=</span><span style="color:#0000CC;">=</span> master_socket<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;worker_socket<span style="color:#0000CC;">,</span> client_addr <span style="color:#0000CC;">=</span> master_socket<span style="color:#0000CC;">.</span>accept<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_client <span style="color:#0000CC;">=</span> HTTPClient<span style="color:#0000CC;">(</span>worker_socket<span style="color:#0000CC;">,</span> client_addr<span style="color:#0000CC;">,</span> ass_ciper<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_client<span style="color:#0000CC;">.</span>join_epoll<span style="color:#0000CC;">(</span>epoll<span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_clients<span style="color:#0000CC;">[</span>http_client<span style="color:#0000CC;">.</span>get_fileno<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">=</span> http_client<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">elif</span> <span style="color:#0000CC;">(</span>event <span style="color:#0000CC;">&amp;</span> <span style="color:#0000CC;">(</span>select<span style="color:#0000CC;">.</span>EPOLLIN<span style="color:#0000CC;">|</span>select<span style="color:#0000CC;">.</span>EPOLLHUP<span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_client <span style="color:#0000CC;">=</span> http_clients<span style="color:#0000CC;">[</span>fileno<span style="color:#0000CC;">]</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http_client<span style="color:#0000CC;">.</span>read_data<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">else</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">print</span> <span style="color:#FF00FF;">"Unhandled event"</span><span style="color:#0000CC;">,</span> event<br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;<span style="color:#0000FF;">finally</span><span style="color:#0000CC;">:</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;epoll<span style="color:#0000CC;">.</span>unregister<span style="color:#0000CC;">(</span>master_socket<span style="color:#0000CC;">.</span>fileno<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">)</span><br /> </li> <li> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;master_socket<span style="color:#0000CC;">.</span>close<span style="color:#0000CC;">(</span><span style="color:#0000CC;">)</span><span style="color:#0000CC;">;</span> </li> </ol> </div> <br /> <br /> <br /> <br /> <br />
阅读(5858) | 评论(0) | 转发(0) |
0

上一篇:Netfilter连接跟踪状态解析

下一篇:没有了

给主人留下些什么吧!~~
评论热议
请登录后评论。

登录 注册