python+socket测试

__author__ = 'mutingting'
# -*- coding: UTF-8 -*-
#client
import socket,datetime,time,xlrd
def GetDateTimeStr():
        return datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')

class SocketClient():
    sR = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    def InitSocket(self):
        # host = '192.168.0.40'
        host = '10.0.238.1'
        port = 8000
        SocketClient.sR.bind((host,port))#绑定端口
        SocketClient.sR.listen(5)#等待客户连接,监听sock对象

        host = '10.0.238.1'
        # host = '192.168.0.40'
        port = 8081
        SocketClient.s.connect((host,port))   #建立连接

    def Uinit(self):
        SocketClient.sR.close()
        SocketClient.s.close()

    def client(self,message):
        SocketClient.s.sendall(message.encode('utf-8'))#发送数据
        clientSock,addr = SocketClient.sR.accept()#建立客户端连接) #创建接收客户端的一个对象connection(名字随便起);把客户端的Ip和port存储到addr
        data = clientSock.recv(2048).decode()#接收数据
        print(GetDateTimeStr() + ' Receive Data = {}'.format(data))
        return data
    def read_file(self):
        ExcelFile = xlrd.open_workbook(r'D:\ccp用例_V1.2_0410.xlsx')
        sheet = ExcelFile.sheet_by_name('Sheet1')
        rows = sheet.nrows
        todyTime = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        for row in range(1, rows):
            cellValue = sheet.cell(row, 6).value
            natureValue = sheet.cell(row, 5).value
            print(natureValue)
            receivedata = obj.client(cellValue)
            with open('D:\\'+todyTime+' TestCase.txt', 'a')as testcase_file:
                testcase_file.write(receivedata +'\n')
            with open('D:\\'+todyTime+' Result.txt', 'a')as result_file:
                if '|0|'not in receivedata and natureValue in "正用例":
                    print('failed'+ '\n'+ "--------------------------------------------------")
                    result_file.write("failed"+'\n')
                elif '|0|'in receivedata and natureValue in '反用例':
                    print('failed'+ '\n'+ "--------------------------------------------------")
                    result_file.write('failed'+'\n')
                else:
                    print("pass"+ '\n'+ "--------------------------------------------------")
                    result_file.write('pass'+'\n')

if __name__ == '__main__':
    obj = SocketClient()
    obj.InitSocket()
    obj.read_file()
    obj.Uinit()








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值