python类TIMEOUT的实例源码

http_client.py 文件源码 项目:oriskami-python 作者: oriskami 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def request(self, method, url, headers, post_data=None):
        s = util.StringIO.StringIO()
        rheaders = util.StringIO.StringIO()
        curl = pycurl.Curl()

        proxy = self._get_proxy(url)
        if proxy:
            if proxy.hostname:
                curl.setopt(pycurl.PROXY, proxy.hostname)
            if proxy.port:
                curl.setopt(pycurl.PROXYPORT, proxy.port)
            if proxy.username or proxy.password:
                curl.setopt(
                    pycurl.PROXYUSERPWD,
                    "%s:%s" % (proxy.username, proxy.password))

        if method == 'get':
            curl.setopt(pycurl.HTTPGET, 1)
        elif method == 'post':
            curl.setopt(pycurl.POST, 1)
            curl.setopt(pycurl.POSTFIELDS, post_data)
        else:
            curl.setopt(pycurl.CUSTOMREQUEST, method.upper())

        # pycurl doesn't like unicode URLs
        curl.setopt(pycurl.URL, util.utf8(url))

        curl.setopt(pycurl.WRITEFUNCTION, s.write)
        curl.setopt(pycurl.HEADERFUNCTION, rheaders.write)
        curl.setopt(pycurl.NOSIGNAL, 1)
        curl.setopt(pycurl.CONNECTTIMEOUT, 30)
        curl.setopt(pycurl.TIMEOUT, 80)
        curl.setopt(pycurl.HTTPHEADER, ['%s: %s' % (k, v)
                                        for k, v in headers.items()])
        if self._verify_ssl_certs:
            curl.setopt(pycurl.CAINFO, os.path.join(
                os.path.dirname(__file__), 'data/ca-certificates.crt'))
        else:
            curl.setopt(pycurl.SSL_VERIFYHOST, False)

        try:
            curl.perform()
        except pycurl.error as e:
            self._handle_request_error(e)
        rbody = s.getvalue()
        rcode = curl.getinfo(pycurl.RESPONSE_CODE)

        return rbody, rcode, self.parse_headers(rheaders.getvalue())
Request.py 文件源码 项目:wfuzz 作者: gwen001 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def to_pycurl_object(c, req):

        c.setopt(pycurl.MAXREDIRS, 5)

        c.setopt(pycurl.WRITEFUNCTION, req.body_callback)
        c.setopt(pycurl.HEADERFUNCTION, req.header_callback)

        c.setopt(pycurl.NOSIGNAL, 1)
        c.setopt(pycurl.SSL_VERIFYPEER, False)
        c.setopt(pycurl.SSL_VERIFYHOST, 0)

        c.setopt(pycurl.URL,req.completeUrl)

        if req.getConnTimeout():
        c.setopt(pycurl.CONNECTTIMEOUT, req.getConnTimeout())

        if req.getTotalTimeout():
        c.setopt(pycurl.TIMEOUT, req.getTotalTimeout())


        authMethod, userpass = req.getAuth()
        if authMethod or userpass:
        if authMethod == "basic":
            c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC)
        elif authMethod == "ntlm":
            c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NTLM)
        elif authMethod == "digest":
            c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
        c.setopt(pycurl.USERPWD, userpass)

        c.setopt(pycurl.HTTPHEADER, req.getHeaders())
        if req.method == "POST":
        c.setopt(pycurl.POSTFIELDS, req.postdata)

        if req.method != "GET" and req.method != "POST":
        c.setopt(pycurl.CUSTOMREQUEST, req.method)
        if req.method == "HEAD":
        c.setopt(pycurl.NOBODY, True)

        if req.followLocation:
        c.setopt(pycurl.FOLLOWLOCATION, 1)

        proxy = req.getProxy()
        if proxy != None:
            c.setopt(pycurl.PROXY, proxy)
            if req.proxytype=="SOCKS5":
                c.setopt(pycurl.PROXYTYPE,pycurl.PROXYTYPE_SOCKS5)
            elif req.proxytype=="SOCKS4":
                c.setopt(pycurl.PROXYTYPE,pycurl.PROXYTYPE_SOCKS4)
            req.delHeader("Proxy-Connection")

        return c
websim.py 文件源码 项目:hyperbolic-caching 作者: kantai 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def work_driver(arg_tup):
    global PROGRESS_CTR_T, MEASURING
    global REQ_ARRS
    global COST_ARRS
    nreqs, host_str, req_arr_ix, send_cost = arg_tup
    req_arr = REQ_ARRS[req_arr_ix]
    cost_arr = COST_ARRS[req_arr_ix]

    success = 0
    miss = 0

    NCURL = 128
    curl_hands = []
    for i in range(NCURL):
        c = pycurl.Curl()
        curl_hands.append(c)
    n = 0
    for ix, item in enumerate(req_arr):
        c = curl_hands[ix % NCURL]
        try:
            if send_cost:
                url = host_str % (item, cost_arr[ix])
            else:
                url = host_str % item
            c.setopt(c.URL, url)
            resp = StringIO()
            headers = StringIO()
            c.setopt(c.WRITEFUNCTION, resp.write)
            c.setopt(c.HEADERFUNCTION, headers.write)
            c.setopt(pycurl.CONNECTTIMEOUT, 20)
            c.setopt(pycurl.TIMEOUT, 20)
            c.perform()

            if c.getinfo(c.RESPONSE_CODE) == 200:
                success += 1
                is_hit = handle_response(resp, headers)
                if not is_hit:
                    miss += 1

        except Exception as e:
            pass

        PROGRESS_CTR_T[req_arr_ix] = ix

    return (success, miss)
websim_plus_classes.py 文件源码 项目:hyperbolic-caching 作者: kantai 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def work_driver(arg_tup):
    global PROGRESS_CTR_T, MEASURING
    global REQ_ARRS, COST_ARRS, CLASS_ARRS
    nreqs, host_str, req_arr_ix = arg_tup
    req_arr = REQ_ARRS[req_arr_ix]
    cost_arr = COST_ARRS[req_arr_ix]
    class_arr = CLASS_ARRS[req_arr_ix]

    success = 0
    miss = 0
    costs = 0

    NCURL = 128
    curl_hands = []
    for i in range(NCURL):
        c = pycurl.Curl()
        curl_hands.append(c)
    n = 0

#    my_output = open(TMP_FILE_NAME % req_arr_ix, 'w')

    for ix, item in enumerate(req_arr):
        c = curl_hands[ix % NCURL]
        try:
            url = host_str % (item, cost_arr[ix], class_arr[ix])
            t_s = time.time()

            c.setopt(c.URL, url)
            resp = StringIO()
            headers = StringIO()
            c.setopt(c.WRITEFUNCTION, resp.write)
            c.setopt(c.HEADERFUNCTION, headers.write)
            c.setopt(pycurl.CONNECTTIMEOUT, 20)
            c.setopt(pycurl.TIMEOUT, 20)
            c.perform()
            t_end = time.time()

            if c.getinfo(c.RESPONSE_CODE) == 200:
                success += 1
                is_hit, cost = handle_response(resp, headers)
                if not is_hit:
                    miss += 1
                    costs += cost
 #               my_output.write("%f, %f\n" % (t_end, (t_end - t_s)))
 #               my_output.flush()

        except Exception as e:
            print e
            pass

        PROGRESS_CTR_T[req_arr_ix] = success

#    my_output.close()
    return (success, miss, costs)
post.py 文件源码 项目:PyBiu 作者: chiaki64 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def post_file_curl(path, key, token):
    c = pycurl.Curl()
    c.setopt(c.POST, 1)
    # if path[0] == "\"":
    path = path[1:-1]

    if os.path.exists(path):
        suffix = os.path.splitext(path)[1]
        # A fucking dirty hack - rename file
        while True:
            number = random.randint(10, 100000)
            if not os.path.exists(os.path.split(path)[0] + "/" + str(number) + suffix):
                newpath = os.path.split(path)[0] + "/" + str(number) + suffix
                break
        os.rename(path, newpath)
        print("rename" + newpath)

    bak_path = newpath
    print(path)
    fields = [('file', (c.FORM_FILE, newpath.encode('gbk'))),
              ('token', token),
              ('key', key),
              ('x:md5', key)]
    c.setopt(c.VERBOSE, 1)
    c.setopt(c.URL, "http://upload.qiniu.com/")
    c.setopt(c.HTTPPOST,  fields)
    c.setopt(c.NOPROGRESS, 0)
    c.setopt(c.PROGRESSFUNCTION, progress)
    c.setopt(pycurl.CONNECTTIMEOUT, 60)
    c.setopt(pycurl.TIMEOUT, 600)
    try:
        info = c.perform()
        print(info)
        print(fields)
        if c.getinfo(c.HTTP_CODE) == 200:
            os.rename(newpath, path)
            print("rename" + path)
            return True
    except pycurl.error as e:
        print(e)
        sys.stdout.write("File no Found!")
        return False
    if os.path.exists(newpath):
        os.rename(newpath, path)
        print("rename" + path)

    c.close()
    return False


问题


面经


文章

微信
公众号

扫码关注公众号