1

tor/proxyアドレスを使用してWebにアクセスするpythonでスクリプトを書きたいと思います。テストのために、次のスクリプトがあります。

import urllib2
from BeautifulSoup import BeautifulSoup

protocol = 'socks4'
ip = '127.0.0.1:9050'

proxy = urllib2.ProxyHandler({protocol:ip})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

page = urllib2.urlopen("http://www.ifconfig.me/ip").read()

print(page)

問題は、ターミナルから直接実行すると、自分の IP アドレスが表示されることです。

proxychains curl ifconfig.me/ip

別の IP が表示されます。どうすれば修正できますか?

ソックス 4 の代わりに http を使用すると、次のエラーが発生します。

Traceback (most recent call last):
  File "proxy_test.py", line 11, in <module>
    page = urllib2.urlopen("http://www.ifconfig.me/ip").read()
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 513, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 438, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 372, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 521, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 501: Tor is not an HTTP Proxy
4

1 に答える 1