【发布时间】: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