如何正确地将http GET请求发送到不同的主机IP但是相同的主机

时间:2015-02-16 05:15:03

标签: python json ip python-requests host

我尝试制作一个小脚本,将GET请求发送给拥有多个IP地址的主机。

到目前为止,我能够让脚本一次性(同一时间)发送带有这些不同IP的GET请求,是否有一种简单的方法来提供它,所以我提供了IP列表,它会发送请求IP#1然后下一个请求IP#2等。

Ps:这里是完整的代码,所以你可以理解我的问题,如果你想改进它,你也可以让我知道:)

感谢您的帮助! :d

#!/usr/bin/env python

import requests
import ujson
import time
import random


url = 'site'
headeR = {'Host': 'site.com'} 

while 1:


    hosts = ['X.X.X.235', 'X.X.X.94', 'X.X.X.191', 'X.X.X.247'] 
    cnt = 0
    while cnt<len(hosts):
      currentUrl = url.replace("site.com", hosts[cnt])
    cnt += 1
    r = requests.get(currentUrl , headers=headeR)
    listingInfoStr = r.content
    result= ujson.loads(listingInfoStr)
    listingInfoJson= result['listinginfo']  
    if listingInfoJson:
        for key, value in listingInfoJson.iteritems():
            #print("key %s, value %s" % (key, value))
            listingId = key
            try:
                subTotal = value["converted_price_per_unit"]
                feeAmount = value["converted_fee_per_unit"]
            except KeyError:
                continue
            totalPrice = subTotal + feeAmount
            totalPriceFloat = float(totalPrice) / 100
            print("listingId %s = [ %s + %s = %s ]" % (listingId, subTotal, feeAmount, totalPrice))
    else:
            print "Still Looking"
    time.sleep(25)

1 个答案:

答案 0 :(得分:1)

如果您可以向单个主机发出请求,则可以轻松地按顺序向不同主机发出多个请求:

import time

def query_listing_info(hosts):
    for host in hosts:
        info = get_listing_info(host)
        if not info:
            print "Still looking"
        else:
            for listing_id, sub_total, fee_amount in parse_listing_info(info):
                total_price = sub_total + fee_amount
                print("listingId {listing_id} = [ {sub_total} + {fee_amount} = "
                      " {total_price} ]".format(**vars()))

hosts = ['x.x.x.x', 'y.y.y.y']
while 1:
    query_listing_info(hosts)
    time.sleep(25) # pause requests

其中get_listing_info(host)host发出单一请求:

import urlparse
import requests

parsed_url = urlparse.urlsplit('http://example.com/render/')
params = {'start': '0', 'count': '1', 'currency': '20', 'country': 'CDN'}

def get_listing_info(host): 
    url = urlparse.urlunsplit(parsed_url[:1] + (host,) + parsed_url[2:])
    r = requests.get(url, headers={'Host': parsed_url.netloc}, params=params)
    return r.json().get('listinginfo')

parse_listing_info(info)提取必要的信息:

def parse_listing_info(listing_info):
    for id, info in listinginfo.iteritems():
        try:
           yield id, info["converted_price_per_unit"], info["converted_fee_per_unit"]
        except KeyError:
           pass