-2

trying to send Post request with specific cookies that on my pc from get request

i searched in google then i found

opener = urllib2.build_opener() # send the cookies
opener.addheaders.append(('Cookie', cookies)) # send the cookies
f = opener.open("http://example")

this code is useful and helped me

but can someone explain it for me and tell me if f variable makes request ?

i don't need cookielib just my example :)

if i typed

url = 'http://example' # to know the values type any password to know the cookies

values = {"username" : "admin",
          "passwd" : "1",
          "lang" : "" ,
          "option" : "com_login",
          "task" : "login",
          "return" : "aW5kZXgucGhw",
          } # request with the hash

data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read()
cookies=response.headers['set-cookie'] #to get the cookies
opener = urllib2.build_opener() # send the cookies
opener.addheaders.append(('Cookie', cookies)) # send the cookies
f = opener.open("http://example.com)

What will happened two post requests !?

1
  • 1
    Read the documentation for urllib2. It will answer your questions on how the library works. If you have specific questions about how to implement logic then post a question. docs.python.org/2/library/urllib2.html
    – Josh J
    Aug 7, 2015 at 14:18

1 Answer 1

0

The request gets sent when you call the open() method on the opener object. The f variable contain a reference to the open connection in case you want to do something else to it later on (such as close it again).

Your comments that say 'send cookies' are in the wrong place, the line where you call append is just preparing the request, it only gets sent when you call open.

4
  • I'm not sure what you are asking
    – codebox
    Aug 7, 2015 at 14:09
  • ok to be clear :) first i'm trying to make one post request contain parameters and specific cookies if i added opener = urllib2.build_opener() # send the cookies opener.addheaders.append(('Cookie', cookies)) # send the cookies f = opener.open("example.com") variable f will make another request or the same request ? :)
    – smile
    Aug 7, 2015 at 14:10
  • Read the docs docs.python.org/2/library/…
    – Josh J
    Aug 7, 2015 at 14:17
  • in my example when i run the script f variable will make a request ??
    – smile
    Aug 7, 2015 at 14:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.