0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Pythonのurllib2でプロキシを刺す

Last updated at Posted at 2017-11-30

どうも、最近糞記事を量産して数を稼いでいる人です。

唐突ですが、如何わしい事をしたい人間は串を指したいものです。
なので、サンプルコードをここに置いておきます。
ちなみに、Python2系です。

set_prox.py
from __future__ import with_statement
import urllib2

#送信パラメーターをセット
obj = {"actions" : "param"}
#タイムアウト設定の時間
timeout = 0.5

with open('/proxy.txt') as ip_list:
    for ip in ip_list:
        proxy = {'http':'http://{0}'.format(ip)}
        proxy_handler = urllib2.ProxyHandler(proxy)
        opener = urllib2.build_opener(proxy_handler)
        urllib2.install_opener(opener)

        #Request作成
        request = urllib2.urlopen(request,timeout=timeout)
        param = urllib.urlencode( obj )
        request = urllib2.Request(url,param)
        #User-Agentを設定
        request.add_header("User-agent", 'Mozilla/5.0')
        #refererを設定
        request.add_header('Referer', 'http://hoge.com')
        #エンコード方法を設定
        request.add_header('Content-Type', 'application/x-www-form-urlencoded')
        request.add_data(param)

        try :
            #URLを開く(送信)
            request = urllib2.urlopen(request,timeout=timeout)
        except :
            print("error")

こんな感じでプロキシーをぶっ刺して送信することが出来ます。
Exceptのエラーについては、色々用意されてるみたいなので各自で調べて設定してください。

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?