当前位置: 首页>>代码示例>>Python>>正文


Python URL.openConnection方法代码示例

本文整理汇总了Python中java.net.URL.openConnection方法的典型用法代码示例。如果您正苦于以下问题:Python URL.openConnection方法的具体用法?Python URL.openConnection怎么用?Python URL.openConnection使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.net.URL的用法示例。


在下文中一共展示了URL.openConnection方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: readWebPage

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def readWebPage( webpageURL ):
    webpageURL = webpageURL.strip()
    log(VERBOSE_, "readWebpage webpageURL=" + webpageURL)
    url = URL(webpageURL)
    conn = url.openConnection()
    conn.setConnectTimeout(30000)
    conn.setReadTimeout(10000)
    conn.connect()
    responseCode = conn.getResponseCode()
    cookie = conn.getHeaderField("Set-Cookie")
    cookiePath = None
    pathDiscr = " Path="
    if cookie and cookie.find(pathDiscr) > 0:
        cookiePath = cookie[cookie.index(pathDiscr) + len(pathDiscr):]
    respLines = []
    if responseCode >= 400:
        log(ERROR_, "HTTP ERROR " + `responseCode` + ": " + `conn.getResponseMessage()`)
    else:
        log(VERBOSE_, "WebPageResponse status=" + `responseCode` + " reason=" + `conn.getResponseMessage()`)
        #log(DEBUG_,"WebPageResponse resp="+`resp` )
        reader = BufferedReader(InputStreamReader(conn.getInputStream()))
        inputLine = reader.readLine()
        while inputLine is not None:
            respLines.append(inputLine)
            inputLine = reader.readLine()

        reader.close()
    return respLines, cookiePath
开发者ID:ammula88,项目名称:WebsphereConfigLib,代码行数:30,代码来源:application.py

示例2: post

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def post(targetURL, params, contentType="text/xml", username=None):
    
    if(type(params) is dict):
        paramStr = ""
        for aKey in params.keys():
            paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&"
        paramStr=paramStr[:-1]
    else:
        paramStr = params
        
    url = URL(targetURL)
    print targetURL
    print paramStr
    print contentType
    connection = url.openConnection()
    if username!=None:    
        userpass = username
        basicAuth = "Basic " + base64.b64encode(userpass);
        connection.setRequestProperty ("Authorization", basicAuth);
    connection.setRequestMethod("POST")
    if contentType != None:
        connection.setRequestProperty("Content-Type", contentType)
    connection.setRequestProperty("Content-Length", str(len(paramStr)))
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoInput(1)
    connection.setDoOutput(2)
    
    wr= DataOutputStream(connection.getOutputStream())
    wr.writeBytes(paramStr)
    wr.flush()
    wr.close()
    return getResponse(connection);
开发者ID:revolutionarysystems,项目名称:o-test,代码行数:35,代码来源:HttpCall.py

示例3: get

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def get(targetURL, params, username=None):

    paramStr = ""
    for aKey in params.keys():
        paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&"
    paramStr=paramStr[:-1]
    url = URL(targetURL+"?"+paramStr)
    print url
    connection = url.openConnection()

    if username!=None:    
        userpass = username
        basicAuth = "Basic " + base64.b64encode(userpass);
        #print basicAuth
        connection.setRequestProperty ("Authorization", basicAuth);
    
    connection.setRequestMethod("GET")    
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoOutput(2)
    
    inStream= connection.getInputStream()
    rd= BufferedReader(InputStreamReader(inStream))
    response = ""
    line = rd.readLine()
    while line != None:
        response +=line+"\r"
        line = rd.readLine()
    rd.close()
    return response
开发者ID:revolutionarysystems,项目名称:common-test,代码行数:32,代码来源:HttpCall.py

示例4: delete

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def delete(targetURL, params):
    url = URL(targetURL)
    connection = url.openConnection()
    connection.setRequestMethod("DELETE")    
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoOutput(2)
    return getResponse(connection);
开发者ID:revolutionarysystems,项目名称:o-test,代码行数:10,代码来源:HttpCall.py

示例5: doService

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def doService(httpMethod, url, credential, requestBody=None):
    
    Security.addProvider(MySSLProvider())
    Security.setProperty("ssl.TrustManagerFactory.algorithm", "TrustAllCertificates")
    HttpsURLConnection.setDefaultHostnameVerifier(MyHostnameVerifier())
    
    urlObj = URL(url)
    con = urlObj.openConnection()
    con.setRequestProperty("Accept", "application/xml")
    con.setRequestProperty("Content-Type", "application/xml")
    con.setRequestProperty("Authorization", credential)
    con.setDoInput(True);
    
    if httpMethod == 'POST':
        con.setDoOutput(True)
        con.setRequestMethod(httpMethod)
        output = DataOutputStream(con.getOutputStream()); 
        if requestBody:
            output.writeBytes(requestBody); 
        output.close();
        
    responseCode = con.getResponseCode()
    logger.info('response code: ' + str(responseCode))
    responseMessage = con.getResponseMessage()
    logger.info('response message: ' + str(responseMessage))
    contentLength = con.getHeaderField('Content-Length')
    logger.info('content length: ' + str(contentLength))        
    
    stream = None
    if responseCode == 200 or responseCode == 201 or responseCode == 202:
        stream = con.getInputStream()
    elif contentLength:
        stream = con.getErrorStream()
        
    if stream:
        dataString = getStreamData(stream)
        logger.info(httpMethod + ' url: ' + url)
        if not url.endswith('.xsd') and len(dataString) < 4096: 
            xmlStr = Util.prettfyXmlByString(dataString)
            logger.info(httpMethod + ' result: \n\n' + xmlStr)
        else:
            logger.info('response body too big, no print out')
        if responseCode == 200 or responseCode == 201 or responseCode == 202:
            return dataString
        else:
            ''' to mark the case failed if response code is not 200-202 '''
            return None
    else:
        logger.error('')
        logger.error('---------------------------------------------------------------------------------------------------')
        logger.error('-------->>>  Input or Error stream is None, it may be a defect if it is positive test case')
        logger.error('---------------------------------------------------------------------------------------------------')
        logger.error('')
        return None
开发者ID:huhe56,项目名称:nsm-rest-api,代码行数:56,代码来源:HttpUtil.py

示例6: new_urlopen

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
 def new_urlopen(req):
     if isinstance(req, str):
         full_url = req
     else:
         full_url = req.get_full_url()
     if full_url.startswith('https'):
         u = URL(full_url)
         conn = u.openConnection()
         return ConnReader(conn)
     else:
         org_urlopen(req)
开发者ID:alexeysirenko,项目名称:grain,代码行数:13,代码来源:ipc.py

示例7: reportToToolServer

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
 def reportToToolServer(self, url):
     """Tell to the tool server when an error is fixed (user clicked
        on correctedBtn) or false positive (falsePositiveBtn).
        Tool instance must have either:
        self.fixedFeedbackMode = "url"
        self.falseFeedbackMode = "url"
     """
     try:
         url = URL(url)
         uc = url.openConnection()
         uc.getInputStream()
         uc.disconnect()
     except (UnknownHostException, IOException, SocketException):
         print url
         print "* I can't connect to the tool server."
开发者ID:alex85k,项目名称:qat_script,代码行数:17,代码来源:tool.py

示例8: send_to_josm

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
 def send_to_josm(self, url):
     """Remote control command to make JOSM download the area nearby
        the error
     """
     try:
         url = URL(url)
         uc = url.openConnection()
         uc.getInputStream()
         return True
     except UnknownHostException:
         return False
     except FileNotFoundException:
         return False
     except SocketException:
         print "\n* Please, enable JOSM remote from Preferences"
         return False
开发者ID:alex85k,项目名称:qat_script,代码行数:18,代码来源:qat_script.py

示例9: delete

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def delete(targetURL, params):
    url = URL(targetURL)
    connection = url.openConnection()
    connection.setRequestMethod("DELETE")    
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoOutput(2)
    inStream= connection.getInputStream()
    rd= BufferedReader(InputStreamReader(inStream))
    response = ""
    line = rd.readLine()
    while line != None:
        response +=line+"\r"
        line = rd.readLine()
    rd.close()
    return response
开发者ID:revolutionarysystems,项目名称:common-test,代码行数:18,代码来源:HttpCall.py

示例10: make_jar_classloader

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
    def make_jar_classloader(jar):
        import os
        from java.net import URL, URLClassLoader

        url = URL('jar:file:%s!/' % jar)
        if os._name == 'nt':
            # URLJarFiles keep a cached open file handle to the jar even
            # after this ClassLoader is GC'ed, disallowing Windows tests
            # from removing the jar file from disk when finished with it
            conn = url.openConnection()
            if conn.getDefaultUseCaches():
                # XXX: Globally turn off jar caching: this stupid
                # instance method actually toggles a static flag. Need a
                # better fix
                conn.setDefaultUseCaches(False)

        return URLClassLoader([url])
开发者ID:nelmiux,项目名称:DataManagement,代码行数:19,代码来源:test_support.py

示例11: post

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def post(targetURL, params, contentType="text/xml", username=None):

    #paramStr = params["data"]
    paramStr = ""
    for aKey in params.keys():
        paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&"
    paramStr=paramStr[:-1]
    url = URL(targetURL)
    print targetURL
    print paramStr
    print contentType
    url = URL(targetURL+"?"+paramStr)
    print url

    connection = url.openConnection()
    if username!=None:    
        userpass = username
        basicAuth = "Basic " + base64.b64encode(userpass);
        connection.setRequestProperty ("Authorization", basicAuth);
    connection.setRequestMethod("POST")
    if contentType != None:
        connection.setRequestProperty("Content-Type", contentType)
    connection.setRequestProperty("Content-Length", str(len(paramStr)))
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoInput(1)
    connection.setDoOutput(2)
    
    #wr= DataOutputStream(connection.getOutputStream())
    #wr.writeBytes(paramStr)
    #wr.flush()
    #wr.close()
    
    inStream= connection.getInputStream()
    rd= BufferedReader(InputStreamReader(inStream))
    response = ""
    line = rd.readLine()
    while line != None:
        response +=line+"\r"
        line = rd.readLine()
    rd.close()
    return response
开发者ID:revolutionarysystems,项目名称:common-test,代码行数:44,代码来源:HttpCall.py

示例12: __init__

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
 def __init__(self):
     sessionDuration = grinder.properties.getInt("player.sessionDurationInSeconds", 0)
     self.password = grinder.properties.getProperty("player.password")
     players_url = grinder.properties.getProperty("player.source-url")
     passwordHash = URLEncoder.encode(grinder.properties.getProperty("player.passwordHash"), "UTF-8")
     workers = grinder.properties.getInt("grinder.processes", 0)
     threads = grinder.properties.getInt("grinder.threads", 0)
     workerIndex = grinder.processNumber - grinder.firstProcessNumber
     threadsPerAgent = workers * threads
     agentIndex = grinder.agentNumber
     start = agentIndex * threadsPerAgent + workerIndex * threads
     if isLogEnabled:
         log("Worker will handle %s players starting from %d" % (threads, start))
     playerRange = (start, threads)
     self.workerPlayers = []
     params = "passwordHash=%s&minimumBalance=%s&startRow=%s&limit=%s" % (
         passwordHash,
         str(sessionDuration / 5),
         str(playerRange[0]),
         str(threads),
     )
     urlStr = players_url + "?" + params
     try:
         data = StringBuffer()
         url = URL(urlStr)
         conn = url.openConnection()
         rd = BufferedReader(InputStreamReader(conn.getInputStream()))
         line = rd.readLine()
         while line is not None:
             data.append(line)
             line = rd.readLine()
         rd.close()
         if isLogEnabled:
             log(data.toString())
         message = JSONValue.parse(str(String(data.toString(), "UTF-8")))
         for entry in message:
             self.workerPlayers.append((entry.get(0), entry.get(1)))
     except Exception:
         raise Exception("Couldn't fetch players from %s: %s" % (urlStr, traceback.format_exc()))
     if isLogEnabled:
         log(str(self.workerPlayers))
开发者ID:ShahakBH,项目名称:jazzino-master,代码行数:43,代码来源:yazino.py

示例13: get

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
def get(targetURL, params, username=None):

    paramStr = ""
    for aKey in params.keys():
        paramStr+=aKey+"="+URLEncoder.encode(params[aKey], "UTF-8")+"&"
    paramStr=paramStr[:-1]
    url = URL(targetURL+"?"+paramStr)
    print url
    connection = url.openConnection()

    if username!=None:    
        userpass = username
        basicAuth = "Basic " + base64.b64encode(userpass);
        print basicAuth
        connection.setRequestProperty ("Authorization", basicAuth);
    
    connection.setRequestMethod("GET")    
    connection.setRequestProperty("Content-Language", "en-GB")
    connection.setUseCaches(0)
    connection.setDoOutput(2)
    
    return getResponse(connection);
开发者ID:revolutionarysystems,项目名称:o-test,代码行数:24,代码来源:HttpCall.py

示例14: call

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
    def call(self):

        consumer = DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);

        provider = DefaultOAuthProvider(
            REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZE_URL
        );

        print "Fetching request token from PassaporteWeb..."

        authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
        authUrl = OAuth.addQueryParameters(authUrl, OAuth.OAUTH_CONSUMER_KEY, CONSUMER_KEY);

        print "Request token: ", consumer.getToken()
        print "Token secret: ", consumer.getTokenSecret()

        print "Now visit:\n", authUrl, "\n... and grant this app authorization"
        print "After granting authorization, you should be redirected to the callback url"

        pin = raw_input("Enter the PIN code and hit ENTER when you're done:")

        print "Fetching access token from PassaporteWeb..."

        provider.retrieveAccessToken(consumer, pin);

        print "Access token: ", consumer.getToken()
        print "Token secret: ", consumer.getTokenSecret()

        url = URL(USER_DATA);
        request = url.openConnection();

        consumer.sign(request);

        print "Sending request..."
        request.connect();

        print "Response: %d %s" % (request.getResponseCode(), request.getResponseMessage())
开发者ID:vitormazzi,项目名称:signpost-examples,代码行数:39,代码来源:PassaporteWeb.py

示例15: doInBackground

# 需要导入模块: from java.net import URL [as 别名]
# 或者: from java.net.URL import openConnection [as 别名]
    def doInBackground(self):
        """Download errors data
        """
        #Download and parse errors
        self.tool = self.app.downloadingTool
        self.checks = self.app.downloadingChecks

        #Initialize progress property.
        progress = 0
        self.super__setProgress(progress)

        progress = 1
        self.super__setProgress(progress)

        #debug
        offline = False
        writeToFile = False

        if offline or self.tool.isLocal:
            if offline and not self.tool.isLocal:
                fileName = File.separator.join([self.app.SCRIPTDIR,
                                                "%s_errors.gfs" % self.tool.name])
            else:
                fileName = self.tool.fileName
            print "\n  read errors from file: %s" % fileName

            inFile = open(fileName, "r")
            self.app.errorsData = inFile.read()
            inFile.close()
        else:
            try:
                print "\n  url: ", self.app.downloadingUrl
                url = URL(self.app.downloadingUrl)
                uc = url.openConnection()
                ins = uc.getInputStream()
                inb = BufferedReader(InputStreamReader(ins, Utils.UTF_8))
                builder = StringBuilder()
                line = inb.readLine()
                while line is not None and not self.isCancelled():
                    builder.append(line)
                    builder.append(System.getProperty("line.separator"))
                    line = inb.readLine()
                if self.isCancelled():
                    if inb is not None:
                        inb.close()
                    if uc is not None:
                        uc.disconnect()
                    return
                inb.close()
                uc.disconnect()
                self.app.errorsData = builder.toString()
            except (UnknownHostException, FileNotFoundException, IOException, SocketException):
                msg = self.app.strings.getString("connection_not_working")
                self.app.downloadAndReadDlg.progressBar.setIndeterminate(False)
                self.app.downloadAndReadDlg.dispose()
                JOptionPane.showMessageDialog(Main.parent, msg)
                self.cancel(True)
                return

        if writeToFile:
            f = open(File.separator.join([self.app.SCRIPTDIR,
                                          "%s_errors.gfs" % self.tool.name]), "w")
            f.write(self.app.errorsData.encode("utf-8"))
            f.close()
            print "errors file saved", File.separator.join([self.app.SCRIPTDIR,
                                                            "%s_errors.gfs" % self.tool.name])
开发者ID:alex85k,项目名称:qat_script,代码行数:68,代码来源:download_and_parse.py


注:本文中的java.net.URL.openConnection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。