我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用pycurl.USERPWD。
def transfer(ipaddr, username, password, commandfile): #transfers commandfile to camera storage = StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://' + ipaddr + '/admin/remoteconfig') c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 5) c.setopt(c.TIMEOUT, TIMEOUT) filesize = os.path.getsize(commandfile) f = open(commandfile, 'rb') c.setopt(c.FAILONERROR, True) c.setopt(pycurl.POSTFIELDSIZE, filesize) c.setopt(pycurl.READFUNCTION, FileReader(f).read_callback) c.setopt(c.WRITEFUNCTION, storage.write) c.setopt(pycurl.HTTPHEADER, ["application/x-www-form-urlencoded"]) c.setopt(c.VERBOSE, VERBOSE) c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) c.setopt(pycurl.USERPWD, username + ':' + password) try: c.perform() except pycurl.error, error: errno, errstr = error print 'An error occurred: ', errstr return False, '' c.close() content = storage.getvalue() f.close() return True, content # *************************************************************** # *** Main program *** # ***************************************************************
def setInterface(self, options): interface, proxy, ipv6 = options["interface"], options["proxies"], options["ipv6"] if interface and interface.lower() != "none": self.c.setopt(pycurl.INTERFACE, str(interface)) if proxy: if proxy["type"] == "socks4": self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS4) elif proxy["type"] == "socks5": self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5) else: self.c.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP) self.c.setopt(pycurl.PROXY, str(proxy["address"])) self.c.setopt(pycurl.PROXYPORT, proxy["port"]) if proxy["username"]: self.c.setopt(pycurl.PROXYUSERPWD, str("%s:%s" % (proxy["username"], proxy["password"]))) if ipv6: self.c.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_WHATEVER) else: self.c.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4) if "auth" in options: self.c.setopt(pycurl.USERPWD, str(options["auth"])) if "timeout" in options: self.c.setopt(pycurl.LOW_SPEED_TIME, options["timeout"])
def transfer(ipaddr, username, password, commandfile, trackprogress): #transfers commandfile to camera storage = StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://' + ipaddr + '/admin/remoteconfig') c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 5) c.setopt(c.TIMEOUT, TIMEOUT) filesize = os.path.getsize(commandfile) f = open(commandfile, 'rb') c.setopt(c.FAILONERROR, True) c.setopt(pycurl.POSTFIELDSIZE, filesize) c.setopt(pycurl.READFUNCTION, FileReader(f).read_callback) if trackprogress: c.setopt(pycurl.NOPROGRESS, 0) c.setopt(pycurl.PROGRESSFUNCTION, progresscallback) starttime = time.time() else: c.setopt(pycurl.NOPROGRESS, 1) c.setopt(c.WRITEFUNCTION, storage.write) c.setopt(pycurl.HTTPHEADER, ["application/x-www-form-urlencoded"]) c.setopt(c.VERBOSE, VERBOSE) c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) c.setopt(pycurl.USERPWD, username + ':' + password) try: c.perform() except pycurl.error, error: errno, errstr = error print 'An error occurred: ', errstr return False, '' c.close() content = storage.getvalue() f.close() return True, content
def transfer(ipaddr, username, password, commandfile): #transfers commandfile to camera storage = StringIO() c = pycurl.Curl() c.setopt(c.URL, 'http://' + ipaddr + '/admin/remoteconfig') c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 5) c.setopt(c.TIMEOUT, 60) filesize = os.path.getsize(commandfile) f = open(commandfile, 'rb') c.setopt(c.FAILONERROR, True) c.setopt(pycurl.POSTFIELDSIZE, filesize) c.setopt(pycurl.READFUNCTION, FileReader(f).read_callback) c.setopt(c.WRITEFUNCTION, storage.write) c.setopt(pycurl.HTTPHEADER, ["application/x-www-form-urlencoded"]) c.setopt(c.VERBOSE, 0) c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) c.setopt(pycurl.USERPWD, username + ':' + password) try: c.perform() except pycurl.error, error: errno, errstr = error print 'An error occurred: ', errstr return False, '' c.close() content = storage.getvalue() f.close() return True, content # *************************************************************** # *** Main program *** # ***************************************************************
def Download_ETens_from_WA_FTP(output_folder, Lat_tiles, Lon_tiles): """ This function retrieves ETensV1.0 data for a given date from the ftp.wateraccounting.unesco-ihe.org server. Restrictions: The data and this python file may not be distributed to others without permission of the WA+ team. Keyword arguments: output_folder -- Directory of the outputs Lat_tiles -- [Lat_min, Lat_max] Tile number of the max and min latitude tile number Lon_tiles -- [Lon_min, Lon_max] Tile number of the max and min longitude tile number """ for v_tile in range(Lat_tiles[0], Lat_tiles[1]+1): for h_tile in range(Lon_tiles[0], Lon_tiles[1]+1): Tilename = "h%sv%s.zip" %(h_tile, v_tile) if not os.path.exists(os.path.join(output_folder,Tilename)): try: # Collect account and FTP information username, password = WebAccounts.Accounts(Type = 'FTP_WA') FTP_name = "ftp://ftp.wateraccounting.unesco-ihe.org//WaterAccounting_Guest/ETensV1.0/%s" % Tilename local_filename = os.path.join(output_folder, Tilename) # Download data from FTP curl = pycurl.Curl() curl.setopt(pycurl.URL, FTP_name) curl.setopt(pycurl.USERPWD, '%s:%s' %(username, password)) fp = open(local_filename, "wb") curl.setopt(pycurl.WRITEDATA, fp) curl.perform() curl.close() fp.close() except: print "tile %s is not found and will be replaced by NaN values" % Tilename return()
def set_interface(self, options): interface, proxy, ipv6 = options[ 'interface'], options['proxies'], options['ipv6'] if interface and interface.lower() != "none": self.setopt(pycurl.INTERFACE, interface) if proxy: if proxy['type'] == "socks4": self.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS4) elif proxy['type'] == "socks5": self.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_SOCKS5) else: self.setopt(pycurl.PROXYTYPE, pycurl.PROXYTYPE_HTTP) self.setopt(pycurl.PROXY, proxy['host']) self.setopt(pycurl.PROXYPORT, proxy['port']) if proxy['username']: userpwd = "{0}:{1}".format( proxy['username'], proxy['password']) self.setopt(pycurl.PROXYUSERPWD, userpwd) if ipv6: self.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_WHATEVER) else: self.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4) if "timeout" in options: self.setopt(pycurl.LOW_SPEED_TIME, options['timeout']) if "auth" in options: self.setopt(pycurl.USERPWD, self.options['auth'])
def curl_common_init(buf): handle = pycurl.Curl() handle.setopt(pycurl.WRITEDATA, buf) handle.setopt(pycurl.HEADERFUNCTION, curl_hdr) handle.setopt(pycurl.DEBUGFUNCTION, curl_debug) handle.setopt(pycurl.USERPWD, '{}:{}'.format(_g.conf._user,_g.conf._pass)) handle.setopt(pycurl.FOLLOWLOCATION, True) # avoid FTP CWD for fastest directory transversal handle.setopt(pycurl.FTP_FILEMETHOD, pycurl.FTPMETHOD_NOCWD) # we always set this flag and let the logging module # handle filtering. handle.setopt(pycurl.VERBOSE, True) # use ipv4 for VPNs handle.setopt(pycurl.IPRESOLVE, pycurl.IPRESOLVE_V4) handle.setopt(pycurl.USE_SSL, True) handle.setopt(pycurl.SSL_VERIFYPEER, False) # XXX handle.setopt(pycurl.SSL_VERIFYHOST, 0) return handle
def perform(self): self.__performHead="" self.__performBody="" conn=pycurl.Curl() conn.setopt(pycurl.SSL_VERIFYPEER,False) conn.setopt(pycurl.SSL_VERIFYHOST,1) conn.setopt(pycurl.URL,self.completeUrl) if self.__method or self.__userpass: if self.__method=="basic": conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_BASIC) elif self.__method=="ntlm": conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_NTLM) elif self.__method=="digest": conn.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST) conn.setopt(pycurl.USERPWD, self.__userpass) if self.__timeout: conn.setopt(pycurl.CONNECTTIMEOUT, self.__timeout) conn.setopt(pycurl.NOSIGNAL, 1) if self.__totaltimeout: conn.setopt(pycurl.TIMEOUT, self.__totaltimeout) conn.setopt(pycurl.NOSIGNAL, 1) conn.setopt(pycurl.WRITEFUNCTION, self.body_callback) conn.setopt(pycurl.HEADERFUNCTION, self.header_callback) if self.__proxy!=None: conn.setopt(pycurl.PROXY,self.__proxy) if self.__headers.has_key("Proxy-Connection"): del self.__headers["Proxy-Connection"] conn.setopt(pycurl.HTTPHEADER,self.__getHeaders()) if self.method=="POST": conn.setopt(pycurl.POSTFIELDS,self.__postdata) conn.perform() rp=Response() rp.parseResponse(self.__performHead) rp.addContent(self.__performBody) self.response=rp ######### ESTE conjunto de funciones no es necesario para el uso habitual de la clase
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
def set_request_context(self, url, get, post, referer, cookies, multipart=False): """ Sets everything needed for the request. """ url = safequote(url) if get: get = urlencode(get) url = "{0}?{1}".format(url, get) self.setopt(pycurl.URL, url) if post: self.setopt(pycurl.POST, 1) if not multipart: if isinstance(post, str): post = convert.to_bytes(post, post) elif not isinstance(post, bytes): post = safeurlencode(post) self.setopt(pycurl.POSTFIELDS, post) else: post = [(x, convert.to_bytes(y, y)) for x, y in post.items()] self.setopt(pycurl.HTTPPOST, post) else: self.setopt(pycurl.POST, 0) if referer and self.last_url: self.headers['Referer'] = str(self.last_url) else: self.headers['Referer'] = "" if cookies: for c in self.cj.output().splitlines(): self.setopt(pycurl.COOKIELIST, c) else: # Magic string that erases all cookies self.setopt(pycurl.COOKIELIST, 'ALL') # TODO: remove auth again if "auth" in self.options: self.setopt(pycurl.USERPWD, self.options['auth']) self.setopt(pycurl.HTTPHEADER, self.headers.list())