def request_url(self, url): if self.auth_info.cmd_request_url == "(webbrowser)": self.open_brower(url) elif self.auth_info.cmd_request_url == "(console_output)": utils.console_output(url) elif self.auth_info.cmd_request_url == "(local_webserver)+(webbrowser)": host = self.auth_info.host port = self.auth_info.port from third.server import ClientRedirectServer from third.server import ClientRedirectHandler import socket try: self.httpd = ClientRedirectServer((host, port), ClientRedirectHandler) self.open_brower(url) except socket.error: raise snserror.auth else: self.__last_request_time = self.time() cmd = "%s '%s'" % (self.auth_info.cmd_request_url, url) logger.debug("request_url command is: %s", cmd) res = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().rstrip() logger.debug("request_url result is: %s", res) return
def show(self): ''' Level 1 serialization and print to console See dump() ''' utils.console_output(unicode(self))
def start_loop(self): self.is_running = True try: self.server.connect(self.client) except socket.timeout: self.is_running = False console_output('TCP:%d send_loop failed' % self.port) return 1 self.receive_thread = threading.Thread(target=TCPManager.receive_loop, args=(self, )) self.send_thread = threading.Thread(target=TCPManager.send_loop, args=(self, )) self.receive_thread.start() self.send_thread.start() return 0
def receive_loop(self): console_output('TCP:%d receive_loop start' % self.port) while self.is_running: try: data = self.server.recv(512) # 接收数据和返回地址 if self.callback: self.callback(data, self.client) except socket.timeout: continue except: if self.isBind: self.client = None traceback.print_exc() self.is_running = False console_output('TCP:%d receive_loop done' % self.port)
def __init__(self, channel = None): self.token = None self.auth_info = snstype.AuthenticationInfo() self.__fetch_code_timeout = 2 self.__fetch_code_max_try = 30 # methods binding import time self.time = lambda : time.time() self.console_input = lambda : utils.console_input() self.console_output = lambda : utils.console_output() #self.utc2str = lambda u: utils.utc2str(u) #self.str2utc = lambda s: utils.str2utc(s) self._urlencode = lambda params : urllib.urlencode(params) # We can not init the auth client here. # As the base class, this part is first # executed. Not until we execute the derived # class, e.g. sina.py, can we get all the # information to init an auth client. self.auth_client = None if channel: self.read_channel(channel)
def __init__(self, channel=None): self.token = None self.auth_info = snstype.AuthenticationInfo() self.__fetch_code_timeout = 2 self.__fetch_code_max_try = 30 # methods binding import time self.time = lambda: time.time() self.console_input = lambda: utils.console_input() self.console_output = lambda: utils.console_output() #self.utc2str = lambda u: utils.utc2str(u) #self.str2utc = lambda s: utils.str2utc(s) self._urlencode = lambda params: urllib.urlencode(params) # We can not init the auth client here. # As the base class, this part is first # executed. Not until we execute the derived # class, e.g. sina.py, can we get all the # information to init an auth client. self.auth_client = None if channel: self.read_channel(channel)
def send_loop(self): console_output('TCP:%d send_loop start' % self.port) while self.is_running: if not self.q.empty(): data = self.q.get() if self.client is not None: try: self.server.send(data) # console_output('→%s:%d' % self.client) # print('%s' % data) except: traceback.print_exc() self.is_running = False if self.q.empty(): time.sleep(0.01) console_output('TCP:%d send_loop done' % self.port)
def receive_loop(self): console_output('UDP:%d receive_loop start' % self.port) while self.is_running: try: data, self.client = self.server.recvfrom(2048) # 接收数据和返回地址 if self.callback: self.callback(data, self.client) except socket.timeout: continue except ConnectionResetError: if self.isBind: self.client = None except: if self.isBind: self.client = None traceback.print_exc() console_output('UDP:%d receive_loop done' % self.port)
def fetch_code(self): if self.auth_info.cmd_fetch_code == "(console_input)": utils.console_output( "Please input the whole url from Broswer's address bar:") return self.console_input().strip() elif self.auth_info.cmd_fetch_code == "(local_webserver)": try: self.httpd.handle_request() if 'code' in self.httpd.query_params: code = self.httpd.query_params['code'] logger.info("Get code from local server: %s", code) return "http://localhost/?%s" % urllib.urlencode( self.httpd.query_params) else: #TODO: # There is a non repeatable bug here. # When we have multiple platforms to authorize, # successive platforms may fail in this branch. # That means there is other HTTP request to the local HTTP server # before the call_back URL. # # Solution: # * Configure different port for different channels. # This is solved at upper layer. # * Support random port by default. raise snserror.auth.fetchcode finally: del self.httpd else: cmd = "%s %s" % (self.auth_info.cmd_fetch_code, self.__last_request_time) logger.debug("fetch_code command is: %s", cmd) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.readline().rstrip() tries = 1 while ret == "(null)": tries += 1 if tries > self.__fetch_code_max_try: break time.sleep(self.__fetch_code_timeout) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().rstrip() return ret
def fetch_code(self): if self.auth_info.cmd_fetch_code == "(console_input)" : utils.console_output("Please input the whole url from Broswer's address bar:") return self.console_input().strip() elif self.auth_info.cmd_fetch_code == "(local_webserver)": try: self.httpd.handle_request() if 'code' in self.httpd.query_params: code = self.httpd.query_params['code'] logger.info("Get code from local server: %s", code) return "http://localhost/?%s" % urllib.urlencode(self.httpd.query_params) else: #TODO: # There is a non repeatable bug here. # When we have multiple platforms to authorize, # successive platforms may fail in this branch. # That means there is other HTTP request to the local HTTP server # before the call_back URL. # # Solution: # * Configure different port for different channels. # This is solved at upper layer. # * Support random port by default. raise snserror.auth.fetchcode finally: del self.httpd else : cmd = "%s %s" % (self.auth_info.cmd_fetch_code, self.__last_request_time) logger.debug("fetch_code command is: %s", cmd) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.readline().rstrip() tries = 1 while ret == "(null)" : tries += 1 if tries > self.__fetch_code_max_try : break time.sleep(self.__fetch_code_timeout) ret = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().rstrip() return ret
def request_url(self, url): if self.auth_info.cmd_request_url == "(webbrowser)" : self.open_brower(url) elif self.auth_info.cmd_request_url == "(console_output)" : utils.console_output(url) elif self.auth_info.cmd_request_url == "(local_webserver)+(webbrowser)" : host = self.auth_info.host port = self.auth_info.port from third.server import ClientRedirectServer from third.server import ClientRedirectHandler import socket try: self.httpd = ClientRedirectServer((host, port), ClientRedirectHandler) self.open_brower(url) except socket.error: raise snserror.auth else : self.__last_request_time = self.time() cmd = "%s '%s'" % (self.auth_info.cmd_request_url, url) logger.debug("request_url command is: %s", cmd) res = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True).stdout.read().rstrip() logger.debug("request_url result is: %s", res) return
def list_channel(self, channel=None, verbose=False): if channel: try: console_output(str(self[channel].jsonconf)) except KeyError: logger.warning("No such channel '%s'.", channel) else: console_output("") console_output("Current channels:") for cname in self.iterkeys(): c = self[cname].jsonconf console_output(" * %s: %s %s" % \ (c['channel_name'],c['platform'],c['open'])) if verbose: console_output(" %s" % json.dumps(c)) console_output("")
def list_platform(self): console_output("") console_output("Supported platforms:") for p in platform.platform_list: console_output(" * %s" % p) console_output("")
def list_channel(self, channel = None, verbose = False): if channel: try: console_output(str(self[channel].jsonconf)) except KeyError: logger.warning("No such channel '%s'.", channel) else: console_output("") console_output("Current channels:") for cname in self.iterkeys(): c = self[cname].jsonconf console_output(" * %s: %s %s" % \ (c['channel_name'],c['platform'],c['open'])) if verbose: console_output(" %s" % json.dumps(c)) console_output("")