本文整理汇总了Python中nntplib.NNTP.head方法的典型用法代码示例。如果您正苦于以下问题:Python NNTP.head方法的具体用法?Python NNTP.head怎么用?Python NNTP.head使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nntplib.NNTP
的用法示例。
在下文中一共展示了NNTP.head方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: localtime
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
#coding=utf-8
#
from nntplib import NNTP
from time import strftime, time, localtime
day = 24 * 60 * 60 # Number of seconds in one day
yesterday = localtime(time() - day)
date = strftime('%y%m%d', yesterday)
hour = strftime('%H%M%S', yesterday)
servername = 'news.mixmin.net'
group = 'talk.euthanasia'
server = NNTP(servername)
ids = server.newnews(group, date, hour)[1]
for id in ids:
head = server.head(id)[3]
for line in head:
if line.lower().startswith('subject:'):
subject = line[9:]
break
body = server.body(id)[3]
print subject
print '-'*len(subject)
print '\n'.join(body)
server.quit()
示例2: NNTP
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
except:
servername = 'news.gmane.org'
groupname = 'gmane.comp.python.general' # cmd line args or defaults
showcount = 10 # show last showcount posts
# connect to nntp server
print 'Connecting to', servername, 'for', groupname
from nntplib import NNTP
connection = NNTP(servername)
(reply, count, first, last, name) = connection.group(groupname)
print '%s has %s articles: %s-%s' % (name, count, first, last)
# get request headers only
fetchfrom = str(int(last) - (showcount-1))
(reply, subjects) = connection.xhdr('subject', (fetchfrom + '-' + last))
# show headers, get message hdr+body
for (id, subj) in subjects: # [-showcount:] if fetch all hdrs
print 'Article %s [%s]' % (id, subj)
if not listonly and raw_input('=> Display?') in ['y', 'Y']:
reply, num, tid, list = connection.head(id)
for line in list:
for prefix in showhdrs:
if line[:len(prefix)] == prefix:
print line[:80]; break
if raw_input('=> Show body?') in ['y', 'Y']:
reply, num, tid, list = connection.body(id)
for line in list:
print line[:80]
print
print connection.quit( )
示例3: localtime
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
# mcadams.posc.mu.edu: alt.assassination.jfk
# news.php.net: php.dev
from nntplib import NNTP
from time import strftime, time, localtime
day = 24 * 60 * 60 # seconds of one day
yesterday = localtime(time() - day)
date = strftime('%y%m%d', yesterday)
hour = strftime('%H%M%S', yesterday)
servername = 'mcadams.posc.mu.edu'
group = 'alt.assassination.jfk'
server = NNTP(servername)
ids = server.newnews(group, date, hour)[1]
for id in ids:
head = server.head(id)[3] # 4th element
for line in head:
if line.lower().startswith('subject:'):
subject = line[9:]
break
body = server.body(id)[3]
print subject
print '-'*len(subject)
print '\n'.join(body)
server.quit()
示例4: Archive
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
class Archive(object):
@staticmethod
def is_diff(body):
return bool([line for line in body if line.startswith("diff ")])
def __init__(self, group, server):
self.conn = NNTP(server)
resp, count, first, last, name = self.conn.group(group)
self.group = group
self.server = server
self.first = int(first)
self.last = int(last)
def get_number_from_user(self, msg_id):
"""
Convert something the user might input into a message id.
These are:
# An NNTP message number
# A gmane link that includes the NNTP message number
# The original Message-Id header of the message.
NOTE: gmane's doesn't include the message number in STAT requests
that involve only the Message-Id (hence the convolution of getting
all the headers).
"""
msg_id = re.sub(r".*gmane.org/gmane.comp.version-control.git/([0-9]+).*", r"\1", str(msg_id))
_, n, id, result = self.conn.head(msg_id)
for header in result:
m = re.match(r"Xref: .*:([0-9]+)\s*$", header, re.I)
if m:
return int(m.group(1))
else:
raise FatalError("No (or bad) Xref header for message '%s'" % msg_id)
def get_patch_series(self, user_input, search_limit=100):
"""
Given an NNTP message number or a Message-Id header return
an mbox containing the patches introduced by the author of that message.
This handles the case where the threading is right *and* the patches
are numbered in a simple scheme:
[PATCH] this patch has no replies and stands on its own
[PATCH 0/2] this is an introduction to the series
|- [PATCH 1/2] the first commit
|- [PATCH 2/2] the second commit
[PATCH 1/3] this is the first commit
|- [PATCH 2/3] and this is the second
|- [PATCH 3/3] and this is the third
TODO: it would be nice to make the search more efficient, we can
use the numbers in [PATCH <foo>/<bar>] to stop early.
"""
start_id = self.get_number_from_user(user_input)
messages = limit(self.messages_starting_from(start_id), search_limit)
try:
thread = Thread(messages.next())
except StopIteration:
raise FatalError("No message at id '%s' using XOVER")
n_since_last = 0
for message in messages:
if n_since_last > 5:
break
elif thread.should_include(message):
n_since_last = 0
thread.append(message)
else:
n_since_last += 1
else:
raise FatalError('did not find end of series within %s messages', search_limit)
for message in self.xover(start_id - 5, start_id -1):
if thread.should_include(message):
thread.append(message)
return self.mboxify(thread)
def mboxify(self, thread):
"""
Convert a thread into an mbox for application via git-am.
"""
lines = []
for message in thread.in_order():
_, number, msg_id, body = self.conn.body(str(message.number))
# git-am doesn't like empty patches very much, and the 0/X'th patch is
# often not a patch, we skip it here. (TODO, warn the user about this)
#.........这里部分代码省略.........
示例5: triple
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
# stat
'Return a triple (response, number, id) where number is the article number and id is the message id.'
resp,num,msg_id = s.stat(last)
print(num,msg_id)
# article
'Return a tuple (response, info) where info is a namedtuple with three attributes number, message_id and lines (in that order).'
print('-'*10)
resp,info = s.article(last)
print(info.number,info.message_id,len(info.lines))
# head
'Same as article(), but sends a HEAD command. The lines returned (or written to file) will only contain the message headers, not the body.'
print('-'*10)
resp,info = s.head(last)
print(info.number,info.message_id,len(info.lines))
# body
'Same as article(), but sends a BODY command. The lines returned (or written to file) will only contain the message body, not the headers.'
print('-'*10)
resp,info = s.body(last)
print(info.number,info.message_id,len(info.lines))
# newgroups
'Return a pair (response, groups) where groups is a list of group names that are new since the given date and time'
#resp,groups = s.newgroups(date,time)
#print len(groups)
#pprint(groups)
#newnews
示例6: DownloadSpots
# 需要导入模块: from nntplib import NNTP [as 别名]
# 或者: from nntplib.NNTP import head [as 别名]
class DownloadSpots(object):
def __init__(self):
try:
self.news = NNTP(NEWS_SERVER, NNTP_PORT, NNTP_USERNAME,
NNTP_PASSWORD)
except NNTPTemporaryError as e:
raise SpotError('NNTP', e)
except socket.error as e:
raise SpotError('Connection', e)
self.conn = sqlite3.connect(NEWS_SERVER + '.db')
self.conn.row_factory = sqlite3.Row
self.cur = self.conn.cursor()
self.cur.executescript('''\
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
PRAGMA temp_store = MEMORY;
PRAGMA count_changes = OFF;
''')
def __del__(self):
print 'quit!'
if hasattr(self, 'news'):
self.news.quit()
if hasattr(self, 'cur'):
self.cur.close()
if hasattr(self, 'conn'):
self.conn.close()
def make_table(self):
sql = '''\
CREATE TABLE IF NOT EXISTS spots (
id int PRIMARY KEY,
full_id str,
cat int,
title str,
poster str,
date int,
size int,
erotiek int,
subcats str,
modulus int,
keyid int,
c_count int
);
CREATE TABLE IF NOT EXISTS comments (
id int PRIMARY KEY,
full_id str,
spot_full_id str
);
CREATE INDEX IF NOT EXISTS spots_full_id_index on spots(full_id);
'''
self.cur.executescript(sql)
self.conn.commit()
def download_detail(self, id):
'''Get information about a spot.
Args:
id (int/string): the nntp id of the spot. Can be:
- a short id: 123456
- a long id: '[email protected]'
Returns:
a dict with the following keys:
- nzb: a list of nntp id's of the nzb file
- image: the location of the image file: url or nntp id
- website: url of the website
- desc: a description of the spot
- title2: the title of the spot
(is called title2 to avoid a conflict)
'''
id = str(id)
self.news.group('free.pt')
print id
head = self.news.head(id)[-1]
xmltext = ''.join(item[7:] for item in head if
item.startswith('X-XML:'))
xml = etree.XML(xmltext)
xmldict = defaultdict(list)
xmldict['nzb'] = [i.text for i in xml.find('.//NZB').iter('Segment')]
imgfind = xml.find('.//Image')
if imgfind is not None:
if imgfind.find('.//Segment') is not None:
xmldict['image'] = imgfind.find('.//Segment').text
else:
xmldict['image'] = imgfind.text
else:
xmldict['image'] = None
webfind = xml.find('.//Website')
if webfind is not None:
xmldict['website'] = webfind.text
else:
xmldict['website'] = None
#.........这里部分代码省略.........