Different IP for each bot? - python

I'm doing a Python bot that will request an url under different IP addresses in one computer. Is there a way to change my IP address for free and apply it to the bot? I have looked around and it seems like people say that I should use proxies for this. But I'm not familiar with proxies and how to implement them in Python. It'd be great if someone can guide me.
Thanks

You can change your IP in python, but your gateway will not be able to route a different IP than one in your sub-net.
Therefore, you have to use a proxy or a diffente router.
If you have/know an active router that will forward your packages using NAT, you can it as the gateway for the IP of the URL you are going to request.
For changing routes you can use this package: https://pypi.python.org/pypi/pyroute2
For using proxies directly in your bot, assuming you are using urllib3, you can check this documentation: http://docs.python-requests.org/en/latest/user/advanced/.
Another thing you might do is to rent some VPS servers for different worldwide IPs, check this search for examples.

Related

I am looking to run a django server on my personal PC that can be available through WAN

I am currently working on a developmental website and I need to be able to move it to a public domain, so I went and made the purchase of a domain and now i want to make the django app i have written publically available without purchasing a web hosting service quite yet or I might plan on investing in an actually windows server, if anyone knows how to go about doing this purely for developmental use and proff of concept practice, it would be great to see a video or a step by step guide.
This is a really broad question, so I will break it down for you in bits.
Your domain name has 'DNS records'. There are several types but the one you are looking for is the an A record. This type of DNS record points the 'domain.com' to an IP adress. The IP adress needs to be your external IP (can be found by going to api.ipify.org for example). This will make sure that when people on the internet will go to 'domain.com', the traffic is routed to you.
Then, on your own router, you will probably need to configure a NAT rule (e.g. all incoming traffic on port 80 needs to be routed to local IP:80 <ip_adress_of_your_computer>). Often this is called 'port forwarding' on your router, but that depends on the model you have.
After that, your web app is available through your domain name. Note that this is only very basic and does not take into account SSL (https).

How do I get the client port number in a Django project?

I am using django to build my web server, other people connect to me as clients. Now I need to know the clients' port number to distinguish them. If their browser opens two 'Tabs' of the same link, i.e. two pages but the same link, I also have to distinguish them.
Although I know I can use request.META['REMOTE_ADDR'] to get the client's IP in my django view function, but this realy is not enough for me.
Then I studied some TCP/IP basics and then I know that in TCP/IP layer, every IP packet has an IP header which contains the client's port number. But how can I access it in django?
Additional info:
I'm using python 2.6 and django 1.4
I know every TAB of a browser will be allocated a random unique port to access my django web page port. -- see this link 'The web server opens port 80, but the browser has a different, randomly-assigned port.' I really need to distinguish them. So my intuitive thoughts is to use the port number in the IP packet. If you have any other suggestion, also welcome.
I have found the similar question here, but I am not using Apache now. And this may be hard for me to config so maybe causing other more complex questions. This might make this simple question complex.
while I debug the django , I find this
request.environ["wsgi.input"].raw._sock.getpeername()
maybe it can work
Yes, after days of struggling, I answer it, with a working, but ugly solution on 'how to get client port in Django'.
in your python26/Lib/SocketServer.py, find def process_request_thread,add
global gClientPort; gClientPort = client_address
use this global value in yout project. Its format is ('12.34.56.78',55437) for example. 55437 is the port number.
Your assumption about 'every user connection opens connection at unique port' is wrong. All users are using the same port to connect.
To distinguish users Django (and almost every other frameworks) is using sessions. Every user gets a cookie with his unique session ID, and this cookie is passed to a server on every connection, so the application can distinguish users.
Here is documentation on sessions:
https://docs.djangoproject.com/en/1.8/topics/http/sessions/

Changing IP for a scraping script

I am trying to a website, but my IP has been banned after a while. I have tried using Tor proxy, but it's unstable and slow. Therefor I think the best solution might be a standard proxy that would be obfuscating it's IP say once per 12 hours. Or do you have any other suggestion?
IP spoofing is useless since the server response would be delivered to some undesired address. You'll either have to ask the site owner or setup something like a botnet which won't be easy nor cheap.

Overriding hostname IP address in qtwebkit request

I'm downloading a web page (with PyQt4/QtWebKit) using given hostname, but I would like to use a pre-defined IP address for that hostname. For example, I need to hit "http://www.mysite.com" but use the IP address 1.2.3.4 instead of the actual resolved IP address. Is this at all possible in QtWebKit? I've tried a couple things so far:
Hitting http://1.2.3.4/ and sending a "Host" header of "www.mysite.com". This almost works, but ends up failing for a number of reasons (I'd be happy to go into more detail here).
Using a global /etc/hosts setting. This didn't work because it is hard to automate and I will be doing multiple downloads at once.
Is there a way to either in python or in PyQt4/QtWebKit to override the IP address associated with a hostname?
This is big for me. Any help at all would be greatly appreciated.
Use custom network access manager, something like this (C++): http://ariya.blogspot.com/2010/05/qnetworkaccessmanager-tracenet-speed.html, so that you can "hijack" the network request and "redirect" it to other domain.

IP address of domain on shared host

I have domain on a shared hosting provider. How do I find the direct IP address of my domain using Python?
Is it possible to post to a script on my domain using the IP address and not the website itself?
Thanks.
I guess the IP should be static so do you really need to look it up more than once?
You need to specify the domain name so that the webserver knows which host configuration to use if you don't have a dedicated IP or your host is the default for that webserver
import socket
socket.gethostbyname("www.stackoverflow.com")
'69.59.196.211'
will get you the ip address (as a string) of your domain.
However, if it's shared hosting I would think it highly unlikely that you'll be able to access your hosting via the ip - most likely you'll have something like Apache's VirtualHost Directive in place which limits you to only 'seeing' requests to your domain. Requests to the IP address will be served by some default configuration.
Would very much depend on the nature of your hosting.
A curious request ...
To look up a domain name, do something like this:
import socket
ipaddress = socket.gethostbyname('www.bbc.co.uk')
Regarding posting to the IP address:
I don't think it would work in the normal way (like from a browser), because there will probably be many sites held under that address.
But, I guess you could do it in a very manual way, using a programming language (e.g. Python), if you connected a client socket to the site's IP address, but still sent the website's name in the HTTP Host request header.
I don't know if that poses more questions than it answers, and I don't know why you'd want to do either of the above, but there it is.
Good luck!

Categories