7
\$\begingroup\$

I have a simple python script that's repeating a HTTP response/request. I would like to know how I could have constructed this better, particularly if there's a way I could have incorporated the slice operator to cut down on my variable usage.

For context - the response contained a JSON string in between the<ExecuteResult> flags that I'm removing currently withsplit()

import requestsimport jsonfrom pprint import pprinturl = "<redacted>"cookies = {<redacted>}headers = {<redacted>}data="<redacted>"submissions = input("How many submissions to create:")while submissions > 0:    response = requests.post(url, headers=headers, cookies=cookies, data=data)    xml_string = response.content    json_file = xml_string.split('<ExecuteResult>')[1]    json_file = (json_file.split('</ExecuteResult>')[0]).replace("\\", "")    json_response = json.loads(json_file)    print "Created Id: " + json_response['_entity']['Id']    submissions -= 1
Reinderien's user avatar
Reinderien
71.2k5 gold badges76 silver badges257 bronze badges
askedFeb 13, 2018 at 7:27
Michael A's user avatar
\$\endgroup\$
1
  • \$\begingroup\$Do yourdata and/orurl actually change for the different submissions?\$\endgroup\$CommentedFeb 13, 2018 at 14:00

2 Answers2

10
\$\begingroup\$

If you are making many requests, it is worth it to have a look atrequests.Session. This re-uses the connection to the server for subsequent requests (and also the headers and cookies).

I would also start separating your different responsibilities. One might be global constants (like your header and cookies), one a function that actually does the submission, one that creates the session and does each submission and then some code under aif __name__ == '__main__': guard that executes the code.

import requestsimport jsonCOOKIES = {<redacted>}HEADERS = {<redacted>}def post(session, url, data):    """Use session to post data to url.    Returns json object with execute result"""    response = session.post(url, data=data)    json_file = response.content.split('<ExecuteResult>')[1]    json_file = (json_file.split('</ExecuteResult>')[0]).replace("\\", "")    return json.loads(json_file)def post_submisssions(url, data, n):    """Create a session and post `n` submissions to `url` using `data`."""    session = requests.Session()    session.headers.update(HEADERS)    session.cookies = COOKIES    for _ in xrange(n):        json_response = post(session, url, data)        print "Created Id:", json_response['_entity']['Id']if __name__ == "__main__":    url = "<redacted>"    data = "<redacted>"    post_submisssions(url, data, input("How many submissions to create:"))

I also added some rudimentarydocstrings.


You could also change your parsing by usingBeautifulSoup:

from bs4 import BeautifulSoupdef post(session, url, data):    """Use session to post data to url.    Returns json object with execute result"""    response = session.post(url, data=data)    soup = BeautifulSoup(response.content, 'lxml')    return json.loads(soup.find('ExecuteResult'.lower()).replace("\\", ""))
answeredFeb 13, 2018 at 14:07
Graipher's user avatar
\$\endgroup\$
7
\$\begingroup\$

I'll start with the obvious:

while submissions > 0:    ...    submissions -= 1

can be transformed into:

for _ in xrange(submissions):    ...

There are other changes that I think would make the code slightly better, albeit not necessarily more pythonic:

  • submissions should probably be namedsubmission_count (thanks,Ludisposed!).
  • You appear to be parsing HTML usingstring.split(). It might work OK now, but if you need to add more functionality in the future this approach will be very cumbersome. Try using a library instead (for example,Beautiful Soup).
  • You're not using thepprint import, so you might as well remove that line.
answeredFeb 13, 2018 at 8:05
Cristian Lupascu's user avatar
\$\endgroup\$
2
  • 3
    \$\begingroup\$submissionCount does not adhere PEP8. Functions and variables should besnake_case\$\endgroup\$CommentedFeb 13, 2018 at 8:37
  • \$\begingroup\$@Ludisposed You are right. Thanks! I edited the answer.\$\endgroup\$CommentedFeb 13, 2018 at 8:56

You mustlog in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.