iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >python之nntp服务器组
  • 232
分享到

python之nntp服务器组

服务器pythonnntp 2023-01-31 06:01:09 232人浏览 薄情痞子
摘要

  Http://www.newzbot.com/serverlist.PHP?since=ALL&orderby=kps&sortorder=desc&show_maxgroup=on&show_post=

 

  1. Http://www.newzbot.com/serverlist.PHP?since=ALL&orderby=kps&sortorder=desc&show_maxgroup=on&show_post=on&show_kps=on&show_created=on 
这是可以找到当前有哪些服务器的地方网址,

这个项目的目的就是收集信息,并且将其生成一个html的报告(当然也可以是其他的形式的报告),完成代码如下

  1. '''''  
  2. Created on 2012-7-18  
  3.  
  4. @author: mars  
  5. ''' 
  6. import nntplib  
  7. from nntplib import NNTP  
  8. from time import time,strftime,localtime  
  9. from email import message_from_string  
  10. from urllib import urlopen  
  11. import textwrap  
  12. import re  
  13.  
  14. day=24*60*60 
  15. def wrap(string,max=70):  
  16.     #make the string to the max linewidth  
  17.     return '\n'.join(textwrap.wrap(string))+'\n' 
  18. class NewsAgent:  
  19.     #can get the new project and announce to the object fo the new from the souuce of the news  
  20.     def __init__(self):  
  21.         self.sources=[]  
  22.         self.destinations=[]  
  23.     def addSource(self,source):  
  24.         self.sources.append(source)  
  25.     def aDDDestination(self,dest):  
  26.         self.destinations.append(dest)  
  27.     def distribute(self):  
  28.         items=[]  
  29.         for source in self.sources:  
  30.             items.extend(source.getItems())  
  31.         for dest in self.destinations:  
  32.             dest.receiveItems(items)  
  33. class NewsItem:  
  34.     #simle news project including tile and text  
  35.     def __init__(self,title,body):  
  36.         self.title=title  
  37.         self.body=body  
  38.  
  39. class NNTPSource:  
  40.     #the nntp source   
  41.     def __init__(self,servername,group,window):  
  42.         self.servername=servername  
  43.         self.group=group  
  44.         self.window=window  
  45.     def getItems(self):  
  46.         start=localtime(time()-self.window*day)  
  47.         date=strftime('%y%m%d',start)  
  48.         hour=strftime('%H%M%S',start)  
  49.           
  50.         server=NNTP(self.servername)  
  51.           
  52.         ids=server.group(self.group)[2]  
  53.         #ids=server.newnews(self.group, date, hour)[1]  
  54.           
  55.         for id in ids:  
  56.             lines=server.article(id)[3]  
  57.             message=message_from_string('\n'.join(lines))  
  58.               
  59.             title=message['subject']  
  60.             body=message.get_payload()  
  61.             if message.is_multipart():  
  62.                 body=body[0]  
  63.                   
  64.             yield NewsItem(title,body)  
  65.               
  66.         server.quit()  
  67.  
  68. class SimpleWEBSource:  
  69.     #user the re  to fetch thr source from the webpage  
  70.     def __init__(self,url,titlePattern,bodyPattern):  
  71.         self.url=url  
  72.         self.titlePattern=re.compile(titlePattern)  
  73.         self.bodyPattern=re.compile(bodyPattern)  
  74.     def getItems(self):  
  75.         text=urlopen(self.url).read()  
  76.         titles=self.titlePattern.findall(text)  
  77.         bodies=self.bodyPattern.findall(text)  
  78.         for title,body in zip(titles,bodies):  
  79.             yield NewsItem(title.wrap(body))  
  80. class PlainDestination:  
  81.     #make it to the pure text  
  82.     def receiveItems(self,items):  
  83.         for item in items:  
  84.             print item.title  
  85.             #print '-'*len(subject)  
  86.             #print '-'*len(item.title)  
  87.             print item.body  
  88.             #print 'fuck&&&&&&&bitch'  
  89. class HTMLDestination:  
  90.     # make it to the html   
  91.     def __init__(self, filename):  
  92.         self.filename = filename  
  93.           
  94.     def receiveItems(self, items):  
  95.         out = open(self.filename, 'w')  
  96.         print >> out, """  
  97.         <html>  
  98.             <head>  
  99.                 <title>Today's News</title>  
  100.             </head>  
  101.             <body>  
  102.             <h1>Today's News</h1>  
  103.         """ 
  104.       
  105.         print >> out, '<ul>' 
  106.         id = 0 
  107.         for item in items:  
  108.             id += 1 
  109.             print >> out, '<li><a href="#%i">%s</a></li>' % (id, item.title)  
  110.         print >> out, '</ul>' 
  111.           
  112.         id = 0 
  113.         for item in items:  
  114.             id += 1 
  115.             print >> out, '<h2><a name="%i">%s</a></h2>' % (id, item.title)  
  116.             print >> out, '<pre>%s</pre>' % item.body  
  117.               
  118.         print >> out, """  
  119.             </body>  
  120.         </html>  
  121.         """ 
  122.           
  123. class runDefaultSetup():  
  124.     #the souce  can modify by yourself  
  125.     agent=NewsAgent()  
  126.     #bbc_url='http://www.chinanews.com/'  
  127.     bbc_url='http://www.bbc.co.uk/news/' 
  128.     #bbc_url='http://www.bbc.co.uk/text_only.stm'  
  129.     bbc_title=r'(?s)a href="[^"]*>\s*<b>\s*(.*?)\s*</b>' 
  130.     bbc_body=r'(?s)</a>\s*<br/>\s*(.*?)\s*<' 
  131.     bbc=SimpleWebSource(bbc_url,bbc_title,bbc_body)  
  132.       
  133.     agent.addSource(bbc)  
  134.     #cong gmane.comp.python.announce get the nntpsource  
  135.       
  136.     clpa_server='news.gmane.org' 
  137.     clpa_group='gmane.comp.Python.apple' 
  138.       
  139.     clpa_window=1 
  140.     clpa=NNTPSource(clpa_server,clpa_group,clpa_window)  
  141.     agent.addSource(clpa)  
  142.       
  143.       
  144.     #add the text and html target  
  145.     agent.addDestination(PlainDestination())  
  146.       
  147.     agent.addDestination(HTMLDestination('news.html'))  
  148.       
  149.     #public  
  150.     agent.distribute()  
  151. if __name__=='__main__':  
  152.     runDefaultSetup()  
  153.           
  154.  
  155.  

其实这个程序呢 在第二版的教程上有,不过呢 那个给出的服务器不能用,所以在文章的开始的时候我就给出了 可以找到服务器地址的地方,比如我这里用的就是

    clpa_server='news.gmane.org'
    clpa_group='gmane.comp.python.apple'
    这个!

当然这段代码我也稍微说下,最开始的类NewsAgent,接着是NewsItem,NNTPSource,SimpleWebSource,PlainDestination,HTMLDestination和runDefaultSetup

程序一运行就开始跑的是runDefaultSetup,这里就将NewsAgent实例化为agent,SimpleWebSource的3个参数分别是url,  title和body,然后将其实例化为bbc!

随后将bbc作为参数,调用agent的addsource。同样的道理完成了nntpsouce这一块。

最后就是就是调用agent.addDestionation。最后HTMLDestionation以news.html作为生成报告的html文本!

--结束END--

本文标题: python之nntp服务器组

本文链接: https://www.lsjlt.com/news/190632.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • python之nntp服务器组
      http://www.newzbot.com/serverlist.phpsince=ALL&orderby=kps&sortorder=desc&show_maxgroup=on&show_post=o...
    99+
    2023-01-31
    服务器 python nntp
  • Python之元组
    元组 元组和序列区别 元组tuple元组特点:属于准序列类型,支持复杂数据类型,长度不限制,不可变类型(增删修改元素,排序),支持切片操作,使用()包裹起来。 元组的创建 查看类型 a=tuple('hello')print(a...
    99+
    2023-01-31
    Python
  • Python列表之元组
    元组属于序列的一种1.    序列包括3种字符串、列表和元组都是序列。序列的两个主要特点是所有操作符和切片操作符。a.    索引操作符让我们可以从序列中抓取一个特定项目b.    切片操作符让我们能获取序列的一个切片,即一部分序列2.  ...
    99+
    2023-01-31
    列表 Python
  • python基础之元组
    元组: # 元组,一种不可变的序列,在创建之后不能做任何的修改 # 1.不可变 # 2.用()创建元组类型,数据项用逗号来分割 # 3.可以是任何的类型 # 4.党员组中只有一...
    99+
    2024-04-02
  • python学习之元组
    在python中,元组使用小括号,小括号的元素使用逗号隔开即可; 1.元组和列表的区别元组和列表的相同点就是都是序列类型的容器对象,可以存放任何类型的数据,支持切片,迭代操作等;元组和列表的不同点是元组是不可变类型,大小固定,而列表是可变...
    99+
    2023-01-31
    python
  • day25-python之继承组合
    1.上节回顾 1 class School: 2 x=1 3 def __init__(self,name,addr,type): 4 self.Name=name 5 self...
    99+
    2023-01-31
    组合 python
  • python学习之数组二
    作用于数组的函数: 通用函数:函数基于元素的,以单元方式作用于数组的,返回的是与原数组具有相同形状的数组。 不通用函数(数组函数):函数能以行或者列的方式作用于整个矩阵;如果没有提供任何参数时,它们将作用于整个矩阵。例如:max、sum和...
    99+
    2023-01-31
    数组 python
  • Android组件之服务的详解
    目录一、服务的概念二、Android的多线程编程2.1 线程的基本用法2.2 在子线程中更新UI更新方式一更新方式二2.3 解析异步消息处理机制2.4 使用AsyncTask三、服务...
    99+
    2024-04-02
  • Python之数组模块——array
    该模块定义了一个对象类型,可以表示一个基本值的数组:整数、浮点数、字符。 数组模块array的大部分属性及方法的应用: import array #array.array(typecode,[initializer])——typeco...
    99+
    2023-01-31
    数组 模块 Python
  • 【Python】算法之求组合
    原题:假设有a,b,c,1,2五个字符,请用任意一门语言求出这五个数的所有组合思考:1)这题字符有点多,我来变换一下,a、b、c三个字符,请用任意一门语言求这三个字符的组合(数学归纳法不是从最少识别规律开始么?)2)嗯,三个字符,肯定是三重...
    99+
    2023-06-02
  • python之列表、元组、字典
    1描述 打了激素的数组数组是只能存储同一数据类型的结构列表:可以存储多数数据类型的数组 2 定义列表: 元组和列表的不同:元组是不可变对象而列表是可变对象 3 列表的特性: 1 索引 分为前向索引、反向索引和多重索引 2...
    99+
    2023-01-31
    字典 列表 python
  • python学习之数组查找。
    import numpy as np def FindIndex(source, Destina): i = 0; for iterating_var in source: if(iterating_v...
    99+
    2023-01-31
    数组 python
  • Python 元组:理解序列之谜
    元组是 Python 中不可变有序序列。与列表类似,它们可以存储各种元素,但它们不能被修改或扩展。元组使用圆括号 () 定义,元素用逗号 , 分隔。 不可变性 元组的主要特征是它们的不可变性。这意味着一旦创建,就无法修改元组中的元素或其顺...
    99+
    2024-04-02
  • 以下哪些是云服务器ecs的产品组件组成要素之一
    云服务器ECS的产品组件主要包括: 负载均衡(Load Balance):负责将多个计算机上的请求分配到正确的服务器上进行处理,以提高网络性能。 多机房(Multi-Tenant):多个云服务器可共用一个物理主机,以实现高可用性和性能。...
    99+
    2023-10-27
    要素 组件 服务器
  • ITIL V3 服务运营篇 之 组织结构
      服务运营组织结构 •服务台 •技术管理 •IT运营控制 •应用管理 运营组织结构图   关于服务台的论述  服务台篇  这篇博文 技术管理 技术管理:提供技术和IT...
    99+
    2023-01-31
    组织结构 ITIL
  • Python自定义一个数组类,支持数组之
    class MyArray: '''保证输入的内容是整型、浮点型''' def ___isNumber(self, num): if not isinstance(num, (int,float)): ...
    99+
    2023-01-31
    数组 自定义 Python
  • 下列哪些不是云服务器ecs的产品组件组成要素之一
    云服务器ECS是一种虚拟服务器系统,它通常由以下组件组成: 集群管理器(Cluster Management Unit):Cluster Management Unit是云服务器ECS的基础组件,用于管理整个云服务器的资源,包括物理服务...
    99+
    2023-10-26
    要素 组件 服务器
  • Python四大金刚之元组详解
    目录引言 一、元组的创建方式二、元组的遍历总结引言  一、元组的创建方式 #第一种: t = ('python','No.1') print(t) pri...
    99+
    2024-04-02
  • 云服务器组成
    云服务器是一种虚拟服务器软件,用于为用户提供快速、可靠的云计算服务。它由多台物理服务器组成,这些服务器可以由用户自行管理。云服务器通常是由一些软件和工具组成的,这些工具可以提供以下功能: 负载均衡功能:云服务器可以通过软件来实现负载均衡...
    99+
    2023-10-26
    服务器
  • python基础学习之组织文件
    目录一、Shutil 模块1.1 复制文件和文件夹1.2 移动文件和文件夹1.3 删除文件和文件夹二、遍历文件三、压缩文件3.1 创建和添加ZIP文件3.2 读取ZIP文件3.3 解...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作