Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python连接到远程主机并在本地主机上捕获网页_Python_Selenium_Subprocess_Python Multiprocessing_Pyvirtualdisplay - Fatal编程技术网

python连接到远程主机并在本地主机上捕获网页

python连接到远程主机并在本地主机上捕获网页,python,selenium,subprocess,python-multiprocessing,pyvirtualdisplay,Python,Selenium,Subprocess,Python Multiprocessing,Pyvirtualdisplay,我在这里查看了多处理,大部分情况下,我已经缩小了范围,但最后一点由于某种原因失败了 上下文 我有一个连接到的远程Web服务器,我已将HTTP页面转发到本地端口。我需要连接到远程主机,在连接的同时,我需要打开本地机器上的HTTP页面并捕获该页面 代码 from selenium import webdriver from pyvirtualdisplay import Display import paramiko import multiprocessing import time def g

我在这里查看了多处理,大部分情况下,我已经缩小了范围,但最后一点由于某种原因失败了

上下文

我有一个连接到的远程Web服务器,我已将HTTP页面转发到本地端口。我需要连接到远程主机,在连接的同时,我需要打开本地机器上的HTTP页面并捕获该页面

代码

from selenium import webdriver
from pyvirtualdisplay import Display
import paramiko
import multiprocessing
import time

def grabscreendisplay():
 time.sleep(10)
 print('running display code now... ')
 display = Display(size=(1024, 768), visible=0)
 display.start()
 driver=webdriver.Chrome('/usr/local/bin/chromedriver')
 URL="http://localhost:9012"
 driver.get(URL)
 screenshot=driver.save_screenshot('my_screenshot.png')
 driver.quit()


def getserver():
    SERV = raw_input('Please enter the server you would like to connect to: ')
    return SERV
def connect_to_server(SERV):
    print(SERV)
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    print('Connecting to ' + SERV)
    ssh.connect(SERV, username='root', key_filename='...')
    connected = True
    print('Connected to ' + SERV)
    time.sleep(30)

def main():
    SERV = getserver()
    p1 = multiprocessing.Process(name='p1', target=connect_to_server(SERV))
    p2 = multiprocessing.Process(name='p2', target=grabscreendisplay())
    p2.start()
    p1.start()

if __name__ == "__main__":
    main()
面临的问题

 with SSHTunnelForwarder(
        <REMOTE_HOST IP>,
        ssh_username=<ssh_username>,
        ssh_pkey=<location of own private key>,
        remote_bind_address=(<REMOTE_HOST>, <REMOTE_PORT>),
        ) as tunnel: 
        client = paramiko.SSHClient() # set up SSH connection
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(<REMOTE_IP>, username=<ssh_username>, key_filename=<location of own private key>)
        display = Display(size=(1024, 768), visible=0)
        display.start()
        driver=webdriver.Chrome('/usr/local/bin/chromedriver')
        print(tunnel.local_bind_port)
        while True:
           sleep(30)
           # ctrl + c to stop program
        # Go into own local web browser and enter http://localhost:<local_bind_port> and you should see the web page

.png只显示到端口的连接失败('localhost拒绝连接')

SSHTunnel

因此,我必须研究SSH隧道,它创建一个随机生成的本地绑定端口,将远程destinaton端口映射到该端口

简单地说,在重复尝试之后,我设法得到了最终结果,但是,如果您正在寻找像我这样的问题的答案,我将提供我的代码和一个希望适合所有人的示例,而不是我的特定问题

from sshtunnel import SSHTunnelForwarder
import paramiko
import time
SSHTunnelForwarder.SSH_TIMEOUT = SSHTunnelForwarder.TUNNEL_TIMEOUT = 5.0

                     ### Setting up the SSHTunnel ###

with SSHTunnelForwarder(
        SERV, #IP of 10.10.10.1
        ssh_username='admin',
        ssh_pkey="...",
        remote_bind_address=(SERV, 80), # map the 10.10.10.1 port 80
        ) as tunnel: 

                     ### preliminary SSH connection ###

        client = paramiko.SSHClient() # set up SSH connection
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(SERV, username='admin', key_filename='...')

             ### Setting up the pyvirtualdisplay on local machine ###

        display = Display(size=(1024, 768), visible=0) # set virtual window size
        display.start()
        driver=webdriver.Chrome('/usr/local/bin/chromedriver') # I'm using Chromedriver
        print(tunnel.local_bind_port) # this prints the randomly generated local port, for example in my case it would be localhost:32842 and would display the web page of the remote host
        time.sleep(5)                    
        URL='http://localhost'+':'+str(tunnel.local_bind_port)
        driver.get(URL)
        time.sleep(10)
        print(driver.title)
        screenshot=driver.get_screenshot_as_file('newscreen.png')
简化的

 with SSHTunnelForwarder(
        <REMOTE_HOST IP>,
        ssh_username=<ssh_username>,
        ssh_pkey=<location of own private key>,
        remote_bind_address=(<REMOTE_HOST>, <REMOTE_PORT>),
        ) as tunnel: 
        client = paramiko.SSHClient() # set up SSH connection
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(<REMOTE_IP>, username=<ssh_username>, key_filename=<location of own private key>)
        display = Display(size=(1024, 768), visible=0)
        display.start()
        driver=webdriver.Chrome('/usr/local/bin/chromedriver')
        print(tunnel.local_bind_port)
        while True:
           sleep(30)
           # ctrl + c to stop program
        # Go into own local web browser and enter http://localhost:<local_bind_port> and you should see the web page
使用SSHTunnelForwarder(
,
ssh_用户名=,
ssh_pkey=,
远程绑定地址=(,),
)作为隧道:
client=paramiko.SSHClient()#设置SSH连接
client.set\缺少\主机\密钥\策略(paramiko.AutoAddPolicy())
client.connect(,用户名=,密钥\文件名=)
显示=显示(大小=(1024768),可见=0)
display.start()
driver=webdriver.Chrome(“/usr/local/bin/chromedriver”)
打印(隧道本地绑定端口)
尽管如此:
睡眠(30)
#按ctrl+c停止程序
#进入自己的本地web浏览器并输入http://localhost: 你应该看到网页