通过python脚本通过代理连接到数据库 [英] Connect to Database via proxy a python script
问题描述
我有一个使用python库MySQLdb连接到远程MySQL数据库的python脚本.它工作正常,但是如何在工作时通过我后面的代理连接它.我可以通过ssh中的命令行进行连接,但是如何获取python脚本以使用代理设置.在MySQLdb命令中似乎没有用于代理配置的任何选项.
I have a python script that connects to a remote MySQL database using the python library MySQLdb. It works fine but how can I get it to connect through a proxy that I'm behind when at work. I can connect via the command line in ssh, but how do I get the python script to use the proxy settings. There doesnt seem to be any options in the MySQLdb commands for proxy configurations.
import MySQLdb as mdb
conn=mdb.connect(host='mysite.com',user='myuser',passwd='mypassword',db='mydb')
cursor = conn.cursor()
推荐答案
我知道这是一篇比较老的文章,但我认为我还是会回答的.
您可以将SSH连接用作代理
使用proxychains
的最简单方法
代理链的默认端口为9050,因此当您连接到远程主机时,请包括-D
参数,例如:
ssh -D 9050 -l user remotehost
然后,在本地计算机上的单独终端窗口中,或使用screen
,您以proxychains
开头的任何命令都将通过SSH服务器进行路由,例如:
proxychains python myscript.py
将路由所有出站TCP请求,无论是数据库连接还是urllib2/requests HTTP(S)请求.
Proxychains并非特定于python,而是其他任何东西.您可以轻松启动Web浏览器或其他任何工具.尝试proxychains firefox
或proxychains curl https://api.ipify.org
I know this is a rather old post, but I thought I'd answer it anyway.
you can use your SSH connection as a proxy
The easiest way it to use proxychains
The default port for proxychains is 9050 so when when you connect to the remote host include the -D
parameter like:
ssh -D 9050 -l user remotehost
Then from a separate terminal window, or using screen
, on your local machine any command you preface with proxychains
is routed through the SSH server, for example:
proxychains python myscript.py
will route all outbound TCP requests, whether a database connection or a urllib2/requests HTTP(S) request.
Proxychains is not specific to python, but anything. You can just as easily launch a web browser or anything else. Try proxychains firefox
or proxychains curl https://api.ipify.org
这篇关于通过python脚本通过代理连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!