3

My goal is to create a persistent cookie on-the-fly by supplying user id & password and use that cookie in POST request using a session object. But below code returns below exception.

('Connection aborted.', error(54, 'Connection reset by peer'))

class CreatePersistentCookie(): """This class is created to generate a persistent cookie that can further be used through out session for all the service requests being executed"""

class CreatePersistentCookie():
    """This class is created to generate a persistent cookie that can further be
       used through out session for all the service requests being executed"""

    def __init__(self, headers, data, url, params, authserver):
        self.headers = headers
        self.data = data
        self.url = url
        self.params = params
        self.authserver = authserver

    def generateCookie(self):
        with requests.session() as s:
            reqsessionObj = s.post(self.authserver,params = self.params)
            reqCookie = reqsessionObj.request.headers['Cookie'] # this returns the Cookie i need
            regexObj = re.compile(r'act-uat=\S+') # this is my app specific pattern search that returns the exact cookie text i need.
            matchObj = regexObj.search(reqCookie)
            sessionCookie = matchObj.group()
            self.headers['Cookie'] = sessionCookie # adding Cookie attribute in headers.
            try:
                r = s.post(self.url, data=json.dumps(self.data), headers=self.headers)
                return r.raise_for_status()
            except requests.exceptions.RequestException as err:
                print err

def main():

    # Defining the params variable. This contains authentication details such as user id,password & App id.
    params = {"accountId": "John",
             "accountPassword": "password",
             "appIdKey": "5c9773e36fd6ea7cc2f9f8ffd9da3e3"
            }
    # Defining the authserver variable that contains the host details where authentication happens.
    authserver = 'https://auth-uat.com/authenticate'

    # creating a object cookieObj from class CreatePersistentCookie that returns persistent cookie.
    #print cookies
    headers = {'Content-Type': 'application/json;charset=UTF-8',
                'Host':'service-uat1.com'}

    data = {"appName":"abc","appKey":"abc","type":"jdbc","queryName":"xyz","version":"v1.2","useCache":"false","bindVars":[{"bindVarName":"In_dt","bindVarVal":"2014-05-13"},{"bindVarName":"In_Location","bindVarVal":"USA"}]}
    url = 'https://uat1.com/gsf/abc/derf/abc/services/xyz'

    cookieObj = CreatePersistentCookie(headers, data, url, params, authserver)

    cookieObj.generateCookie()

if __name__ == '__main__':
    main()

3 Answers 3

4

Connection reset by peer indicates that the server you're trying to connect to is refusing the connection. Normally, there is a handshake between your computer and the website's server, but here for some reason, the server is refusing the connection. I would use the urllib, requests, mechanize, and cookielib modules (some of which only work in Python 2.7). Then, using urllib you can attach a user-client header like Firefox, which will trick the browser into accepting the connection because they will think you are a regular person surfing the web, not a robot.

Sign up to request clarification or add additional context in comments.

1 Comment

This is what solved my issue. Using requests (python package), I copied the request headers from Safari and used those headers to send the request, and stopped getting Connection Reset By Peer errors. requests.readthedocs.io/en/master/user/quickstart/… ``` headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8", "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15" } ```
1

Try the below command in terminal it worked for me

pip install requests[security]

1 Comment

could you please add an explanation what is this for? Thanks
0

In my case it worked from Postman but not from python script. Restarting the system fixed it.

Comments

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.