Python论坛  - 讨论区

标题:[python-chinese] 我实现一个基于数据库的新闻组服务器

2006年06月21日 星期三 17:30

neo openunix at 163.com
Wed Jun 21 17:30:31 HKT 2006

新建网页 1
Personal Amateur Radiostations of P.R.China
ZONE CQ24 ITU44 ShenZhen, China  
Hi All

遇到一些问题,现在头大,找不出问题所在.

我用telnet 127.0.0.1 119 测试LIST命令成功.但用outlook死活出不来列表.但有一次也不知为什么就成功了.现在又有问题了.

程序如下:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# Network News Transport Protocol Server Program

import sys
import socket
import threading
import time
import asyncore, asynchat
import string, StringIO
from netkiller import *

"""
class NNTPD(asyncore.dispatcher):
    #,threading.Thread threadname
    def __init__(self, host, port):
        asyncore.dispatcher.__init__ (self)
        #threading.Thread.__init__(self, name = threadname)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.there = (host, port)
        here = ('', port + 8000)
        self.bind (here)
        self.listen (5)
    def handle_accept (self):
        nntp_receiver (self, self.accept())
    def handle_close (self):
        self.close()
    
    def run(self):
        nntpd = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        try:
            nntpd.bind(('127.0.0.1', 119))
            nntpd.listen(5)
            while 1:
                mycom = commands('com')
                mycom.connect(nntpd)
                mycom.start()
                time.sleep(1)
        except socket.error, msg:
            print msg
            nntpd.close()
"""
class nntp_server (asyncore.dispatcher):
    
    def __init__ (self, host, port):
        asyncore.dispatcher.__init__ (self)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.there = (host, port)
        self.bind (self.there)
        self.listen (5)
    def handle_connect(self):
        pass
    def handle_accept (self):
        conn, addr = self.accept()
        nntp_receiver (self, (conn, addr))
    def handle_error(self):
        pass
    def handle_close (self):
        self.close()

class nntp_receiver (asynchat.async_chat):

    channel_counter = 0

    def __init__ (self, server, (conn, addr)):
        asynchat.async_chat.__init__ (self, conn)
        self.set_terminator ('\r\n')
        self.server = server
        #self.id = self.channel_counter
        self.channel_counter = self.channel_counter + 1
        #self.sender = nntp_sender (self, server.there)
        #self.sender.id = self.id
        self.buffer = ''
        self.handle_connect()
    def handle_connect (self):
        self.push(Messages.banner)
        
    def collect_incoming_data (self, data):
        self.buffer = self.buffer + data

    def found_terminator (self):
        data = self.buffer
        self.buffer = ''
        
        commands(self,data)
        #self.handle_close()
        #self.send (data + '\r\n')
        #self.push (data + '\r\n')
        #self.sender.push (data + '\n')

    def handle_close (self):
        print 'Closing'
        self.channel_counter = self.channel_counter - 1
        #self.sender.close()
        self.close()     
"""           
class nntp_sender (asynchat.async_chat):

    def __init__ (self, receiver, address):
        asynchat.async_chat.__init__ (self)
        self.receiver = receiver
        self.set_terminator (None)
        self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
        self.buffer = ''
        self.set_terminator ('\n')
        self.connect (address)
        
    def handle_connect (self):
        print 'Connected'

    def collect_incoming_data (self, data):
        self.buffer = self.buffer + data

    def found_terminator (self):
        data = self.buffer
        self.buffer = ''
        print '==> (%d) %s' % (self.id, repr(data))
        self.receiver.push (data + '\n')

    def handle_close (self):
        self.receiver.close()
        self.close()            
"""
        
class commands:
    #threading.Thread
    conn = None
    addr = None
    buffer = None
    msg = None
    conn = None
    HELP = """
    HELP
    100 Legal commands
      authinfo user Name|pass Password
      article [MessageID|Number]
      body [MessageID|Number]
      check MessageID
      date
      group newsgroup
      head [MessageID|Number]
      help
      ihave
      last
      list [active|active.times|newsgroups|subscriptions]
      listgroup newsgroup
      mode stream
      mode reader
      newgroups yymmdd hhmmss [GMT] []
      newnews newsgroups yymmdd hhmmss [GMT] []
      next
      post
      slave
      stat [MessageID|Number]
      takethis MessageID
      xgtitle [group_pattern]
      xhdr header [range|MessageID]
      xover [range]
      xpat header range|MessageID pat [morepat...]
    .
    """
    command = ('LIST','MODE READER','GROUP','XOVER')
    #def __init__(self, threadname):
        #threading.Thread.__init__(self, name = threadname)
    def __init__(self, conn,buffer):
        self.conn = conn
        self.buffer = buffer
        self.msg = Messages()
        print '<== (%d) %s' % (conn.channel_counter, repr(buffer))
        self.nntp(self.buffer)

    def welcome(self):
        self.conn.send("200 \"Welcome to Netkiller News server\"\r\n")
    def nntp(self, buffer):
        if not buffer: return True
        if buffer[:11] == 'MODE READER':
            self.conn.send("200 \"Welcome to NewsFan News server\"\r\n")
            #break
        elif buffer[:4] == 'LIST':
            lists = self.msg.list()
            length = str(len(lists))
            print length
            self.conn.send(length + " list of newsgroups follows\r\n")
            for group in lists:
                self.conn.send(group+"\r\n")
            self.conn.send(".\r\n")
        elif buffer[:5] == 'GROUP':
            name = buffer[6:]
            #last, first = self.msg.group(name)
            self.conn.send("211 " + self.msg.group(name) + " "+name+" selected\r\n")
        elif buffer[:9] == 'NEWGROUPS':
            newgroups = self.msg.newgroups(950803,192708,'GMT')
            length = len(newgroups)
            self.conn.send(str(length)+ " New newsgroups follow.\r\n")
            for name in newgroups:
                self.conn.send(name+"\r\n")
            self.conn.send(".\r\n")
            #break            
            
        elif self.buffer[:5] == 'XOVER':
            xover = buffer[6:-2]
            xover_list = self.msg.xover(10,10)
            length = len(xover_list)
            self.conn.send("244 xover follows\r\n")
            for xo in xover_list:
                self.conn.send( xo + "\r\n")
            self.conn.send(".\r\n")
        elif buffer[:4] =='XHDR':
            subject = buffer[5:-2]
            #self.msg.xhdr('subject',first,last)
            xover_list = self.msg.xover(10,10)
            #length = len(xover_list)
            self.conn.send(" "+subject+" data follows\r\n")
            for xo in xover_list:
                self.conn.send( xo + "\r\n")
            self.conn.send(".\r\n")
        elif buffer[:4] == 'post': 
            text = ""
            self.conn.send("354 Start mail input; end with ;.;\r\n")
            while True:
                buffer = self.conn.recv(1024)
                text = text + self.buffer
                if buffer[-3:] == ".\r\n":
                    self.conn.send("250 OK\r\n")
                    break
            self.buffer = text
            print self.buffer
            #break
        elif buffer[:4] == 'RSET':
            self.conn.send("250 OK\r\n")
            #break
        elif buffer[:4] =='HELP':
            self.conn.send(self.HELP)
        elif buffer[:4] =='QUIT':
            self.conn.send("221 Closing connection. Good bye.\r\n")
            return True
        else:
            self.conn.send("500 command not recognized\r\n")
        return False
  
    def close(self):
        self.conn.close()
        self.buffer = None
        self.conn = None
        self.addr = None

class Messages:
    banner = '200 \"Welcome to Netkiler News server\"\r\n'
    subject = None
    mail_from = None
    rcpt_to = None
    data = None

    group = ""

    grouplist = (
        ("cn.comp.linux", 2, 1, "y"),
        ("cn.comp.freebsd", 3, 1, "y"),
        ("cn.comp.dos", 10, 4, "y"),
        ("cn.test", 2, 1, "y"),
        ("comp.lang.python", 5, 2, "y")
        )
    def welcome(self):
        return 'Welcome'
    def list (self):
        lists = []
        for name, last, first, mode in self.grouplist:
            lists.append(name+ " " + str(last) + " " + str(first) +" "+ mode) 
        return lists
    def group(self,groupname):
        group_rang = ""
        for name, last, first, mode in self.grouplist:
            if name == groupname:
                group_rang = str(last - first)+" "+ str(last)+" "+str(first)
                break
        return group_rang
    def xover(self,first,last):
        xover_tmp = []
        xover_tmp.append('4       �?...............重新抢第�?     "温柔�?�?" <wryd at newsfan.net>   27 Nov 2004 18:08:00 +0800      <41a85200$1_1 at News.Newsfan.NET>         920     8     Xref: NewsFanServer2 计算�?.网络.网管:4 计算�?.网络.系统集成:3')
        xover_tmp.append('5       这样太影响人�?  "月风" <cgsyy at 126.com>  27 Nov 2004 19:23:18 +0800<41a863a6_3 at News.Newsfan.NET>           653     3       Xref: NewsFanServer2 计算�?.网络.网管:5')
        return xover_tmp
    def head(self):
        self.xover(first,last)
    def xhdr(self,first,last):
        xover_tmp = []
        xover_tmp.append('1       HI           "NNTP.HK" <admin at nntp.hk>               14 Jun 2006 14:43:05 +0800      <448faff9$1 at news.nntp.hk>               1151    31      Xref: news.nntp.hk vip.cicefans:1\r\n')
        xover_tmp.append('2       Chen�?         "CiceFans" <CiceFans at yahoo.com.hk>      14 Jun 2006 19:50:32 +0800      <448ff808$1 at news.nntp.hk>               994     4       Xref: news.nntp.hk vip.cicefans:2\r\n')
        xover_tmp.append('3       Re: Neo�?     "CiceFans" <CiceFans at yahoo.com.hk>      14 Jun 2006 19:53:56 +0800      <448ff8d4$1 at news.nntp.hk>       <448ff808$1 at news.nntp.hk>       1329    16      Xref: news.nntp.hk vip.cicefans:3\r\n')
        xover_tmp.append('4       Re: Chen�?     "CiceFans" <CiceFans at yahoo.com.hk>      14 Jun 2006 19:55:19 +0800      <448ff927$1 at news.nntp.hk>       <448ff808$1 at news.nntp.hk>       1329    16      Xref: news.nntp.hk vip.cicefans:4\r\n')
        return xover_tmp
    def newgroups(self,data,time,gmt):
        lists = []
        lists.append("cn.test.os")
        lists.append("cn.test.qa")
        return lists

             
def main():
    try:
        nntpd = nntp_server('127.0.0.1',119)
        asyncore.loop()
    except KeyboardInterrupt:
        print "Crtl+C pressed. Shutting down."


if __name__ == '__main__':
    main()
    
    





Neo Chan (netkiller)
Best Regards, VY 73! DE BG7NYT
http://netkiller.hikz.com/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 2959 bytes
Desc: not available
Url : http://lists.exoweb.net/pipermail/python-chinese/attachments/20060621/514c952b/smime-0001.bin

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年06月21日 星期三 17:42

openunix openunix at 163.com
Wed Jun 21 17:42:08 HKT 2006

telnet 127.0.0.1 119 测试

200 "Welcome to Netkiler News server"
LIST
5 list of newsgroups follows
cn.comp.linux 2 1 y
cn.comp.freebsd 3 1 y
cn.comp.dos 10 4 y
cn.test 2 1 y
comp.lang.python 5 2 y
.

Sniffer 看过Outlook访问其它新闻组。也是这样的操作啊。。:(
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060621/9e0c0cdc/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年06月21日 星期三 17:59

Gavin gavin at sz.net.cn
Wed Jun 21 17:59:15 HKT 2006

neo又换名openunix了:)
感觉失踪了很久:)



----- Original Message ----- 
  发件人: openunix 
  收件人: neo ; python-chinese 
  发送时间: 2006年6月21日 17:42
  主题: Re: [python-chinese]我实现一个基于数据库的新闻组服务器


  telnet 127.0.0.1 119 测试

  200 "Welcome to Netkiler News server"
  LIST
  5 list of newsgroups follows
  cn.comp.linux 2 1 y
  cn.comp.freebsd 3 1 y
  cn.comp.dos 10 4 y
  cn.test 2 1 y
  comp.lang.python 5 2 y
  .

  Sniffer 看过Outlook访问其它新闻组。也是这样的操作啊。。:(







  您 想 拥 有 abc at 188.com 邮 箱 吗 ? 
  创 纪 录 16G 超 大 容 量(送 6G 免 费 网 盘),最 高 性 价 比 的 电 子 邮 件 解 决 之 道 


------------------------------------------------------------------------------


  _______________________________________________
  python-chinese
  Post: send python-chinese at lists.python.cn
  Subscribe: send subscribe to python-chinese-request at lists.python.cn
  Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
  Detail Info: http://python.cn/mailman/listinfo/python-chinese
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060621/cdf403f3/attachment.htm

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2006年06月21日 星期三 18:22

openunix openunix at 163.com
Wed Jun 21 18:22:29 HKT 2006

 哈哈..openunix是我邮箱名字.
neo是ename
netkiller 是nickname
bg7nyt 是 callsign

都是我,这几天搞socket,thread呢..web开发没$$$涂...想能不能改改行.:(  
-----原始邮件-----
发件人:"Gavin" 
发送时间:2006-06-21 17:59:15
收件人:"" 
抄送:(无)
主题:Re: [python-chinese]我实现一个基于数据库的新闻组服务器


       
-----原始邮件-----
发件人:"Gavin" 
发送时间:2006-06-21 17:59:15
收件人:"" 
抄送:(无)
主题:Re: [python-chinese]我实现一个基于数据库的新闻组服务器


null
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.exoweb.net/pipermail/python-chinese/attachments/20060621/17c7ee25/attachment.html

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号