Created
October 12, 2012 07:48
-
-
Save ozozozd/3877842 to your computer and use it in GitHub Desktop.
Akamai Web UI Automated Purge
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cookie | |
from google.appengine.api import urlfetch | |
from urlparse import urljoin | |
import mechanize | |
from StringIO import StringIO | |
class GAEOpener(object): | |
def __init__(self): | |
self.cookie = Cookie.SimpleCookie() | |
self.last_response = None | |
def open(self, url, data = None): | |
base_url = url | |
if data is None: | |
method = urlfetch.GET | |
else: | |
method = urlfetch.POST | |
while url is not None: | |
self.last_response = urlfetch.fetch(url = url, | |
payload = data, | |
method = method, | |
headers = self._get_headers(self.cookie), | |
allow_truncated = False, | |
follow_redirects = False, | |
deadline = 10 | |
) | |
data = None # Next request will be a get, so no need to send the data again. | |
method = urlfetch.GET | |
self.cookie.load(self.last_response.headers.get('set-cookie', '')) # Load the cookies from the response | |
url = urljoin(base_url, self.last_response.headers.get('location')) | |
if url == base_url: | |
url = None | |
return self.last_response | |
def _get_headers(self, cookie): | |
headers = { | |
'Host' : 'control.akamai.com', | |
'User-Agent' : 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)', | |
'Cookie' : self._make_cookie_header(cookie) | |
} | |
return headers | |
def _make_cookie_header(self, cookie): | |
cookie_header = "" | |
for value in cookie.values(): | |
cookie_header += "%s=%s; " % (value.key, value.value) | |
return cookie_header | |
def get_cookie_header(self): | |
return self._make_cookie_header(self.cookie) | |
def purge(snippet_url): | |
gaeopener = GAEOpener() | |
publish_url = 'https://control.akamai.com/portal/eccu/ccu.jsp?sidebar=HTTPDL_ccu&type=context&tab=PUBLISH' | |
ak_url = 'https://control.akamai.com/EdgeAuth/login.jsp' | |
br = mechanize.Browser() | |
br.open(ak_url) | |
forms_gen = br.forms() | |
forms_gen.next() | |
real_form = forms_gen.next() | |
real_form.set_value("<username goes here>", name="username") | |
real_form.set_value("<password goes here>", name="password") | |
url, data, hdrs = real_form.click_request_data() | |
r = gaeopener.open(url, data) | |
resp_publish = gaeopener.open('https://control.akamai.com/portal/eccu/ccu.jsp?sidebar=HTTPDL_ccu&type=context&tab=PUBLISH') | |
forms = mechanize._form._ParseFileEx(StringIO(resp_publish.content), "http://control.akamai.com") | |
removeForm = forms[2] | |
# May add multiple URLs here separated by "\n" | |
removeForm.set_value(snippet_url, name='arlList') | |
url, data, hdrs = removeForm.click_request_data() | |
r = gaeopener.open(url, data) | |
return r.content |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment