专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Perl/Python

有懂Python的朋友没,帮忙解释下下面代码的意思。解决方案

发布时间:2011-06-29 20:11:06 文章来源:www.iduyao.cn 采编人员:星星草
有懂Python的朋友没,帮忙解释下下面代码的意思。
Python code

#!/usr/bin/env python
#    Aug 19 2011
#    Copyleft@2011 Published Under BSD Lisense
#            Ronald Liu
#    lzsdc01@gmail.com
#    FYI  http://lzsblog.appspot.com/%3Fp%3D291001
#
import sys,DNS,base64

def splitList(txt):
    arr = txt.split("\n")
    
    l = []
    for line in arr:
        if (not len(line)): #empty line
            continue
        if (line[0] == "!"): #Comment line
            continue
        elif(line[0:2] =="@@"):#Forbidding line
            continue
        elif(line.find("/")!=-1 or line.find("*")!=-1): #URL is ignored, only domains left
            continue
        
        #In this case, domain name is irrelevant to protocol(http or https)
        elif(line[0:2] =="||"):
            l.append(line[2:])
        elif(line[0] == "."):
            l.append(line[1:])
        else:
            l.append(line)
    
    return l
    
    

#Decode and decorate the input string
f = open("gfwlist.txt","r")
txt = f.read().replace("\n","")
txt = base64.decodestring(txt)

domains = splitList(txt)

#Set default DNS server
DNSServer=['8.8.8.8']
DNS.defaults['server']=DNSServer

DNS.DiscoverNameServers()

#These two varible are used to track the percentage of parsing process.
l = 0
a = len(domains)

for line in domains:
    request=DNS.Request()
    try:
        result=request.req(name=line,qtype="A")
    except DNS.Base.DNSError:
        msg=line+"\tTime Out\n"
    else:
        if not len(result.answers):
            msg=line+"\tNo record\n"
        else:
            msg=line+ "\tBingo\n"
            for i in result.answers:
                if (i["typename"]=='A'):    #Only A record is useful IP
                    print line + " " + i["data"]
    print >> sys.stderr, line + msg, l*1.0/a, "%"





RT。不知道上面程序做的什么功能?能不能改成.NET写?

请高手指教。。。。。3Q。。。

------解决方案--------------------
简单方法是 你可以参考Python的语法,
import sys,DNS,base64
是导入库
def 是定义函数

find()相对于String.IndexOf()方法
for line in arr:
是foreach循环

open是打开文件

request.req应该是HttpWebRequest之类的

print 就是Console.WriteLine等同

都是些基本的语法,查下帮助基本都能明白
http://docs.python.org/
------解决方案--------------------
这个代码貌似是选择代理的,被某种物质挡住的(你懂的),就走代理,否则直接访问。
------解决方案--------------------
不懂 帮顶
------解决方案--------------------
读取一个文件,将其中的每行的域名读取出来放到一个数组,然后循环请求

不过txt = f.read().replace("\n","")将换行符替换了,arr = txt.split("\n")还怎么分拆?

不懂
------解决方案--------------------
python代码没什么难懂的。你先试着自己阅读吧。具体看不懂的地方再细问。别上来就贴一堆代码,自己看也不看就求解释,浪费别人的时间。
------解决方案--------------------
貌似是翻墙的……
------解决方案--------------------
200分,各种大神现身啊!
------解决方案--------------------
把这段代码直接放到ironpython里编译,不就成.net的程序了嘛,为什么要去重写?
------解决方案--------------------
大致的过程

C# code
List<string> splitList(String txt)
  {
    String[] arr = txt.Split('\n');
    List<string> l = new List<string>();

    for (int i = 0; i < arr.Length; i++)
    {
      String line = arr[i];
      if (line.Trim().Length == 0)
      {
        continue;
      }
      if (line.Substring(0, 1) == "!")
      {
        continue;
      }
      else if (line.Substring(0, 2) == "@@")
      {
        continue;
      }
      else if (line.IndexOf("/") != -1 || line.IndexOf("*") != -1)
      {
        continue;
      }

      else if (line.Substring(0, 2) == "||")
      {
        l.Add(line.Substring(2));
      }
      else if (line.Substring(0, 1) == ".")
      {
        l.Add(line.Substring(1));
      }
      else
      {
        l.Add(line);
      }
    }
    return l;
  }



 StreamReader f = new StreamReader();
    String txt = File.ReadAllText(@"c:\gfwlist.txt").Replace("\n", "");
    byte[] encodedDataAsBytes = System.Convert.FromBase64String(txt);
    txt = System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);
    List<string> domains = splitList(txt);
    //下面对每一个地址进行查询,.NET好像没有现成的类,你可以参考
  http://www.codeproject.com/KB/IP/DNS_NET_Resolver.aspx
友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: