예제 #1
0
def agent_system(s,sp=' ',return_error=False):
    '''运行指定cmd命令,获取返回code和结果'''
    s=[i.strip() for i in s.split(sp)]
    while '' in s:
        s.remove('')
    #import locale
    
    #if locale.getdefaultlocale()[1]=='cp936':
    #    recode=True
    #else:
    #    recode=False
    if len(s)==0:return None
    
    #s[0]='set PYTHONIOENCODING=UTF-8&&'+s[0] 

    subs=subprocess.Popen(s,bufsize=0,stdout=subprocess.PIPE,stdin=subprocess.PIPE,stderr=subprocess.PIPE)
    subs.stdin.close()
    subs.wait()

    result=''
    errors=''
    for line in subs.stdout.readlines():
        ##blow for pycharm and cygwin show chinese#

        output = auto_decode(line)
        result+=output
    for line in subs.stderr.readlines():
        ##blow for pycharm and cygwin show chinese#

        output = auto_decode(line)
        errors+=output
    if return_error:
        return (subs.returncode,result,errors)
    else:
        return (subs.returncode,result)
예제 #2
0
 def _unpack_http(self, data: bytes):
     '''bytes请求转为head(str)和body,自动处理编码和gzip'''
     head = self._get_http_head(data)
     body = self._get_http_body(data)
     #处理请求gzip
     if 'Content-Encoding: gzip' in head:
         body = self._gzip_decode(body)
     if 'Content-Type: text/html' in head:
         body = auto_decode(body)
     try:  #无标识才尝试
         body = auto_decode(body)
     except Exception as ex:
         pass
     return head, body
예제 #3
0
 def _get_http_head(self, data) -> str:
     if isinstance(data, str):
         data = data.strip().split('\r\n\r\n')
         return data[0]
     else:
         data = data.strip().split(b'\r\n\r\n')
         d = auto_decode(data[0])
         return d
예제 #4
0
    def _monitor_thread(self):
        while 1:
            try:
                state = self._thread_state_get()
                if state == 'end':
                    return
                elif state == 'pause':
                    time.sleep(0.001)
                    continue
                else:
                    pass

                if self._ping_process == None:
                    self._thread_info.append(
                        'ping_process==None -> thread_state={}->end'.format(
                            self._thread_state))
                    return

                while self._ping_process.poll() is None:
                    line = self._ping_process.stdout.readline()
                    output = auto_decode(line).strip()
                    if output == '': continue
                    self._last_update_time_set(time.time())
                    if len(output.split(' ')) >= 5 and ': ' in output:
                        self._last_delay_set(self._parse_ping_line(output))
                        self._history_record.append(
                            (self._last_update_time_get(),
                             self._last_delay_get()))
                    else:
                        self._last_delay_set(-1)
                        self._history_record.append(
                            (self._last_update_time_get(), -1))

                time.sleep(0.5)

            except Exception as ex:
                self._thread_info.append('exception -> ' + str(ex))
예제 #5
0
    def _recv_http(self, sock) -> bytes:
        data = b''
        nodata_num = 0
        while 1:
            if nodata_num > 15:  #不得已
                break

            ssl.wrap_socket()

            #sock=seeked_socket(sock)

            #sock=ssl.wrap_socket(sock)
            #data_recv = sock.recv(self.buffer)
            data_recv = sock.recv(1)
            # data_recv = sock.recv(self.buffer)
            if data_recv == b'\x16':

                #sock.seek(0)
                sock = ssl.wrap_socket(sock)
                data_recv = sock.recv(self.buffer)

            if len(data_recv) == 0:
                nodata_num += 1
            else:
                nodata_num = 0
            data += data_recv
            if b'\r\n\r\n' in data:  #接收完成请求头
                contenlen_index = data.split(
                    b'\r\n\r\n', maxsplit=1)[0].find(b'Content-Length: ')
                if contenlen_index == -1:
                    contenlen_index = data.split(
                        b'\r\n\r\n', maxsplit=1)[0].find(b'content-length: ')
                if contenlen_index == -1:
                    if data.split(b'\r\n')[0].split(b' ')[0] in (b'GET',
                                                                 b'OPTIONS'):
                        if data.split(b'\r\n\r\n')[1] == b'':
                            break
                        else:
                            continue
                    elif b'Transfer-Encoding: chunked' in data:
                        return self._recv_chunked(sock, data)
                    else:
                        time.sleep(0.1)
                        continue
                else:
                    #正常接收http body
                    tmp = data[contenlen_index:]

                    num = int(auto_decode(tmp.split(b'\r\n')[0][16:]), base=10)

                    now_num = len(tmp.split(b'\r\n\r\n', maxsplit=1)[-1])
                    if num == now_num:
                        break
                    elif now_num > num:
                        self._print(
                            '[-]warn:recv http response lenth error -> now_num>content lenth'
                        )
                        break
                    while 1:
                        data_recv = sock.recv(num - now_num)
                        data += data_recv
                        now_num += len(data_recv)
                        if num == now_num:
                            break
                        elif now_num > num:
                            self._print(
                                '[-]warn:recv http response lenth error -> now_num>content lenth'
                            )
                            break
                    break

        return data
예제 #6
0
 def _build_str(self, ba: bytearray, index):
     tmpline = ba[:index]
     try:
         return auto_decode(tmpline)
     except Exception as ex:
         return bytes(tmpline).__str__()