Purge by python and expect

Gaute Amundsen gaute at pht.no
Sun Nov 12 16:16:41 CET 2006


Ok, I gott there first I guess :)

Quick & dirty, more like sketches in fact, 
so please post your improvements, and suggestions.

Regards
Gaute Amundsen

By expect:

#!/usr/bin/expect --

set timeout 5
log_user 0

spawn -noecho telnet localhost 90
#spawn telnet localhost 90

expect {
"Escape character is '^]'." {
    send -- "url.purge .*\r";
    expect -re "(\\d+ \\d+)(.*)PURGE"
    puts $expect_out(1,string)
    close;
    puts "DONE\r" }

"Connection refused" {
    puts "FAILED\r"}

}


by python:

#!/usr/bin/python

def by_http():
    # not complete, need to take a list of urls, and possibly a host as 
paramertes
    import httplib
    #(scheme, host, path, params, query, fragment ) = 
urlparse.urlparse(objecturl)

    conn = httplib.HTTPConnection("cache.site.com")
    conn.request("PURGE", "/foo/bar/baf")
    r1 = conn.getresponse()
    conn.close()
    return r1.status, r1.reason


def by_telnet():
    # deletes everything from all hosts. dont know if host matching is 
possible
    import telnetlib
    import socket
    #tn.set_debuglevel(10)

    try:
        tn = telnetlib.Telnet('localhost',90)
    except socket.error, e:
        return e[1]
        #socket.error: (111, 'Connection refused')

    tn.write("url.purge .*\n") # purge documets matching regexp
    (a,b,c) = tn.expect(["\d+ \d+"], 2)
    tn.close()
    return b.group(0) + "\nDONE"



More information about the varnish-misc mailing list