«

»

Jan 15

Python: Mengirim HTTP GET Request

Berikut adalah sebuah contoh script Python untuk mengirim sebuah HTTP GET request ke sebuah aplikasi yang berjalan di localhost port 8080:

import httplib
import time

def printText(txt):
    lines = txt.split('\n')
    for line in lines:
        print line.strip()

httpServ = httplib.HTTPConnection("127.0.0.1", 8080)
httpServ.connect()

httpServ.request('GET', "/?action=command&command=walk_mode&value=1")
response = httpServ.getresponse()
if response.status == httplib.OK:
    print "Output from HTML request"
    printText (response.read())
time.sleep(3)
httpServ.request('GET', "/?action=command&command=walk_mode&value=0")
response = httpServ.getresponse()
if response.status == httplib.OK:
    print "Output from HTML request"
    printText (response.read())

httpServ.close()
print "Done"