Get post parameters python

How to get post and get parameters

POST operations on the other hand may encure side effects. GET operations are supposed to be safe operations, that don’t perform any side-effects (besides caching, etc), so they are easily cached by proxies and such.

Web.py: how to get POST parameter and GET parameter?

There’s actually an (undocumented?) _method parameter that can be get , post or both (the default) to return variables from the different sources. See the source for web.input(). So for example:

get_input = web.input(_method='get') post_input = web.input(_method='post') 

However, I’ve used web.py a lot, and never needed this. Why do you need to distinguish between input parameters in the query string and the data?

Javascript — how to get GET and POST variables with, Here’s something to gather all the GET variables in a global object, a routine optimized over several years. Since the rise of jQuery, it now seems appropriate to store them in jQuery itself, am checking with John on a potential core implementation.

How to get POST and GET parameters from Web Page in python

How about something like this to get you started? It pulls out forms and input attributes:

from BeautifulSoup import BeautifulSoup s = urllib2.urlopen('http://stackoverflow.com/questions/10614974/how-to-get-post-and-get-parameters-from-web-page-in-python').read() soup = BeautifulSoup(s) forms = soup.findall('form') for form in forms: print 'form action: %s (%s)' % (form['action'], form['method']) inputs = form.findAll('input') for input in inputs: print " -> %s" % (input.attrs) 
form action: /search (get) -> [(u'autocomplete', u'off'), (u'name', u'q'), (u'class', u'textbox'), (u'placeholder', u'search'), (u'tabindex', u'1'), (u'type', u'text'), (u'maxlength', u'140'), (u'size', u'28'), (u'value', u'')] form action: /questions/10614974/answer/submit (post) -> [(u'id', u'fkey'), (u'name', u'fkey'), (u'type', u'hidden'), (u'value', u'923d3d8b45bbca57cbf0b126b2eb9342')] -> [(u'id', u'author'), (u'name', u'author'), (u'type', u'text')] -> [(u'id', u'display-name'), (u'name', u'display-name'), (u'type', u'text'), (u'size', u'30'), (u'maxlength', u'30'), (u'value', u''), (u'tabindex', u'105')] -> [(u'id', u'm-address'), (u'name', u'm-address'), (u'type', u'text'), (u'size', u'40'), (u'maxlength', u'100'), (u'value', u''), (u'tabindex', u'106')] -> [(u'id', u'home-page'), (u'name', u'home-page'), (u'type', u'text'), (u'size', u'40'), (u'maxlength', u'200'), (u'value', u''), (u'tabindex', u'107')] -> [(u'id', u'submit-button'), (u'type', u'submit'), (u'value', u'Post Your Answer'), (u'tabindex', u'110')] 

How are parameters sent in an HTTP POST request?, Short answer: in POST requests, values are sent in the «body» of the request. With web-forms they are most likely sent with a media type of application/x-www-form-urlencoded or multipart/form-data.

Читайте также:  Php html background color

HTTP GET and POST parameters recommendations

Yes, your assumptions are correct. You should be consistent on how you pass your parameters or require the parameters to be passed, but it’s not going to do any harm really.

GET operations are supposed to be safe operations, that don’t perform any side-effects (besides caching, etc), so they are easily cached by proxies and such. POST operations on the other hand may encure side effects.

I would recommend reading the Wikipedia entry on HTTP protocol:

GET

Requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause. See safe methods below.

POST

Submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

There are other operations too (e.g. HEAD, PUT, DELETE), and you should consider using them if you are designing an API. These are heavily discussed in RESTful API design.

That rule is most definitely still recommended.

It’s reflected in the refresh behaviour of modern browsers. These will happily refresh with GET values, but will pop up a warning dialog on a refresh of a POST (‘are you sure you want to resubmit?’ etc.).

It looks like you were trying to combine the two methods (GET and POST) .. by POSTing to an URL with GET values. While this should work fine, it isn’t commonly done. Forms usually exclusively use either one or the other.

Yes, the semantics of GET and POST should be respected.

Given that fact, then there is often a very good reason for putting some parameters in the GET and some in the POST vars — consider the case where you have a web based script which does something like:

UPDATE datatable SET quantity=30 WHERE order=21559 

This might be represented as:

 /update?order=21559 POST data: quantity=30 

How to get POST and GET parameters from Web Page, My algorithm is like this: find in web page this type of strings ; then for each found result: get fields and construct request then save it somewhere. My purpose is to write crawler which gets all links, GET and POST parameters from web site and then save it …

How to get POST parameters with Python?

You are sending it as POST. To obtain POST variables, try replacing:

form = cgi.FieldStorage(environ="post") 
import sys, urllib query_string = sys.stdin.read() multiform = urllib.parse.parse_qs(query_string) 

How To Retrieve URL and POST Parameters with Express, First, open your terminal window and create a new project directory: mkdir express-params-example. …. Step 2 – Using req. query with URL Parameters. req. …. Step 3 – Using req. params with Routes. req. …. Step 4 – Using . param with Route Handlers. …. Step 5 – Using req. body with POST Parameters.

Источник

Python 3 urllib: making requests with GET or POST parameters

Python 3 urllib: making requests with GET or POST parameters

Learn how to make a request in Python 3 using urllib that includes either GET or POST parameters.

The urllib packages are the most common and easy way of making requests in Python and offer all the necessary functionality needed to customize and add the required information to a request including GET and POST parameters.

GET requests are generally used to just fetch information from a server, any included parameters are usually to tell the server to format the data in a certain way, these parameters should not include sensitive data. GET parameters are attached to a request via a query string at the end of a URL.

POST requests are generally used to send data to a server, included parameters can sometimes include sensitive data such as usernames and passwords, although these will also require added protection such as SSL. POST parameters are attached to a request via the request body, and are more difficult to access than GET parameters.

Making a basic request using urllib without parameters

Python’s urllib library is split up into several modules. To make a basic request in Python 3, you will need to import the urllib.request module, this contains the function urlopen() which you can use to make a request to a specified URL. To fetch the actual output of the request, you can use the read() function on the returned object to read the contents.

import urllib.request response = urllib.request.urlopen( "http://example.com" ) response_text = response.read()

The urlopen() returns an object that can be used in a context manager, this will ensure that the response object is cleaned up properly once you are finished with it, and it helps to logically separate your response handling from the rest of your code to ensure greater readability.

import urllib.request with urllib.request.urlopen( "http://example.com" ) as response: response_text = response.read() print( response_text )

The above code will return the HTML content for the site example.com.

if no data argument is given with the urlopen() function, then the request method will be GET. If the data argument is given, then the request method will be POST.

Making a request with urllib with GET parameters

The parameters that are passed alongside a GET request are done so through the query string attached to the end of a URL, so adding your own parameters does not require any special functions or classes, all you’ll need to do is make sure your query string is properly encoded and formatted.

To create your key-value pairs that will be contained in the query string, you can create a dictionary object, this object can then be encoded and formatted using urllib’s urlencode() function contained within the urllib.parse module.

import urllib.request import urllib.parse url = "http://example.com" params = < "param1": "arg1", "param2": "arg2", "param3": "arg3" > query_string = urllib.parse.urlencode( params ) url = url + "?" + query_string with urllib.request.urlopen( url ) as response: response_text = response.read() print( response_text )

The formatted string that is returned by the urlencode() function should look like this:

param1=arg1¶m2=arg2¶m3=arg3 

When connecting it to the end of your URL, don’t forget it add a ‘?’ character to signify the start of your query string. Once you’ve added the query string, the final URL should look like this:

http://example.com?param1=arg1¶m2=arg2¶m3=arg3 

Making a request with urllib with POST parameters

The parameters that are passed alongside a POST request are done so via the request body and are a generally a little more difficult to access compared to GET parameters, however, urllib does make it quite easy for us to add POST parameters.

Similarly to how we added the GET parameters, you can create a dictionary to store the key-value pairs of your POST parameters that can then be formatted using urlencode(). We must then go through the extra step of encoding the formatted string into bytes and specifying a desired character encoding.

We can then open the request as normal using urlopen() but this time we will add our data as an extra argument, this will add our parameters to the request and change the request type to POST (the default being GET).

import urllib.request import urllib.parse url = "http://example.com" params = < "param1": "arg1", "param2": "arg2", "param3": "arg3" > query_string = urllib.parse.urlencode( params ) data = query_string.encode( "ascii" ) with urllib.request.urlopen( url, data ) as response: response_text = response.read() print( response_text )

This will make a request to example.com with the 3 parameters attached to the request body.

If you have any questions feel free to leave them in the comments below!

Christopher Thornton @Instructobit 5 years ago

Источник

Оцените статью