Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pycurl无限循环与getopt问题_Python_Getopt_Pycurl - Fatal编程技术网

Python pycurl无限循环与getopt问题

Python pycurl无限循环与getopt问题,python,getopt,pycurl,Python,Getopt,Pycurl,我是新的编码和尝试学习,因为我去 我正在尝试创建一个python脚本,它将捕获并打印txt文件中URL列表中的所有标题 它似乎正在到达那里,但我陷入了一个无限循环,其中一个URL,我不知道为什么“-h”或“-help”不会返回用法()。任何帮助都将不胜感激 以下是我目前的情况: #!/usr/bin/python import pycurl import cStringIO import sys, getopt buf = cStringIO.StringIO() c = pyc

我是新的编码和尝试学习,因为我去

我正在尝试创建一个python脚本,它将捕获并打印txt文件中URL列表中的所有标题

它似乎正在到达那里,但我陷入了一个无限循环,其中一个URL,我不知道为什么“-h”或“-help”不会返回
用法()
。任何帮助都将不胜感激

以下是我目前的情况:

 #!/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:])
你正在使用

如果opt==(“-h”,“-help”):

对于帮助选项,但是

如果在中选择(..)

对于所有其他选项。
opt
要么是
-h
要么是
-help
,但不是两者都是,因此您需要在
中使用
,检查
opt
是否也是其中之一。

如果您正在学习,pycurl可能不是最佳选择。他们说你熟悉libcurl库。发件人:

PycURL面向高级开发人员-如果您需要数十个并发、快速和可靠的连接或上面列出的任何复杂功能,那么PycURL适合您

PycURL的主要缺点是,它是libcurl上相对较薄的一层,没有任何好的Pythonic类层次结构。这意味着它的学习曲线有些陡峭,除非您已经熟悉libcurl的capi

这是他们如何进行多重提取的:


要使用la python获取标题,请安装
requests
库,然后执行以下操作:

for url in list_of_urls:
    r = requests.get(url)
    print r.headers

要处理命令行参数,请使用python附带的电池中的
argparser

我已经找到了一种方法来解决有关用法()的getopt问题。我对opt中的opt、arg做了以下代码更改:if opt==“-h”:usage()sys.exit()elif opt-in(“--help”):usage()sys.ext()
您误用了
buf
buf.close
不带大括号不能关闭它,返回一个函数。@xbello抱歉,我应该如何关闭它?使用
buf.close()
。但是要小心,因为你在循环外打开它,在循环内关闭它。在
c.setop(c.URL,i)
@xbello确定之前打开缓冲区,确认打开的是c.setopt(c.WRITEFUNCTION,buf.write)行吗?谢谢将等待其他问题的答案并相应地选择答案