python tool提交http/https request测试return code 200

import httplib

import ConfigParser
import multiprocessing 


def rest_test_connection_200(self):

        """
        this method is used for test http connection to REST Service
        """
        params = {}
        ns_url = TEST_DATA.get("Notification Server", "REST_TestConnection")
        response = self.execute_request('GET', ns_url, params, 0, 0)
        print response

        return response

 def rest_removeprofile_401(self):
        """
        this method is used for simulating air-watch call to remove device profile to REST Service
        """
        params = {
                "RequestID": "123456789abcdef",
                "User": {
                      "Email": "xwang@websense.com"
                    },
                "Device": {
                      "Udid": "4B6D5E40DFB22BD57EC4AF54DA1F297A3221388B"
                    }
                }

        ns_url = TEST_DATA.get("Notification Server", "REST_RemoveProfile")
        response = self.execute_request('POST', ns_url, params, 0, 1)
        print response
        return response

    def execute_request(self, requestMethod, requestURI, params, contentflag=0, passwdflag=0, acct_index=0):
        if contentflag == 0:
            headers = {"Content-Type": "application/json", "Accept": "application/json"}
        else:
            headers = {"Content-Type": "application/xml", "Accept": "application/json"}
        status = 200

        try:
            conn = httplib.HTTPSConnection(self.host, self.port)
            if passwdflag == 0:
                if acct_index == 0:
                    auth = base64.b64encode(self.username + ':' + self.password)
                elif  acct_index == 1:
                    auth = base64.b64encode(self.username2 + ':' + self.password)
                elif  acct_index == 2:
                    auth = base64.b64encode(self.username3 + ':' + self.password)
                elif  acct_index == 3:
                    auth = base64.b64encode(self.username4 + ':' + self.password)
            else:
                auth = base64.b64encode(self.username + ':' + self.wrongpassword)
            headers["Authorization"] = "Basic "+auth
            conn.request(requestMethod, requestURI, json.dumps(params), headers)
            tmslog.log("NotificationSimu_REST_Stab.py send out REST request - Request ID : %s on %s" %(params['RequestID'],datetime.now()))
            starttime = time.time()
            response = conn.getresponse()
            tmslog.log("NotificationSimu_REST_Stab.py get response from REST request - Request ID : %s with response %s" %(params['RequestID'], response.status))
            endtime = time.time()
            sec_cost = endtime - starttime
            output = "time cost for REST request ID : %s and response is : %s seconds." %(params['RequestID'],  str(sec_cost))
            tmslog.log(output)
            #tmslog.log('-'*20)
            status = response.status
            #return response.read().strip()
        except Exception, e:
            print repr(e)
        finally:
            if conn:
                conn.close()
        return status


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值