【问题标题】:python- How to properly send http GET requests to different Host-IPs but same hostpython-如何正确地将http GET请求发送到不同的主机IP但相同的主机
【发布时间】:2015-04-16 13:56:27
【问题描述】:

我正在尝试编写一个小脚本来将 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)

【问题讨论】:

  • 您可以使用带有计数器的while 循环来代替for 循环。每次增加计数器,然后你就可以正确地做到这一点。

标签: python json ip python-requests host


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-04
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多