How to send HTTP GET request from remote host using SSH connection in Python?

妖精的绣舞 提交于 2021-02-04 21:29:38

问题


I'm using an SSH connection with Paramiko. My code:

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=remote_host, username=remote_user, password=remote_password,
               port=remote_port)

How to send HTTP GET request from connected remote host (use it like a proxy)?


I've found solution according to the answer:

with SSHTunnelForwarder(
        ssh_address_or_host=(remote_host, remote_port),
        ssh_username=remote_user,
        ssh_password=remote_password,
        remote_bind_address=("www.python.org", 80),
        ) as tunnel:
    conn = http.client.HTTPConnection("127.0.0.1", port=tunnel.local_bind_port)
    conn.request("GET", '/')

回答1:


There are two options:

External tool

Use any tool available on the SSH server that can send the HTTP request. E.g. curl or wget:

curl https://www.example.com/

And execute it using Paramiko: Python Paramiko - Run command

This solution is easier, but has the dependency on the command — So it's also platform dependent.

Port forwarding

Forward a local port to the remote HTTP server port 80 and connect to the forwarded port using your local Python code.

You will find lot of examples how to forward a database port. Like this one: Enable Python to Connect to MySQL via SSH Tunnelling

In your case, you need to do the same, just instead of using a database client to connect to the forwarded port, you connect an HTTP client (like HTTPConnection).

Also in most cases, the database forwarding usually ends on the SSH server itself (localhost/127.0.0.1), while you want to connect further.

This solution is more complicated, but without external dependencies – So it's platform independent. On the other hand, the port forwarding is a special privilege, that may be restricted on the server (but usually it is not). Before trying to implement it, you can test it with your SSH client.



来源:https://stackoverflow.com/questions/64017727/how-to-send-http-get-request-from-remote-host-using-ssh-connection-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!