Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request from different Country IP

Tags:

python

proxy

ip

I would like to view the movie content from a different country on the Playstation store. However, the playstation store blocks the IP based on location of the request, so a movie in Canada cannot be viewed from the US:

https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063

Is there a way to do something like the following:

url = 'https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063'
r = requests.get(url, proxy_from = COUNTRY['CA']) # In pseudocode

Basically, so that you can specify a country and then send a request from an IP that would be recognized as coming from that country. How would this be done?

like image 825
David542 Avatar asked Sep 28 '22 22:09

David542


1 Answers

If I understand correctly you are basically asking how to make a url request in python using a proxy?

If yes, you can do it like follows:

import urllib2
import urllib
import random

CAproxies = [{"http":"199.201.122.175:3128", "https":"199.201.122.175:3128"},{"http":"192.99.3.129:3128", "https":"192.99.3.129:3128"},{"http":"192.99.246.101:8118", "https":"192.99.246.101:8118"},{"http":"205.205.129.130:443", "https":"205.205.129.130:443"} ]


proxies = urllib2.ProxyHandler(random.choice(CAproxies))

url = 'https://store.playstation.com/#!/en-ca/movies/the-house/cid=UV0130-NPVA92773_CN-0000000000236063'

request = urllib2.Request(url)
request.add_header("User-Agent", "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:25.0) Gecko/20100101 Firefox/25.0")
request.add_header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")

opener = urllib2.build_opener(proxies)
urllib2.install_opener(opener)
r = urllib2.urlopen(request, timeout=15)
html = r.read()

The headers are good if you want the service to think you are using a browser, they usually have defences against bots. You need to replace the proxy address with your own proxy, this is just an invented proxy for illustration purposes.

A list of Proxies can be found here for example: http://www.proxy-listen.de/Proxy/Proxyliste.html In case the proxy given above doesn't work. In case one of the proxies work better for your particular location (lets say the 2nd one works best for you, it might be a good idea to change the random choice of a proxy to only the second one. i.e.

random.choice(CAproxies) -> CAproxies[1]

CAproxies[3] works the best for me. The first 250 characters from the html:

>>> html[0:250]
'<!DOCTYPE html>\n\n<html class="ctry mobvportA rgba">\n  <head>\n    <meta http-equiv="x-ua-compatible" content="IE=edge" />\n    <meta charset="utf-8"/>\n\n    <link rel="dns-prefetch" href="//ajax.googleapis.com">\n    <link rel="dns-prefetch" href="//ssl.'
like image 65
patapouf_ai Avatar answered Oct 02 '22 16:10

patapouf_ai