def main(): """ SendDataClient类,作为flashshot的服务 """ log.init_log('./logs/send_data_client') # 如果配置文件中配置了这个地区需要设定IP代理,则在上传文件前,先将代理IP取消,然后等执行完毕后再设置上 # 并且将 uping 设置为 1,此时每五分钟执行的checkIpProxy将不会修改此IP,上传结束后就修改回 0 if config.NEED_PROXY: configFile = ConfigParser.ConfigParser() configFile.read(CONFIGFILE) configFile.set("info", "uping", 1) configFile.write(open(CONFIGFILE, "w")) logging.info('setProxy("0") ') # 在传送图片前,先将本地代理IP关掉 ipProxy.setProxy("0") target_folder = sys.argv[1] target_filenames = get_file_list(target_folder) upload_files(target_folder, target_filenames) # 在传送图片后,将本地代理Ip继续设定 if config.NEED_PROXY: configFile = ConfigParser.ConfigParser() configFile.read(CONFIGFILE) ip1 = configFile.get("info", "ip1") configFile.set("info", "uping", 0) configFile.write(open(CONFIGFILE, "w")) enableProxyScript = "python ipProxy.py " + ip1 os.popen(enableProxyScript) # ipProxy.setProxy(ip1) logging.info('setProxy ' + ip1)
def screenshot(): print("screenshot start") username = request.query.username token = request.query.token if not check_login(username, token): return genErrorJsonString("login failed") url = request.query.url if url == '': return genErrorJsonString("UrlEmptyError!") province = request.query.province city = request.query.city useragent = request.query.useragent if useragent == '': return genErrorJsonString("UseragentEmptyError") region = '' if not province == '': region = province else: region = city ip = ipProxy.getIpFromRegion(region.encode('utf8')) print(url + " " + ip + " " + region + " " + useragent) if ip == "IpNotFonud": return genErrorJsonString("IpNotFonud") try: # change the local IP by modify the registration list ipProxy.setProxy(ip) TIME_FORMAT = '%Y%m%d_%H%M%S' currentTime = datetime.datetime.now().strftime(TIME_FORMAT) outPutImg = url.replace(".", "").replace("http://www","")\ .replace("/","").replace(":","") +\ "_" + region + "_" + useragent + "_" + currentTime + ".png" # print(outPutImg) savedImg = "./snapshot/" + outPutImg screenshotScript = "phantomjs screenshot.js " + url + " " \ + savedImg + " " + useragent; print(screenshotScript) # subprocess.call(["phantomjs", "screenshot.js", url, savedImg.encode(sys.getfilesystemencoding()), useragent],shell=True) # screenResult = os.popen(screenshotScript.encode(sys.getfilesystemencoding())) # 解决了中文乱码问题, 使用subprocess可以使得disableProxy可以有效执行 screenResult = subprocess.call(screenshotScript.encode(\ sys.getfilesystemencoding())) print(screenResult) # must diableProxy before return if not screenResult == 0: return genErrorJsonString("fail to load the address") except Exception as e: print("ERROR: " + str(e.args)) finally: disableProxyScript = "python ipProxy.py 0" os.popen(disableProxyScript) outPutImgUrl = LOCAL_IP_ADDRESS + outPutImg print(outPutImgUrl) jsonStringToReturn = "{ 'screenshotUrl' : '" + outPutImgUrl + "'}" return jsonStringToReturn
import ipProxy ip = ipProxy.getIpFromRegion("beijing") print(ip) print("ip: " + ip) ipProxy.setProxy(ip) ipProxy.disableProxy() ipProxy.refresh()
def main(): """main""" logger.info("------------start--------------") config = ConfigParser.ConfigParser() config.read(CONFIGFILE) ip1 = config.get("info", "ip1") ip2 = config.get("info", "ip2") uping = config.get("info", "uping") # if uping == "1": print("uping ") logger.info("uping! return!") return logger.info(ip1) print(ip1) logger.info(ip2) print(ip2) if checkProxy(ip1): ipProxy.setProxy(ip1) if checkProxy(ip2): logger.info("ip1 and ip2 ok") print("ip1 and ip2 ok") return else: logger.info("ip2 need selectedIp") print("ip2 need selectedIp") # 从IP列表中找到IP不是ip1的IP并验证 selectedIp = getIpfromProxyList(ip1) config.set("info", "ip2", selectedIp) config.write(open(CONFIGFILE, "w")) return else: if checkProxy(ip2): logger.info("ip2 --> ip1") print("ip2 --> ip1") ipProxy.setProxy(ip2) selectedIp = getIpfromProxyList(ip2) config.set("info", "ip1", ip2) config.set("info", "ip2", selectedIp) config.write(open(CONFIGFILE, "w")) return # 如果两个IP都不可用,查找之后的ip仍然不可用,则驱动例行的grapCityIp.py提前执行 else: logger.info("ip1 and ip2 all need selectedIp") print("ip1 and ip2 all need selectedIp") selectedIp1 = getIpfromProxyList("0") # 一直循环驱动直到找到正确IP loopVar = 0 while selectedIp1 == "0": if loopVar < 3: loopVar += 1 elif loopVar < 5: loopVar += 1 # 如果超过三次还不成功,则取消代理 ipProxy.setProxy("0") else: return grapCityIpScript = "python grapCityIp.py" scriptResult = subprocess.call(grapCityIpScript.encode(\ sys.getfilesystemencoding())) logger.info(scriptResult) selectedIp1 = getIpfromProxyList("0") ipProxy.setProxy(selectedIp1) selectedIp2 = getIpfromProxyList(selectedIp1) config.set("info", "ip1", selectedIp1) config.set("info", "ip2", selectedIp2) config.write(open(CONFIGFILE, "w")) return
def screenshot(): print("screenshot start") username = request.query.username token = request.query.token if not check_login(username, token): return genErrorJsonString("login failed") url = request.query.url if url == '': return genErrorJsonString("UrlEmptyError!") province = request.query.province city = request.query.city useragent = request.query.useragent if useragent == '': return genErrorJsonString("UseragentEmptyError") region = '' if not province == '': region = province else: region = city ip = ipProxy.getIpFromRegion(region.encode('utf8')) print(url + " " + ip + " " + region + " " + useragent) if ip == "IpNotFonud": return genErrorJsonString("IpNotFonud") try: # change the local IP by modify the registration list ipProxy.setProxy(ip) TIME_FORMAT = '%Y%m%d_%H%M%S' currentTime = datetime.datetime.now().strftime(TIME_FORMAT) outPutImg = url.replace(".", "").replace("http://www","")\ .replace("/","").replace(":","") +\ "_" + region + "_" + useragent + "_" + currentTime + ".png" # print(outPutImg) savedImg = "./snapshot/" + outPutImg screenshotScript = "phantomjs screenshot.js " + url + " " \ + savedImg + " " + useragent print(screenshotScript) # subprocess.call(["phantomjs", "screenshot.js", url, savedImg.encode(sys.getfilesystemencoding()), useragent],shell=True) # screenResult = os.popen(screenshotScript.encode(sys.getfilesystemencoding())) # 解决了中文乱码问题, 使用subprocess可以使得disableProxy可以有效执行 screenResult = subprocess.call(screenshotScript.encode(\ sys.getfilesystemencoding())) print(screenResult) # must diableProxy before return if not screenResult == 0: return genErrorJsonString("fail to load the address") except Exception as e: print("ERROR: " + str(e.args)) finally: disableProxyScript = "python ipProxy.py 0" os.popen(disableProxyScript) outPutImgUrl = LOCAL_IP_ADDRESS + outPutImg print(outPutImgUrl) jsonStringToReturn = "{ 'screenshotUrl' : '" + outPutImgUrl + "'}" return jsonStringToReturn