Fernando Medina Corey

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Raw HTTP request parser in Python.

License

wrvenkat/request_parser

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Читайте также:  Adding python exe to path

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

request_parser is an HTTP request parsing library in Python providing an easy API to access information in HTTP requests.

Requires six and future. These can be installed as below.

pip install six pip install future

To use the library, git clone the repository and copy the library directory request_parser to a location that is present in PYTHONPATH or copy somewhere and add that location to PYTHONPATH .

Once the library is setup, it can be used as shown below.

from request_parser.http.request import HttpRequest from io import BytesIO def parse_request_example(request=None): if request is None: return # create an iterable object out of request bytes request_stream = BytesIO(request) # create an HttpRequest object for the request http_request = HttpRequest(request_stream=request_stream) # parse request header http_request.parse_request_header() # parse request body http_request.parse_request_body() # do stuff with parsed information from the HttpRequest object print "This is a request to: "+http_request.get_host()

It is also possible to use the parse() method of HttpRequest object to parse the request header and the body in a single call.

Following picture shows how the infromation contained in an HttpRequest object looks like after successfully parsing a multipart/form-data request.

alt text

For more documentation, please see the wiki.

The master branch is compatible only with Python 2.7. The python3 branch works with Python 3.5 and above.

Источник

Python Requests and Beautiful Soup — Playing with HTTP Requests, HTML Parsing and APIs

Python Requests and Beautiful Soup - Playing with HTTP Requests, HTML Parsing and APIs

Recently, while running the Redmond Python Meetup I’ve found that a great way to get started using Python is to pick a few common tools to start learning. Naturally, I gravitated towards teaching the basics of one of the most popular Python packages — Requests. I’ve also found it’s useful to throw in using Beatiful Soup to show folks how they can efficiently interact with HTML data after getting an HTML page.

Sound interesting? Let’s look at what I typically cover — including a few basic examples of how you can use Requests to make HTTP GET and POST requests.

Step one — Get Requests and Beautiful Soup

Despite being incredibly popular, Requests is not in Python’s standard library. In order to get requests you should use pip. For this guide, I’ll be assuming that you have a version of Python 3 and pip installed.

If you want to use a Python virtual environment you can make yourself a project directory somewhere, navigate to that project in your terminal, command prompt or powershell and then run python -m venv my_venv and source my_venv/bin/activate . Windows users will need to run my_venv\Scripts\activate.bat .

When you’re ready to install Requests you can use pip install requests from in your terminal, command prompt, or powershell.

To install Beautiful Soup, you can run pip install beautifulsoup4 in the same place.

Getting Started with Requests

Next up, we’ll use requests in the python interpreter. Go ahead and run python to get into the interpreter and we’ll start using it.

In general, if you get any errors running python you may want to try running the commands with python3 instead of python or making sure you have Python installed

Start by importing the requests library and making a simple GET request to the URL for this blog (Remember that the »> isn’t something you’re typing).

>>> import requests >>> requests.get('https://www.fernandomc.com/') Response [200]> 

After running these commands, and assuming you see the same I do, this means my website is still functional and serving content (phew!). What this is essentially saying is that the site responded with a HTTP 200 OK response code. You can read more about HTTP codes here.

But this is kinda boring. We probably wanted to actually see the content of the site. So how can we get this? Well, requests actually makes this pretty easy. Let’s try making the same request again, except this time we’ll store the result in a variable called r .

>>> import requests >>> r = requests.get('https://www.fernandomc.com/') >>> r.text ' \n

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