def analysisHTTP(requestinfo): infos = requestinfo.split('\r\n') logger.info('接收到的内容:' + str(infos)) request_action = infos[0].split(' ') request_method = request_action[0] request_url = request_action[1] request_protocol = request_action[2] request = eval(request_method + 'Request()') setinfos = getattr(request, 'setinfos') content = '' if request_method == 'GET': if '?' in request_url: content = request_url[request_url.index('?') + 1:] request_url = request_url[:request_url.index('?')] else: content = infos[-1] infodict = { 'method': request_method, 'url': request_url, 'protocol': request_protocol, 'content': content } for info in infos[1:]: infolist = info.split(': ') if len(infolist) != 2: continue infodict.setdefault(infolist[0], infolist[1]) setinfos(infodict) # print('\r\n收到' + request_method + '请求,请求内容如下:\r\n' + str(request.__dict__)) return request
def createResponse(req): res = response.HTTPResponse() res.setCookie(req) dispatcher(req,res) res.content_type = req.accept[0] resp = res.packageresponse() logger.info('返回的请求:{}'.format(resp)) return bytes(resp,'utf-8')
def getRequestObj(cli,addr): try: data = cli.recv(1024) reqmsg = data.decode('utf-8') logger.info('开始解析请求。。。') req = request.analysisHTTP(reqmsg) logger.info('请求解析成功。。。') return req except Exception as exp: logger.error(str(exp)) return request.GETRequest()
def refreshSessions(self, sessionID): if not sessionID or sessionID not in self.sessions.keys(): sessionObj = session.Session() self.sessions.update({sessionObj.SSESSIONID: sessionObj}) logger.info('当前session总量:{}'.format(self.sessions)) else: if sessionID in self.sessions.keys(): sessionObj = self.sessions.get(sessionID, None) if sessionObj.compareSessionTime(): sessionObj.changeLastTime() else: self.sessions.pop(sessionID) sessionObj = session.Session() self.sessions.update({sessionObj.SSESSIONID: sessionObj}) logger.info('当前session总量:'.format(self.sessions)) return sessionObj.SSESSIONID
def setinfos(self, infodict): self.referer = infodict.get('Referer', '') self.method = infodict.get('method', 'GET') self.url = infodict.get('url', '/') self.host = infodict.get('Host') self.connection = infodict.get('Connection', '') self.user_agent = infodict.get('User-Agent', '') self.upgrade_insecure_requests = int( infodict.get('Upgrade-Insecure-Requests', '1')) self.accept = infodict.get('Accept', '').split(',') self.accept_encoding = infodict.get('Accept-Encoding', '') self.accept_language = infodict.get('Accept-Language', '') self.params = self.analysisContent(infodict.get('content', '')) self.cookie = self.analysisContent(infodict.get('Cookie', '')) self.sessionId = self.cookie.get('sessionId', None) logger.info('接收到的sessionid:{}'.format(self.sessionId)) self.sessionId = ssmanage.refreshSessions(self.sessionId) logger.info('更新后的sessionid:{}'.format(self.sessionId))
def startConnector(self): while True: logger.info('等待连接。。。') cli,addr = self.socketServer.accept() #cli.settimeout(1) job = Job(cli,addr) logger.info('{}发起请求,正在将任务添加到任务池。。。'.format(addr)) logger.info('添加成功,当前任务池任务数{}'.format(self.pool.work_queue.count)) self.pool.execute(job)
def getSession(self, sessionID): logger.info('当前session总量:{}'.format(self.sessions)) return self.sessions.get(sessionID)
def findWepApps() : serviceHomePath = Application.serverConfig.get('workPath', '') apps = os.listdir(serviceHomePath) Application.serverConfig.update({'apps': apps}) logger.info('发现服务:{}'.format(apps))
def startConnector(): serviceConf = Application.serviceConfig[0] logger.info('准备在{0}:{1}启动连接器{2},线程数{3}。。。'.format(serviceConf['host'],serviceConf['port'],Application.serverConfig.get('serverName'),serviceConf['max_thread_num'])) connector = Connector(Application.serverConfig.get('serverName'),serviceConf['host'],serviceConf['port'],serviceConf['max_thread_num']) logger.info('连接器启动成功输入:{0}:{1} 访问主页'.format(serviceConf['host'],serviceConf['port'])) connector.startConnector()
#encoding=utf-8 import json from bin import server from tools.logutils import logger with open('mycat_conf.json') as conf: logger.info('读取配置文件mycat_conf.json') confdict = json.load(conf) logger.info('配置文件读取成功,开始初始化全局上下文ApplicationContext。。。') server.initApplication(confdict) logger.info('全局上下文配置成功,开始配置日志组件。。。') server.initLogger() logger.info('所有组件配置成功,开始启动服务。。。') server.startService()
def run(self): logger.info('开始处理任务。。。') response = engine.engine(self.cli,self.addr) self.cli.send(response) logger.info('任务处理成功。。。') self.cli.close()
def startService(): logger.info('开始发现服务。。。') service.findWepApps() logger.info('服务发现完成,启动连接器。。。') service.startConnector()