简体   繁体   English

Python中的欺骗IP地址

[英]Spoof IP address in Python

There is a certain data API which I need to call to fetch some data. 我需要调用某些数据API来获取一些数据。 Imagine there are few million such records. 想象有几百万条这样的记录。 For each such record, I construct the data URL (that returns a JSON response) from the record id and API key, fetch the response and put it to my local database. 对于每条这样的记录,我根据记录ID和API密钥构造数据URL(返回JSON响应),获取响应并将其放入我的本地数据库。 Now if I do it on my Mac, even with a 16 GB RAM, this will take months. 现在,如果我在Mac上进行操作,即使具有16 GB的RAM,这也将花费数月。 Whereas if I try it on a 64 GB multicore EC2 desktop provided by AWS, it works lightning fast..except there is a problem. 而如果我在由AWS提供的64 GB多核EC2桌面上进行尝试,它的工作速度将非常快。.除非存在问题。

There is a rate limit on the number of queries the API server can answer. API服务器可以回答的查询数量受到速率限制。 If it goes beyond 40 any minute, it starts returning error messages in place of the JSON response. 如果超过40分钟,它将开始返回错误消息,而不是JSON响应。 And on the EC2 machine, it ends up going upto 600 requests per minute. 在EC2机器上,它最终每分钟最多处理600个请求。 If it works correctly, I will have all the data in 2-3 hours. 如果工作正常,我将在2-3小时内获得所有数据。 The funny thing is that the rate limit is imposed by IP, not by API key. 有趣的是,速率限制是由IP而不是API密钥强加的。 So if I can somehow spoof the IP address (say from a list of 15 IPs in a round-robin manner) for the requests, it will stay within the limits. 因此,如果我能够以某种方式欺骗请求的IP地址(例如,以循环方式从15个IP列表中),它将保持在限制范围内。 How do I do it? 我该怎么做? I am using urllib . 我正在使用urllib Here's my sample code: 这是我的示例代码:

url = urltemplate % (list_of_params_including_API_key)
data = json.load(urllib.urlopen(url))
//parse the data and load it into database

Use the socket.bind method to specify the source address of the network connection. 使用socket.bind方法指定网络连接的源地址。

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# bind socket to a particular IP. port 0 allows it to select an unused local port
s.bind((spoofed_ip, 0))
s.connect((server_ip, 80))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM