def load_url(self, url, content=b''): ''' @name 转发URL到PHP-FPM @author hwliang<2020-07-11> @param url string(URI地址) @param content stream(POST数据io对象) @return fastcgi-socket ''' fcgi = fcgi_client.FCGIApp(connect=self.fcgi_sock) try: script_name, query_string = url.split('?') except ValueError: script_name = url query_string = '' env = { 'SCRIPT_FILENAME': '%s%s' % (self.document_root, script_name), 'QUERY_STRING': query_string, 'REQUEST_METHOD': request.method, 'SCRIPT_NAME': self.last_path + script_name, 'REQUEST_URI': self.last_path + url, 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_SOFTWARE': 'BT-Panel', 'REDIRECT_STATUS': '200', 'CONTENT_TYPE': request.headers.get('Content-Type', 'application/x-www-form-urlencoded'), 'CONTENT_LENGTH': str(request.headers.get('Content-Length', '0')), 'DOCUMENT_URI': request.path, 'DOCUMENT_ROOT': self.document_root, 'SERVER_PROTOCOL': 'HTTP/1.1', 'REMOTE_ADDR': request.remote_addr.replace('::ffff:', ''), 'REMOTE_PORT': str(request.environ.get('REMOTE_PORT')), 'SERVER_ADDR': request.headers.get('host'), 'SERVER_PORT': '80', 'SERVER_NAME': 'BT-Panel', } for k in request.headers.keys(): key = 'HTTP_' + k.replace('-', '_').upper() env[key] = request.headers[k] fpm_sock = fcgi(env, content) return fpm_sock
def load_url_public(self,url,content = b'',method='GET',content_type='application/x-www-form-urlencoded'): ''' @name 转发URL到PHP-FPM 公共 @author hwliang<2020-07-11> @param url string(URI地址) @param content stream(POST数据io对象) @return fastcgi-socket ''' fcgi = fcgi_client.FCGIApp(connect=self.fcgi_sock) try: script_name, query_string = url.split('?') except ValueError: script_name = url query_string = '' content_length = len(content) if content: content = StringIO(content) env = { 'SCRIPT_FILENAME': '%s%s' % (self.document_root, script_name), 'QUERY_STRING': query_string, 'REQUEST_METHOD': method, 'SCRIPT_NAME': self.last_path + script_name, 'REQUEST_URI': url, 'GATEWAY_INTERFACE': 'CGI/1.1', 'SERVER_SOFTWARE': 'BT-Panel', 'REDIRECT_STATUS': '200', 'CONTENT_TYPE': content_type, 'CONTENT_LENGTH': str(content_length), 'DOCUMENT_URI': script_name, 'DOCUMENT_ROOT': self.document_root, 'SERVER_PROTOCOL' : 'HTTP/1.1', 'REMOTE_ADDR': '127.0.0.1', 'REMOTE_PORT': '8888', 'SERVER_ADDR': '127.0.0.1', 'SERVER_PORT': '80', 'SERVER_NAME': 'BT-Panel' } fpm_sock = fcgi(env, content) _data = b'' while True: fastcgi_header = fpm_sock.recv(8) if not fastcgi_header: break if len(fastcgi_header) != 8: _data += fastcgi_header break fast_pack = struct.unpack(FCGI_Header, fastcgi_header) if fast_pack[1] == 3: break tlen = fast_pack[3] while tlen > 0: sd = fpm_sock.recv(tlen) if not sd: break tlen -= len(sd) _data += sd if fast_pack[4]: fpm_sock.recv(fast_pack[4]) status,headers,data = panelPHP().format_header_data(_data) return data