Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
/thttpPublic

A lightweight wrapper around urllib

License

MIT, Unlicense licenses found

Licenses found

MIT
LICENSE.md
Unlicense
UNLICENSE.md
NotificationsYou must be signed in to change notification settings

sesh/thttp

thttp is a single file, lightweight, well-tested wrapper aroundurllib that's intended to be copied directly into your project.

It's features include:

  • Making GET, POST, PATCH, PUT, HEAD and OPTIONS requests
  • Sending query parameters with your request
  • Encoding JSON payloads for POST, PATCH and PUT requests
  • Encoding form-encoded payloads for POST, PATCH and PUT request
  • Sending custom headers with any request
  • Disabling SSL certificate verification for local testing / corporate proxies
  • Following (or not following) redirects
  • Sending through a CookieJar object that can be reused between requests
  • Authenticating with HTTP basic auth
  • Specifying a custom timeout for your request
  • Decompressing gzipped content in the response
  • Loading JSON from the response
  • Returning error responses instead of throwing exceptions fromurllib
  • pretty() function for printing responses
  • Ability to upload files using the{"file": open("file.png", "rb")} style

Future features:

  • Better detection of JSON responses
  • Improve handling of non-utf-8 requests
  • Improve handling of non-utf-8 responses

Note: this project is not intended to solve all use cases that can be achieved with urllib, requests, httpx, or other HTTP libraries. The intent is to provide a lightweight tool that simplifies some of the most common use cases for developers.

Installation

copythttp.py directly into your project:

curl https://raw.githubusercontent.com/sesh/thttp/main/thttp.py > thttp.py

Or, install withpip:

python3 -m pip install thttp

Basic Usage

See the tests for examples of usage, but, effectively it's as simple as:

fromthttpimportrequestresponse=request("https://httpbingo.org/get",params={"data":"empty"})response.json# {'args': {'data': ['empty']}, 'headers': {'Accept-Encoding': ['identity'], 'Fly-Client-Ip': ['45.76.105.111'], 'Fly-Forwarded-Port': ['443'], 'Fly-Forwarded-Proto': ['https'], 'Fly-Forwarded-Ssl': ['on'], 'Fly-Region': ['hkg'], 'Fly-Request-Id': ['01F6P2WQAY1NGPRDCXV9H60XW5'], 'Host': ['httpbingo.org'], 'User-Agent': ['Python-urllib/3.8'], 'Via': ['1.1 fly.io'], 'X-Forwarded-For': ['45.76.105.111, 77.83.142.42'], 'X-Forwarded-Port': ['443'], 'X-Forwarded-Proto': ['https'], 'X-Forwarded-Ssl': ['on'], 'X-Request-Start': ['t=1622091390302198']}, 'origin': '45.76.105.111, 77.83.142.42', 'url': 'https://httpbingo.org/get?data=empty'}response.status# 200

Running the tests

> python3 -m unittest thttp.py

And to check the coverage:

> coverage run -m unittest thttp.py> coverage html&& open htmlcov/index.html

Runblack before committing any changes.

> black thttp.py

Packaging for release

rm dist/*python3 -m buildpython3 -m twine upload dist/*

License

This code is currently released under theUNLICENSE and considered public domain.If more appropriate for your usage you may consider this project released under theMIT License.

About

A lightweight wrapper around urllib

Topics

Resources

License

MIT, Unlicense licenses found

Licenses found

MIT
LICENSE.md
Unlicense
UNLICENSE.md

Security policy

Stars

Watchers

Forks

Languages


[8]ページ先頭

©2009-2025 Movatter.jp