how to send cookies inside post request

2024/10/14 21:17:29

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

#! /usr/bin/python
import re #regex
import urllib
import urllib2
#get request 
x = urllib2.urlopen("http://www.example.com) #GET Request
cookies=x.headers['set-cookie'] #to get the cookies from get request url = 'http://example' # to know the values type any password to know the cookies 
values = {"username" : "admin","passwd" : password,"lang" : "" ,"option" : "com_login","task" : "login","return" : "aW5kZXgucGhw" }data = urllib.urlencode(values)
req = urllib2.Request(url, data)
response = urllib2.urlopen(req)
result = response.read() 
cookies=response.headers['set-cookie'] #to get the last cookies from post req in this variable

then i searched in google

how to send cookies inside same post request and found

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

but i don't exactly where should i type it in my code

what i need to do exactly is to

send GET request, put the cookies from the request in variable,then make post request with the value that i got from the GET request

if anyone know answer i need edit on my code

Answer

Just create a HTTP opener and a cookiejar handler. So cookies will be retrieved and will be passed together to next request automatically. See:

import urllib2 as net
import cookielib
import urllib   cookiejar = cookielib.CookieJar()
cookiejar.clear_session_cookies()
opener = net.build_opener(net.HTTPCookieProcessor(cookiejar))data = urllib.urlencode(values)
request  = net.Request(url, urllib.urlencode(data))
response = opener.open(request)

As opener is a global handler, just make any request and the previous cookies sent from previous request will be in the next request (POST/GET), automatically.

https://en.xdnf.cn/q/117905.html

Related Q&A

Flask-Uploads gives AttributeError?

from flask import Flask from flask.ext.uploads import UploadSet, configure_uploads, IMAGESapp = Flask(__name__)app.config[UPLOADED_PHOTOS_DEST] = /home/kevin photos = UploadSet(photos, IMAGES)configure…

Python: Alternate way to covert from base64 string to opencv

Im trying to convert this string in base64 (http://pastebin.com/uz4ta0RL) to something usable by OpenCV. Currently I am using this code (img_temp is the string) to use the OpenCV functions by convertin…

Move 3D plot to avoid clipping by margins

Im trying to figure out how I can get the 3D matplotlib images below to plot higher on the canvas so it doesnt get clipped. Here is the code Im using to create the plot. I couldnt find a way to attach …

HTML Link parsing using BeautifulSoup

here is my Python code which Im using to extract the Specific HTML from the Page links Im sending as parameter. Im using BeautifulSoup. This code works fine for sometimes and sometimes it is getting st…

XML format change using XSL file in a Python code

Have written a Python code to transform a XML file to a particular format using XSL stylesheet. Python code below:#!/usr/bin/env python # -*- coding:utf-8 -*- from lxml import etree def transform(xmlP…

How to extract a specific value from a dictionary in python

I want to extract distance from google distance matrix API in Python. The objet it returned is a python dictionary.{destination_addresses: [Mumbai, Maharashtra, India ],origin_addresses: [Powai, Mumbai…

label on top of image in python

I am trying to display text on top of an image. Right now the text is below the image or if I put a row=0 in the grid disappears. I am assuming it is behind the image. I cant seem to get it to work. My…

plot multiple graphs from multiple files gnuplot

I have a set of files named like this:qd-dPZ-z1-1nn.dat qd-dPZ-z2-1nn.dat qd-dPZ-z4-1nn.dat qd-dPZ-z8-1nn.dat qd-dPZ-z16-1nn.dat qd-dPZ-z32-1nn.dat qd-dPZ-z1-2nn.dat qd-dPZ-z2-2nn.dat qd-dPZ-z4…

Python writing to CSV... TypeError: coercing to Unicode: need string or buffer, file found

outputList is a list of lists. [ [a,b,c], [d,e,f], [g,h,i] ] and I want to output it to a csv file with each list as a separate row. Im getting this error TypeError: coercing to Unicode: need string or…

Preserve Signature in Decorator python 2

I am writing a decorator which will catch TypeError for incorrect number of arguments in a function call and will print a customised message. The code is here:import inspectdef inspect_signature(f):def…