简体   繁体   English

连接到 python 中的代理 (SOCKS) 数据库

[英]Connect to Proxy (SOCKS) Database in python

I am trying to connect to a database that needs proxy (socks) to be able to connect, if I use the proxy connection manually, I can connect, but I need to make the script connect to the proxy (socks) of the machine to make this SELECT我正在尝试连接到需要代理(socks)才能连接的数据库,如果我手动使用代理连接,我可以连接,但我需要让脚本连接到机器的代理(socks)做这个SELECT

SCRIPT脚本

import socket
import socks
import requests
import pymssql

socks.set_default_proxy(socks.SOCKS5, "138.34.133.155", 1080, True, 'user','password')
socket.socket = socks.socksocket

server = '172.43.56.89'
username = 'user'
password = 'password'
database = 'dbname'

conn = requests.get(pymssql.connect(host=server,user=username,password=password,database=database))

cursor = conn.cursor()

cursor.execute("SELECT column FROM table")

row = cursor.fetchall()

conn.close()

for i in row:
    print(i)

OUTPUT OUTPUT

Traceback (most recent call last): File "connection.py", line 15, in conn = requests.get(pymssql.connect(host=server,user=username,password=password,database=database)) File "src\pymssql.pyx", line 642, in pymssql.connect pymssql.OperationalError: (20009, 'DB-Lib error message 20009, severity 9:\nUnable to connect: Adaptive Server is unavailable or does not exist (172.43.56.89:1433)\nNet-Lib error during Unknown error (10060)\n') Traceback(最近一次调用最后一次):文件“connection.py”,第 15 行,在 conn = requests.get(pymssql.connect(host=server,user=username,password=password,database=database)) 文件“src\ pymssql.pyx", line 642, in pymssql.connect pymssql.OperationalError: (20009, 'DB-Lib 错误消息 20009, 严重性 9:\n无法连接: Adaptive Server 不可用或不存在 (172.43.56.89:1433) \n未知错误期间的 Net-Lib 错误 (10060)\n')

I think an option is to mount a local tunnelling sock with port forwarding, to map your database port and act as if your server where a localhost one.我认为一个选项是安装一个带有端口转发的本地隧道 sock,到 map 您的数据库端口,并就像您的服务器在本地主机一样。

It's really efficient if you're running your python script on a Unix computer.如果您在 Unix 计算机上运行 python 脚本,这真的很有效。

Something like this system call (for a 3306 mariaDB):像这样的系统调用(对于3306 mariaDB):

ssh -L 3306:localhost:3306 user@x.x.x.x

First, your run SSH, then, you tell him to enable a port forwarding from the 3306 port to the localhost:3306 port of the server you connect through user@IP .首先,您运行 SSH,然后,您告诉他启用从3306端口到您通过user@IP连接的服务器的localhost:3306端口的端口转发。

With this, every query from your local machine:3306 will by send to your MariaDB:3306 server, allowing you to use it as if you where on the server.这样,来自local machine:3306的每个查询都将发送到您的MariaDB:3306服务器,让您可以像在服务器上一样使用它。

If you do not want to hack into pymssql source code, there're external tools that redirect all TCP traffic over the socks proxy, such as FreeCap for Windows, RedSocks for Linux and Proximac for macOS.如果您不想侵入pymssql源代码,则可以使用外部工具通过 socks 代理重定向所有 TCP 流量,例如用于 Windows 的FreeCap 、用于ZEDC9F0A5A5D577938311647 macOS 的RedSocks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM