読者です 読者をやめる 読者になる 読者になる

PycURL練習帳

Python

GET

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://127.0.0.1:7500/dev1/0/000/001/0000001253.fid")

buf = StringIO()
c.setopt(pycurl.WRITEFUNCTION, buf.write)
c.setopt(pycurl.FOLLOWLOCATION, 1)
c.setopt(pycurl.MAXREDIRS, 5)
c.setopt(pycurl.CONNECTTIMEOUT, 30)
c.setopt(pycurl.TIMEOUT, 300)
c.setopt(pycurl.NOSIGNAL, 1)
c.perform()

Get(Range)

c = pycurl.Curl()
c.setopt(pycurl.URL, "http://127.0.0.1:7500/dev2/0/000/001/0000001252.fid")

buf = StringIO()
c.setopt(pycurl.WRITEFUNCTION, buf.write)
c.setopt(pycurl.RANGE, "0-8191")
c.perform()
content = buf.getvalue()

HEAD

import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://127.0.0.1:7500/dev1/0/000/001/0000001253.fid")
c.setopt(pycurl.NOBODY, 1)
c.perform()

status = c.getinfo(pycurl.HTTP_STATUS)
content_length = c.getinfo(pycurl.CONTENT_LENGTH_DOWNLOAD)

PUT

import pycurl
import os

tmp = os.tmpfile()
content = "SPAM"
tmp.write(content)
tmp.seek(0)

c = pycurl.Curl()
c.setopt(pycurl.URL, "http://127.0.0.1:7500/dev1/0/000/001/0000001253.fid")
c.setopt(pycurl.PUT, 1)
c.setopt(pycurl.INFILE, tmp)
c.setopt(pycurl.INFILESIZE, len(content))
c.perform()