Skip to content

Commit 89c2b48

Browse files
committed
refactored http purge again for consistent interface, improved examples
1 parent 48c1390 commit 89c2b48

File tree

3 files changed

+27
-16
lines changed

3 files changed

+27
-16
lines changed

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ across multiple Varnish instances. Here are the features of this python module
3535
Example::
3636

3737
manager = VarnishManager( ('server1:6082', 'server2:6082') )
38-
manager.run('purge.url', '^/$')
38+
manager.run('ping')
39+
manager.run('ban.url ^/secret/$')
40+
manager.run('ban.list')
41+
manager.run('purge.url', 'http://mydomain.com/articles/.*')
3942
manager.close()
4043

4144
Testing::

runtests.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def test_ping(self):
1616
self.assert_(map(lambda x: isinstance(x, float), (True,True)))
1717

1818
def test_purge(self):
19-
self.assertEqual(self.manager.purge_url(
20-
'http://%s/myrandomurl/.*' % WEB_ADDR).status, 200)
19+
resp = self.manager.run(
20+
'purge.url', 'http://%s/myrandomurl/.*' % WEB_ADDR)[0][0]
21+
self.assertEqual(resp.status, 200)
2122

2223
def test_ban(self):
2324
regex = '^/banned/*'

varnish.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@
4343
format = '%(asctime)s %(levelname)s %(message)s',
4444
)
4545

46+
def http_purge_url(url):
47+
"""
48+
Do an HTTP PURGE of the given asset.
49+
The URL is run through urlparse and must point to the varnish instance not the varnishadm
50+
"""
51+
url = urlparse(url)
52+
connection = HTTPConnection(url.hostname, url.port or 80)
53+
connection.request('PURGE', '%s?%s' % (url.path or '/', url.query), '',
54+
{'Host': url.hostname})
55+
response = connection.getresponse()
56+
if response.status != 200:
57+
logging.error('Purge failed with status: %s' % response.status)
58+
return response
59+
4660
class VarnishHandler(Telnet):
4761
def __init__(self, host_port_timeout, secret=None, **kwargs):
4862
if isinstance(host_port_timeout, basestring):
@@ -240,6 +254,11 @@ def ban_list(self):
240254
"""
241255
return self.fetch('ban.list')[1]
242256

257+
def purge_url(self, url):
258+
"""
259+
Wrapper for http_purge_url
260+
"""
261+
return http_purge_url(url)
243262

244263

245264
class ThreadedRunner(Thread):
@@ -299,16 +318,4 @@ def close(self):
299318
self.run('close', threaded=True)
300319
self.servers = ()
301320

302-
def purge_url(self, url):
303-
"""
304-
Do an HTTP PURGE of the given asset.
305-
The URL is run through urlparse and must point to the varnish instance not the varnishadm
306-
"""
307-
url = urlparse(url)
308-
connection = HTTPConnection(url.hostname, url.port or 80)
309-
connection.request('PURGE', '%s?%s' % (url.path or '/', url.query), '',
310-
{'Host': url.hostname})
311-
response = connection.getresponse()
312-
if response.status != 200:
313-
logging.error('Purge failed with status: %s' % response.status)
314-
return response
321+

0 commit comments

Comments
 (0)