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

a12321321321312321 2011-09-13 09:13:06

#!/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。。。
...全文
321 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
KiriIsomer 2011-09-27
  • 打赏
  • 举报
回复
会python不会C#的路过,,,这个脚本功能估计是检查txt里的网址们有没有被墙.
bent1ey 2011-09-27
  • 打赏
  • 举报
回复
200分都没大神
quruis2002 2011-09-21
  • 打赏
  • 举报
回复
python新手路过学习ing......
RockPine 2011-09-21
  • 打赏
  • 举报
回复
我刚开始学python,路过
孟子E章 2011-09-16
  • 打赏
  • 举报
回复
大致的过程

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
georgema 2011-09-15
  • 打赏
  • 举报
回复
把这段代码直接放到ironpython里编译,不就成.net的程序了嘛,为什么要去重写?
iambic 2011-09-14
  • 打赏
  • 举报
回复
python代码没什么难懂的。你先试着自己阅读吧。具体看不懂的地方再细问。别上来就贴一堆代码,自己看也不看就求解释,浪费别人的时间。
CCDDR 2011-09-14
  • 打赏
  • 举报
回复
200分,各种大神现身啊!
a12321321321312321 2011-09-13
  • 打赏
  • 举报
回复
额。。。搞.net的对python没几个人懂么??
zzxap 2011-09-13
  • 打赏
  • 举报
回复
读取一个文件,将其中的每行的域名读取出来放到一个数组,然后循环请求

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

不懂
q107770540 2011-09-13
  • 打赏
  • 举报
回复
不懂 帮顶
threenewbee 2011-09-13
  • 打赏
  • 举报
回复
这个代码貌似是选择代理的,被某种物质挡住的(你懂的),就走代理,否则直接访问。
孟子E章 2011-09-13
  • 打赏
  • 举报
回复
简单方法是 你可以参考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/

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧