即时通讯新的编码和尝试学习,因为我去。pycurl无限循环和getopt问题
我试图创建一个python脚本,将抓取和打印所有的头文件中的所有网址在一个txt文件中的URL。
它似乎到达那里,但我陷入了一个无限循环与网址之一,我不知道为什么和由于某种原因,“-h或--help”不会返回usage()。任何帮助,将不胜感激。
下面是我迄今为止
#!/usr/bin/python
import pycurl
import cStringIO
import sys, getopt
buf = cStringIO.StringIO()
c = pycurl.Curl()
def usage():
print "-h --help, -i --urlist, -o --proxy"
sys.exit()
def main(argv):
iurlist = None
proxy = None
try:
opts, args = getopt.getopt(argv,"hi:o:t",["help", "iurlist=","proxy="])
if not opts:
print "No options supplied"
print "Type -h for help"
sys.exit()
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for opt, arg in opts:
if opt == ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-i", "--iurlist"):
iurlist = arg
elif opt in ("-o", "--proxy"):
proxy = arg
else:
assert False, "Unhandeled option"
with open(iurlist) as f:
iurlist = f.readlines()
print iurlist
try:
for i in iurlist:
c.setopt(c.URL, i)
c.setopt(c.PROXY, proxy)
c.setopt(c.HEADER, 1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.MAXREDIRS, 30)
c.setopt(c.USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0')
c.setopt(c.TIMEOUT, 8)
c.setopt(c.CONNECTTIMEOUT, 5)
c.setopt(c.NOBODY, 1)
c.setopt(c.PROXY, proxy)
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.SSL_VERIFYPEER, 0)
c.perform()
print buf.getvalue()
buf.close
except pycurl.error, error:
errno, errstr = error
print 'An error has occurred: ', errstr
if __name__ == "__main__":
main(sys.argv[1:])
这是最新的代码
#!/usr/bin/python
import pycurl
import cStringIO
import sys, getopt
c = pycurl.Curl()
def usage():
print "-h --help, -i --urlist, -o --proxy"
print "Example Usage: cURLdect.py -i urlist.txt -o http://192.168.1.64:8080"
sys.exit()
def main(argv):
iurlist = None
proxy = None
try:
opts, args = getopt.getopt(argv,"hi:o:t",["help", "iurlist=","proxy="])
if not opts:
print "No options supplied"
print "Type -h for help"
sys.exit()
except getopt.GetoptError as err:
print str(err)
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit()
elif opt in ("-i", "--iurlist"):
iurlist = arg
elif opt in ("-o", "--proxy"):
proxy = arg
else:
assert False, "Unhandeled option"
with open(iurlist) as f:
iurlist = f.readlines()
print iurlist
try:
for i in iurlist:
buf = cStringIO.StringIO()
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.PROXY, proxy)
c.setopt(c.HEADER, 1)
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.MAXREDIRS, 300)
c.setopt(c.USERAGENT, 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0')
c.setopt(c.TIMEOUT, 8)
c.setopt(c.CONNECTTIMEOUT, 5)
c.setopt(c.NOBODY, 1)
c.setopt(c.SSL_VERIFYPEER, 0)
c.setopt(c.URL, i)
c.perform()
print buf.getvalue()
buf.close()
except pycurl.error, error:
errno, errstr = error
print 'An error has occurred: ', errstr
if __name__ == "__main__":
main(sys.argv[1:])
我已经找出了一种方法来解决有关使用()的getopt问题。我对代码进行了如下更改:for opt,arg in opts: if opt ==“-h”: usage() sys。退出() elif opt in(“--help”): usage() sys.ext()' – LearningCode
您正在滥用buf。不带大括号的''buf.close'不会关闭它,返回一个函数。 – xbello
@xbello对不起,我该如何关闭它? – LearningCode