Przykładowe implementacje metod get i put w Pythonie.
import os,sys
import httplib2
import urllib
http = httplib2.Http()
urlapi = "http://www.ncbi.nlm.nih.gov/blast/Blast.cgi"
def put(q, p="blastp", db="nr", alns=100, descs=100):
data = dict(CMD="put",
QUERY=q,
PROGRAM=p,
FILTER="L",
DATABASE=db,
ALIGNMENTS=alns,
DESCRIPTIONS=descs
)
try:
resp, content = http.request(urlapi, "POST", urllib.urlencode(data))
lines = content.split("\n")
for line in lines:
line = line.strip()
if line.startswith("RID ="):
RID = line.split()[-1]
elif line.startswith("RTOE ="):
RTOE = int(line.split()[-1])
return RID, RTOE
except Exception:
print("Something has gone wrong.")
return None, None
return None, None
def get(rid, alns=100, descs=100):
data=dict(CMD="get",
RID=rid,
FORMAT_OBJECT="Alignment",
ALIGNMENT_TYPE="Pairwise",
FORMAT_TYPE="Text",
DESCRIPTIONS=str(descs),
ALIGNMENTS=str(alns),
SHOW_LINKOUT="no"
)
try:
resp, content = http.request(urlapi + "?" + urllib.urlencode(data), "GET")
lines = content.split("\n")
status = "UNKNOWN"
for line in lines:
line = line.strip()
if line == "Status=WAITING":
status = "WAITING"
break
elif line == "Status=READY":
status = "READY"
if status == "WAITING":
return status, []
elif status == "READY":
return status, lines
else:
return status, []
except Exception:
print("Something has gone wrong.")
status = "ERROR"
return status, []