Python笔记 之 使用nntp模块阅读网络新闻

本文代码下载地址

Python代码

登录 news.gmane.io,查看新闻组 gmane.comp.python.committers 前十条信息,每次按下 y 键依次浏览题头和内容。

import nntplib

#默认设置
defaultsite = 'news.gmane.io'
defaultgroup = 'gmane.comp.python.committers'
defaultcount = 10

showhdrs = ['Path','From','Newsgroups','Subject','Date','Lines']

def reader(site=defaultsite,group=defaultgroup,newscount=defaultcount):
    #打印网站相关信息
    print('Connectiong to ',site,' for ',group)
    server = nntplib.NNTP(site)
    print('Version: ',server.nntp_version)
    print('Implementation: ',server.nntp_implementation)
    print(server.getwelcome())
    #获取新闻组信息
    (replay,count,first,last,name) = server.group(group)
    print('%s has %s articles,from %s to %s .'%(name,count,first,last))
    #请求题头
    fecthfrom = str(first)+'-'+str(newscount-1)
    (replay,subjects) = server.xhdr('subject',(fecthfrom))
    print('Reply: ',replay,'\n',
          'Subjects: ',subjects)
    #显示题头,获取消息的题头和主体
    for (id,subj) in subjects:
        print('Article %s [%s]'%(id,subj))
        if  input('Display?') in ('y','Y'):
            #print(server.head(id))
            #hand返回响应信息和文章信息
            #需要两次解读文件内容 lines
            #lines 为byte类型,需要解码
            (replay,articleinfo) = server.head(id)
            (num,id,lines) = articleinfo
            for line in lines:
                line = line.decode('utf-8')
                for prefix in showhdrs:
                    if line[:len(prefix)] == prefix:
                        print(line)
            if input('Show body?') in ('y','Y'):
                #print(server.body(id))
                # body返回响应信息和文章内容
                # 需要两次解读文件内容 lines
                # lines 为byte类型,需要解码
                (replay,articleinfo)=server.body(id)
                (num, id, lines) = articleinfo
                for line in lines:
                    print(line.decode('utf-8'))

    print(server.quit())

if __name__ == '__main__':
    reader()

运行结果

Connectiong to  news.gmane.io  for  gmane.comp.python.committers
Version:  2
Implementation:  INN 2.6.3
200 news.gmane.io InterNetNews NNRP server INN 2.6.3 ready (posting ok)
gmane.comp.python.committers has 7150 articles,from 1 to 7151 .
Reply:  221 Header or metadata information for subject follows (from overview) 
 Subjects:  [('1', 'Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('2', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('3', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('4', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('5', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.'), ('6', 'Re: [Python-Dev] next beta'), ('7', 'Re: [Python-Dev] next beta'), ('8', 'Re: [Python-Dev] next beta'), ('9', 'Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.')]
Article 1 [Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?y
Path: news.gmane.org!not-for-mail
From: Trent Nelson <tnelson@onresolve.com>
Newsgroups: gmane.comp.python.committers
Subject: Nominate Hirokazu Yamamoto (oceancity) for
Date: Mon, 11 Aug 2008 20:27:36 +0100
Lines: 5
Show body?y

What do people think about making Hirokazu Yamamoto a committer?  I rarely see any mailing list posts from him, but he sure makes up for it in terms of patches submitted to the issue tracker.  As far as I can tell (I've noticed his regular involvement via patches and whatnot for well over a year), he's a pretty switched on guy with a lot of Windows experience -- every time trunk fails to build with vc6/7/8, for example, he's got patches lined up.


        Trent.
Article 2 [Re: Nominate Hirokazu Yamamoto (oceancity) for commit privs.]
Display?
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在NSL-KDD数据集中,包含了许多字符串类型的特征,例如“protocol_type”,“service”,“flag”等等。这些字符串特征需要被转换成数字标签,方便后续的机器学习算法使用。下面是一个将NSL-KDD中的字符串特征转换成数字标签的示例代码: ```python import pandas as pd # 读取NSL-KDD数据集 data = pd.read_csv('kddcup.csv', header=None) # 将字符串特征转换成数字标签 service_mapping = {'aol': 1, 'auth': 2, 'bgp': 3, 'courier': 4, 'csnet_ns': 5, 'ctf': 6, 'daytime': 7, 'discard': 8, 'domain': 9, 'domain_u': 10, 'echo': 11, 'eco_i': 12, 'ecr_i': 13, 'efs': 14, 'exec': 15, 'finger': 16, 'ftp': 17, 'ftp_data': 18, 'gopher': 19, 'harvest': 20, 'hostnames': 21, 'http': 22, 'http_2784': 23, 'http_443': 24, 'http_8001': 25, 'imap4': 26, 'IRC': 27, 'iso_tsap': 28, 'klogin': 29, 'kshell': 30, 'ldap': 31, 'link': 32, 'login': 33, 'mtp': 34, 'name': 35, 'netbios_dgm': 36, 'netbios_ns': 37, 'netbios_ssn': 38, 'netstat': 39, 'nnsp': 40, 'nntp': 41, 'ntp_u': 42, 'other': 43, 'pm_dump': 44, 'pop_2': 45, 'pop_3': 46, 'printer': 47, 'private': 48, 'red_i': 49, 'remote_job': 50, 'rje': 51, 'shell': 52, 'smtp': 53, 'sql_net': 54, 'ssh': 55, 'sunrpc': 56, 'supdup': 57, 'systat': 58, 'telnet': 59, 'tftp_u': 60, 'tim_i': 61, 'time': 62, 'urh_i': 63, 'urp_i': 64, 'uucp': 65, 'uucp_path': 66, 'vmnet': 67, 'whois': 68, 'X11': 69, 'Z39_50': 70} data[1] = data[1].map(service_mapping) protocol_mapping = {'tcp': 1, 'udp': 2, 'icmp': 3} data[2] = data[2].map(protocol_mapping) flag_mapping = {'OTH': 1, 'REJ': 2, 'RSTO': 3, 'RSTOS0': 4, 'RSTR': 5, 'S0': 6, 'S1': 7, 'S2': 8, 'S3': 9, 'SF': 10, 'SH': 11} data[3] = data[3].map(flag_mapping) # 输出转换后的数据 print(data.head()) ``` 在上面的代码中,我们使用了字典(mapping)的方式将字符串特征转换成数字标签。例如,将“service”特征中的“ftp”转换成数字标签17。最终输出的数据是一个经过转换的数据集,其中字符串特征已经被转换成了数字标签。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值