解决思路两种:
1.直接linux catalina.out读取:一分每10s轮询出现新的直接读取返回<br>
2.缓存注入cookie,先手动登录获取session 然后通过注入user,pwd 进cookie
import paramiko
import os
from time import sleep
class SSH(object):
def __init__(self,host,port,user,pwd):
self.host=host
self.port=port
self.user=user
self.pwd=pwd
"""此方法是统计在登陆之前获取的服务日志code的数量"""
def before_count(self,logfile_absolute_path):
logfile_parentdir,logfile_name=os.path.split(logfile_absolute_path)
shell="cd {}; cat {}|grep 动态验证码为".format(logfile_parentdir,logfile_name)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.host,self.port,self.user,self.pwd)
self.ssh=ssh
stdin, stdout, stderr = self.ssh.exec_command(shell)
res,err = stdout.readlines(),stderr.readlines()
result = res if res else err
return len(result)
# """其实这样写不是最好办法,解决多行根本是paramiko执行机制,
# 每次执行完ssh.exec_command()函数会自动回到session初始化root路径,多行建议以;分号隔开"""
def ssh_connect(self,logfile_absolute_path,beforecode_number):
target_code=""
for i in range(6):
logfile_parentdir,logfile_name=os.path.split(logfile_absolute_path)
shell="cd {}; cat {}|grep 动态验证码为".format(logfile_parentdir,logfile_name)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(self.host,self.port,self.user,self.pwd)
self.ssh=ssh
stdin, stdout, stderr = self.ssh.exec_command(shell)
res,err = stdout.readlines(),stderr.readlines()
result = res if res else err
if len(result)> beforecode_number:
var=result[-1].strip()
a=var.split("-")[-1]
target_code=a.split(r":")[-1].strip()
break
else:
sleep(10)
self.ssh.close()
return target_code
cookie注入:python自动化解决验证码以及滑块验证跳过,拿QQ邮箱为例
# coding =utf-8
from selenium import webdriver
from time import sleep
def access_code():
driver=webdriver.Chrome()
driver.implicitly_wait(5)
driver.get("https://mail.qq.com/cgi-bin/frame_html?sid=X8TXPdLKi6bWUyB7&r=56ca1d8478e5e4903be3ecab133915c6")
sleep(4)
s = driver.get_cookie("u")
print(s)
cookie_log = driver.get_cookies()
print(cookie_log)
driver.add_cookie({'name': 'u', 'value': '15367496688'})
""" add username cookie"""
driver.add_cookie({'name':'p','value': 'qweqwe123'})
"""add password cookie"""
driver.get("https://mail.qq.com/cgi-bin/frame_html?sid=X8TXPdLKi6bWUyB7&r=56ca1d8478e5e4903be3ecab133915c6")
driver.switch_to.frame("login_frame")
driver.find_element_by_id("u").send_keys("15367496688")
driver.find_element_by_id("p").send_keys("qweqwe123")
driver.find_element_by_id("login_button").click()
sleep(5)
driver.close()
if __name__ == "__main__":
access_code()