简体   繁体   English

更改python请求的IP

[英]Changing IP of python requests

How do I change the IP of HTTP requests in python?如何在python中更改HTTP请求的IP?
My friend built an API for a website, and sometimes it blocks certain IP's and so we need to change the IP of the request... here is an example:我的朋友为一个网站构建了一个 API,有时它会阻止某些 IP,因此我们需要更改请求的 IP……这是一个示例:

login_req = self.sess.post('https://www.XXX/YYY', params={...}

Now, each request that it sends, is through the computer's IP, and we need it basically to pass through an imaginary VPN.现在,它发送的每个请求都是通过计算机的 IP,我们基本上需要它通过一个虚构的 VPN。

Thanks for the help.谢谢您的帮助。 If something isn't clear I will explain.如果有什么不清楚的,我会解释。

Short answer : you can't.简短的回答:你不能。

Long answer : it seems like you're misunderstanding how IP addresses work.长答案:您似乎误解了 IP 地址的工作方式。 Your IP address is the network address that corresponds to your computer - when you send a request to a server, you attach your IP as a "return address" of sorts, so that the server can send a response back to you.您的 IP 地址是与您的计算机对应的网络地址 - 当您向服务器发送请求时,您会将您的 IP 作为各种“返回地址”附加,以便服务器可以向您发送响应。

However, just like a physical address, you don't get to choose what your IP address is – you live on a street, and that's your address, you don't get to change what the street is called or what your house number is.然而,就像物理地址一样,您无法选择您的 IP 地址——您住在一条街上,那是您的地址,您无法更改街道名称或门牌号. In general, when you send a request from your computer, the message passes through a chain of devices.通常,当您从计算机发送请求时,消息会通过一系列设备。 For example:例如:

Your computer --> Your router --> Your ISP --> The Server

In a lot of cases, each of these assigns a different IP address to whatever's below it.在很多情况下,它们中的每一个都为它下面的任何内容分配一个不同的 IP 地址。 So, when your request passes through your router, your router records your IP address and then forwards the request through your ISP using its own IP address.因此,当您的请求通过您的路由器时,您的路由器会记录您的 IP 地址,然后使用自己的IP 地址通过您的 ISP 转发请求。 Hence how several users on the same network can have the same IP address.因此,同一网络上的多个用户如何拥有相同的 IP 地址。

There are physical IP addresses, that correspond directly to devices, but there are a limited amount of these.存在直接对应于设备的物理 IP 地址,但数量有限。 Mostly, each Internet Service Provider has a few blocks of IP addresses that it can attach to things;大多数情况下,每个 Internet 服务提供商都有一些可以附加到事物的 IP 地址块; an ISP can keep a specific IP address pointed to a specific computer all of the time, but they don't have to, and for many of their regular users, they don't.一个ISP可以保持一个特定的IP地址指向一个特定的计算机的所有的时间,但他们没有,和他们的许多普通用户的,他们不知道。

Your computer has basically no power to determine what its own IP address is, basically.您的计算机基本上没有能力确定它自己的 IP 地址是什么。 There's nothing python can do about that.对此,python 无能为力。

Your Question :你的问题

we need [the request] basically to pass through an imaginary VPN.我们需要 [请求] 基本上通过一个虚构的 VPN。

It'd be easier to actually requisition a real proxy or VPN from somewhere and push your request through it.从某处实际申请真正的代理或 VPN 并通过它推送您的请求会更容易。 You'd have to talk with your internet service provider to get them to set something like that up for you specifically, and unless you're representing a reasonably big company they're unlikely to want to put in that effort.您必须与您的互联网服务提供商交谈,让他们专门为您设置类似的东西,除非您代表一家相当大的公司,否则他们不太可能愿意为此付出努力。 Most python libraries that deal with HTTP can easily handle proxy servers, so once you figure it out it shouldn't be a problem.大多数处理 HTTP 的 python 库都可以轻松处理代理服务器,所以一旦你弄清楚它应该不是问题。

You can use an IP address from https://www.sslproxies.org/您可以使用来自https://www.sslproxies.org/的 IP 地址

For example,例如,

import requests

response=requests.get("yourURL", proxies={'https': 'https://219.121.1.93:80', 'http': http://219.121.1.93:80 "})

The IP addresses on that site are pretty crappy and sometimes don't work, so it would be best to find a way to constantly scrape IP addresses from the site so you have a couple to try.该站点上的 IP 地址非常糟糕,有时无法正常工作,因此最好找到一种方法来不断从该站点抓取 IP 地址,这样您就可以尝试几个。 Check out this article: https://www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/查看这篇文章: https : //www.scrapehero.com/how-to-rotate-proxies-and-ip-addresses-using-python-3/

warning: These should not be used for sensitive information as they are not secure.警告:这些不应该用于敏感信息,因为它们不安全。 Don't use those IP addresses unless you are ok with anyone in the world knowing what your're doing.不要使用这些 IP 地址,除非您对世界上任何知道您在做什么的人感到满意。

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

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