2

I'm trying to use http://www.camp.bicnirrh.res.in/featcalc/ to POST multipart/form-data via python 2.7. Specifically, I'm uploading a file (called 'Practice.txt') in FASTA format basically this format:

'>1(ENTER)STRINGOFSPECIFICCAPITALLETTERS' 

to this site which also has a text box where you can also enter data manually (which I'm leaving blank). This data site also has checkbox options, of which I want to select 'Length', 'Net Charge', 'Aliphatic Index' and 'Hydrophobicity'. At the bottom of the page there is a 'Submit' button.
Currently, this is the code I've used for my POST response.

files = {'file': ('Practice.txt', open('Practice.txt', 'rb'))}
data = {'Length':'Length', 'Net Charge':'Net Charge', 'Aliphatic Index':'Aliphatic Index','Hydrophobicity':'Hydrophobicity'}
r = requests.post(url, files=files, data=data)
r.text

The problem is that none of this is coming back with any data when I do r.text. The website computes the values for all of these things when using the browser. I got WireShark, and I've been trying to look at the live feeds to see what exactly I'm sending to the server, and although I'm using the code above verbatim, it is not giving back the values that the browser would.

Does anyone have any ideas about why this might be happening/how to actually get data? Thanks for any input!

1 Answer 1

3

This will works:

import requests
import urllib

session = requests.Session()

file={'file':(open('practice.txt','r').read())}

url = 'http://www.camp.bicnirrh.res.in/featcalc/tt1.php'

payload = {
  'length'   :'length',     #Length
  'netcharge':'netcharge',  #Net Charge
  'aliphatic':'aliphatic',  #Aliphatic Index
  'gravy'    :'gravy'       #Hydrophobicity
  }

raw = urllib.urlencode(payload)

response = session.post(url, files=file, data=payload)

print(response.text)

All options:

payload = {
  'length'     :'length',      #Length
  'netcharge'  :'netcharge',   #Net Charge
  'amino'      :'amino',       #Amino acid composition
  'aliphatic'  :'aliphatic',   #Aliphatic Index
  'instability':'instability', #Instability Index
  'gravy'      :'gravy',       #Hydrophobicity
  'sec'        :'sec'          #Secondary Structure Propensity
  }
Sign up to request clarification or add additional context in comments.

2 Comments

@Shay Glad to have been of help! Feel free to accept my answerif you feel it was useful to you. :-) (meta.stackoverflow.com/questions/5234/…)
Thanks again for before. I was hoping you could help me out one more time with another POST request that should be identical to the one above, but for some reason it's not. I did everything above with slight changes - import requests, import urllib, session = requests.Session(), file={'file':(open('Bishop/newdenovo2.txt','r').read())}, url = 'camp.bicnirrh.res.in/predict/hii.php' payload = {"algo[]":"svm"} (since I just want the first checkbox item 'SVM'), the raw and response is identical to the one above. I can post this as a question too! Thank you again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.