class Port: def port_is_used(self, port_num): ''' 判断端口是否被占用 :return: ''' flag = True self.dos_cmd = DosCmd() result = self.dos_cmd.excute_cmd_result('netstat -ano | findstr ' + str(port_num)) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, device_list): ''' 生成可用端口 :param start_port: :param device_list: :return: ''' port_list = [] if device_list != None: while len(port_list) != len(device_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port = start_port + 1 return port_list else: print('端口生成失败')
class Port: def port_is_used(self, port_num): ''' 判断端口是否被占用 ''' flag = None self.dos = DosCmd() command = 'netstat -ano | findstr ' + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, device_list): '''start_port 4701 生成可用端口 @parameter start_port @parameter device_list ''' port_list = [] if device_list != None: while len(port_list) != len(device_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port = start_port + 1 return port_list else: print "生成可用端口失败" return None
class Server: def __init__(self): #执行控制台的命令使用 self.dos = DosCmd() #获取设备devices的集合 self.device_list = self.get_devices() def get_devices(self): ''' 获取设备devices的集合 ''' devices_list = [] #执行adb devices命令来获取 devices list result_list = self.dos.excute_cmd_result('adb devices') #取出devicees存入devices_list中 if len(result_list) >= 2: for i in result_list: if 'List' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': devices_list.append(devices_info[0]) return devices_list else: return None
class Port: def __init__(self): self.dos = DosCmd() def port_is_usrd(self, port_num): ''' 判断端口是否被占用 ''' flag = None command = 'netstat -ano|findstr ' + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, count): ''' 生成可用端口,参数起始端口号和端口个数 ''' port_list = [] if count > 0: while len(port_list) < count: if self.port_is_usrd(start_port) != True: port_list.append(start_port) start_port = start_port + 1 return port_list else: print u"生成可用端口失败" return None
class Port(object): def port_is_used(self, port_num): """ 判断端口是否被占用 """ flag = None self.dos = DosCmd() command = "netstat -ano | findstr " + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: flag = True else: flag = False return flag def creat_port_list(self, start_port, device_list): """ 生成可用端口 :param start_port: :param device_list: """ port_list = [] if device_list != None: while len(port_list) != len(device_list): if self.port_is_used(start_port) != True: port_list.append(start_port) start_port = start_port + 1 return port_list else: print "生成可用端口失败" return None
class CreatePort: def __init__(self): self.dos = DosCmd() def port_is_used(self, port_num): """ 判断端口号是否被占用 :param port_num: 端口号 :return: """ flag = None command = 'netstat -ano | findstr ' + str(port_num) result = self.dos.excute_cmd_result(command) if len(result) > 0: flag = True else: flag = False return flag def create_port_list(self, start_port, device_list): """ 生成可用的端口号列表 :param start_port: 开始端口号 :param device_list: 设备列表 :return: """ if device_list == None: print('设备列表为空,无法生成端口号') return None else: port_list = [] while len(port_list) != len(device_list): if not self.port_is_used(start_port): port_list.append(start_port) start_port = int(start_port) + 1 return port_list
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.write_file = WriteUserCommand() def get_devices(self): ''' 获取设备信息 ''' devices_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'List' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': devices_list.append(devices_info[0]) return devices_list else: return None def create_port_list(self, start_port): ''' 创建可用端口 ''' port = Port() port_list = [] port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self, i): ''' 生成命令 ''' #appium -p 4700 -bp 4701 -U 127.0.0.1:21503 command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command = "appium -p " + str(appium_port_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U " + device_list[ i] + " --no-reset --session-override --log E:/Teacher/Imooc/AppiumPython/log/test02.log" #appium -p 4723 -bp 4726 -U 127.0.0.1:62001 --no-reset --session-override --log E:/Teacher/Imooc/AppiumPython/log/test01.log command_list.append(command) self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]), str(appium_port_list[i])) return command_list def start_server(self, i): ''' 启动服务 ''' self.start_list = self.create_command_list(i) print self.start_list self.dos.excute_cmd(self.start_list[0]) def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') def main(self): thread_list = [] self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) thread_list.append(appium_start) for j in thread_list: j.start() time.sleep(25)
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_devices() self.write_file = WriteUserCommand() def get_devices(self): ''' 获取设备信息 ''' devices_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'list' in i: continue devices_info = i.split('\t') if devices_info[1] == 'device': # list index out of range devices_list.append(devices_info[0]) return devices_list else: return None def create_port_list(self, start_post): ''' 创建可用端口 ''' port = Port() port_list = [] port_list = port.create_post_list(start_post, self.device_list) return port_list def create_command_list(self, i): ''' 生成命令 ''' #appium -p 4700 -bp 4701 -U 127.0.0.1:21503 command_list = [] appium_post_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command = "appium -p " + str(appium_post_list[i]) + " -bp " + str( bootstrap_port_list[i] ) + " -U " + device_list[i] + " --no-reset --session-override" command_list.append(command) self.write_file.write_data(i, device_list[i], str(bootstrap_port_list[i]), str(appium_post_list[i])) return command_list def start_server(self, i): #启动服务 self.start_list = self.create_command_list(i) print self.start_list self.dos.excute_cmd(self.start_list[0]) def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('tasklist -f -PID node.exe') def main(self): self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) appium_start.start()
class Server: def __init__(self): self.dos = DosCmd() self.port = CreatePort() self.write_yaml = WriteUserconfigCommand() self.device_list = self.get_devices() self.start_list = self.start_appium_command_list() def get_devices(self): """ 获取设备信息 :return: """ command = 'adb devices' result_list = self.dos.excute_cmd_result(command) # print(result_list) devices_list = [] if len(result_list) > 1: for i in result_list: if '\tdevice' in i: devices_info = i.split('\tdevice') devices_list.append(devices_info[0]) return devices_list else: return None def create_port(self, start_port): """ 生成端口号列表 :param start_port: :return: """ port = CreatePort() port_list = port.create_port_list(start_port, self.device_list) return port_list def start_appium_command_list(self): # appium -p 4700 -bp 4701 -U 4f368950 """ 生成启动appium服务的命令列表集合 :return: """ # 先清空掉yaml文件 然后再写入 self.write_yaml.clear_data() command_list = [] appium_port_list = self.create_port(4700) bootstrap_port_list = self.create_port(4900) if self.device_list is not None: for i in range(len(self.device_list)): command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str( bootstrap_port_list[i]) + ' -U ' + str(self.device_list[i]) + ' --no-reset --session-override --log D:/code/log/test01.log --local-timezone ' command_list.append(command) self.write_yaml.write_data_to_yaml(i, self.device_list[i], bootstrap_port_list[i], appium_port_list[i]) return command_list def start_appium_server(self, i): """ 通过启动命令集合 执行单个启动服务命令 :param i: :return: """ self.dos.excute_cmd(self.start_list[i]) def kill_appium_server(self): find_command = 'tasklist | find "node.exe"' server_list = self.dos.excute_cmd_result(find_command) if len(server_list) > 1 : kill_command = 'taskkill -F -PID node.exe' self.dos.excute_cmd(kill_command) def main(self): """ 使用多线程运行启动设备服务 :return: """ # 先把当前的进程全部kill掉 self.kill_appium_server() # 多线程开始启动服务 for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_appium_server, args=(i,)) appium_start.start() time.sleep(10)
class Server: ''' 获取设备信息 ''' def __init__(self): self.write_file = WriteUserCommand() self.dos = DosCmd() self.device_list = self.get_devices() def get_devices(self): device_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) > 1: for i in result_list: if 'List' in i: continue device_info = i.split('\t') if device_info[1] == 'device': device_list.append(device_info[0]) return device_list else: return None def create_port_list(self, start_port): port = Port() port_list = [] port_list = port.create_port_list(start_port, self.device_list) return port_list def create_command_list(self,i): ''' appium -p 4700 -bp 4701 -U 127.0.0.1:62001 :return: ''' command_list = [] appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4800) deivce_list = self.device_list self.write_file.post_value(i,bootstrap_port_list[i],deivce_list[i],appium_port_list[i]) command = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str(bootstrap_port_list[i]) + ' -U ' + \ deivce_list[i] + ' --no-reset --session-override' command_list.append(command) if len(command_list) == 0: print('当前无可用设备') else: print(command_list) return command_list # 启动程序服务 def start_server(self, i): self.start_list = self.create_command_list(i) self.dos.excute_cmd(self.start_list[0]) # 杀掉进程 def kill_server(self): server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') else: print('没有找到相关进程') # 主函数使用多线程去调用start_server def main_server(self): self.kill_server() self.write_file.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i,)) appium_start.start() time.sleep(5)
class Server: def __init__(self): self.dos = DosCmd() self.device_list = self.get_device_list() self.opera_yaml = OperaYaml() def get_device_list(self): #获取设备列表 print 'start get devices...' device_list = [] result_list = self.dos.excute_cmd_result('adb devices') if len(result_list) >= 2: for i in result_list: if 'List' in i: continue device_info = i.split('\t') if device_info[1] == 'device': device_list.append(device_info[0]) return device_list else: return None def create_port_list(self, start_port): #创建可用端口列表 port = Port() port_list = port.create_port_list(start_port, len(self.device_list)) return port_list def create_command_i(self, i): #生成命令,非命令列表 #appium -p 4700 -bp 4900 -U deviceName appium_port_list = self.create_port_list(4700) bootstrap_port_list = self.create_port_list(4900) device_list = self.device_list command_i = 'appium -p ' + str(appium_port_list[i]) + ' -bp ' + str( bootstrap_port_list[i] ) + ' -U ' + device_list[i] + ' --no-reset --session-override' print command_i self.opera_yaml.write_data(i, bootstrap_port_list[i], device_list[i], appium_port_list[i]) return command_i def start_server(self, i): #启动服务 self.start_command = self.create_command_i(i) print "start server..." self.dos.excute_cmd(self.start_command) def kill_server(self): print 'start kill server...' #停止服务 server_list = self.dos.excute_cmd_result('tasklist | find "node.exe"') if len(server_list) > 0: self.dos.excute_cmd('taskkill -F -PID node.exe') def main(self): #主函数多线程 print 'start main server...' thread_list = [] self.kill_server() self.opera_yaml.clear_data() for i in range(len(self.device_list)): appium_start = threading.Thread(target=self.start_server, args=(i, )) thread_list.append(appium_start) for j in thread_list: print 'start thread ...' j.start() time.sleep(20)