I've had a look around here for multiprocessing, for the most part, I've gotten it narrowed down but the last bit fails for some reason.
Context
I have a remote webserver that I connect to that I have port forwarded the HTTP page to a local port. I need to connect to the remote host, while I'm connected, I need to open up the HTTP page on my local machine and capture the page.
Code
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()
Problem faced
The .png just displays a failed connection to the port ('localhost refused to connect')
SSHTunnel
So I had to research SSH Tunnel which creates a randomly generated local bind port to map the remote destinaton port to.
In short after repeat attempts I managed to get my end result, however if you are seeking an answer to a problem like mine I will provide my code and also an example that hopes to fit all instead of my specific issue
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')
Simplified
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.