免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2792 | 回复: 1
打印 上一主题 下一主题

初学Python,写一个小程序练练手  关闭 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-02-27 17:11 |只看该作者 |倒序浏览
看精华贴时可以把一些BBCode转成html
本人初学,时间仓促,请大家多指教

  1. #! C:\Python23\python.exe
  2. # -*- coding: cp936 -*-

  3. import BaseHTTPServer
  4. import urllib
  5. import webbrowser
  6. import re

  7. PORT = 10080
  8. CUDistillateRoot = "http://www.chinaunix.net/jh/";
  9. pageStyle = r"""
  10. <!--
  11. .code {
  12.         font-family: 'song',Verdana; font-size: 12px; color: #006600;
  13.         background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
  14.         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px
  15. }

  16. .quote {
  17.         font-family: 'song',Verdana; font-size: 12px; color: #444444; line-height: 125%;
  18.         background-color: #FAFAFA; border: #D1D7DC; border-style: solid;
  19.         border-left-width: 1px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px
  20. }
  21. -->
  22. </style>
  23. """

  24. def main():
  25.     httpserver = BaseHTTPServer.HTTPServer(("", PORT), CUDistillateHandler)
  26.     webbrowser.open("http://localhost:" + `PORT`);
  27.     httpserver.serve_forever()

  28. def replace_TAG(htmlcode):
  29.     htmlcode = re.compile(r'(src="?)(/images/\w*"?)').sub(r"\1http://www.chinaunix.net\2", htmlcode)
  30.     htmlcode = re.compile(r'(<a href="?)http://www.chinaunix.net/jh(/\d{1,3}/\d*\.html?"?)').sub(r"\1\2", htmlcode)
  31.     return htmlcode

  32. def replace_BBCode(htmlcode):
  33.     htmlcode = re.compile(r'</style>',re.VERBOSE).sub(pageStyle, htmlcode)
  34.    
  35.     htmlcode = re.compile(r'\[b:[^\]]*\]').sub(r'<span style="font-weight: bold">', htmlcode)
  36.     htmlcode = re.compile(r'\[/b:[^\]]*\]').sub(r'</span>', htmlcode)
  37.    
  38.     htmlcode = re.compile(r'\[color=(\w*):[^\]]*\]').sub(r'<span style="color: \1">', htmlcode)
  39.     htmlcode = re.compile(r'\[/color:[^\]]*\]').sub(r'</span>', htmlcode)
  40.    
  41.     htmlcode = re.compile(r'\[url=([^\]]*)\]([^\[]*)\[/url\]').sub(r'<a href=\1 target=_blank>\2</a>', htmlcode)
  42.     htmlcode = re.compile(r'\[url\]([^\[]*)\[/url\]').sub(r'<a href=\1 target=_blank>\1</a>', htmlcode)
  43.    
  44.     htmlcode = re.compile(r'\[code:\w{1,}:[^\]]*\]').sub(r"""
  45.         <table width=90% border=0 align=center cellspacing=1 cellpadding=3><tr><td class=code>""", htmlcode)
  46.     htmlcode = re.compile(r'\[/code:\w{1,}:[^\]]*\]').sub(r'</td></tr></table>', htmlcode)

  47.     htmlcode = re.compile(r'\[quote:\w{1,}="([^"]*)"\]').sub(r"""
  48.         <table width=90% border=0 align=center cellspacing=1 cellpadding=3>
  49.         <tr><td><br/><b>\1写到:</b></td></tr>
  50.         <tr><td class=quote>""", htmlcode)
  51.     htmlcode = re.compile(r'\[/quote:[^\]]*\]').sub(r'</td></tr></table>', htmlcode)

  52.     htmlcode = re.compile(r'\[list:[^\]]*\]').sub(r'<ol>', htmlcode)
  53.     htmlcode = re.compile(r'\[list=(\w{1}):[^\]]*\]').sub(r'<ol type=\1>', htmlcode)
  54.     htmlcode = re.compile(r'\[/list[^\]]*\]').sub(r'</ol>', htmlcode)
  55.     htmlcode = re.compile(r'\[\*:\w*\]').sub(r'<li>', htmlcode)
  56.    
  57.     return htmlcode

  58. class CUDistillateHandler(BaseHTTPServer.BaseHTTPRequestHandler):   
  59.     def do_GET(self):
  60.         if re.match("^/\d{1,3}", self.path):
  61.             srcurl = CUDistillateRoot + self.path            
  62.         else:
  63.             srcurl = CUDistillateRoot
  64.         try:
  65.             htmlpage = urllib.urlopen(srcurl).read()
  66.             htmlpage = replace_TAG(htmlpage)
  67.             if re.match("^/\d{1,3}/\d{1,}\.html$", self.path):
  68.                 htmlpage = replace_BBCode(htmlpage)
  69.         except:
  70.             htmlpage = """
  71.                 <html>
  72.                 <body><h1>不能访问www.chinaunix.net,请检查网络连接</h1>
  73.                 </body>
  74.                 </html>
  75.             """
  76.         self.send_response(200)
  77.         self.send_header("Content-type", "text/html")
  78.         self.end_headers()
  79.         self.wfile.write(htmlpage)

  80. if __name__ == '__main__':
  81.     main()
复制代码

cudistillate.jpg (140.59 KB, 下载次数: 73)

cudistillate.jpg

论坛徽章:
0
2 [报告]
发表于 2004-02-27 20:41 |只看该作者

初学Python,写一个小程序练练手

各位,抱歉,没有地方发,只能发在shell版了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP