Skip to content

Commit

Permalink
Remove requests dependency
Browse files Browse the repository at this point in the history
Use urllib.request to replace requests.
Users no longer need to install an extra Python package to get it run.
  • Loading branch information
taoky committed Dec 4, 2023
1 parent 0fbeee8 commit 2f1e7dd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A script for speed testing for each mirrors providing mirrorz service.

## How to use

Make sure you have `curl` installed. Also at least Python 3.5 is required, and `requests` package is installed.
Make sure you have `curl` installed. Also at least Python 3.5 is required.

Use

Expand All @@ -24,8 +24,8 @@ If you get stuck when loading meta data, you may issue `Ctrl+C` to skip one mirr

## Proxy

This script sends two kinds of http request: python `requests` to get metadata and `curl` to do actual speed test.
This script sends two kinds of http request: python `urllib.request` (built-in) to get metadata and `curl` to do actual speed test.

Some sites provide their meta data (`mirrorz.json`) in a place that is hard to access, which you may use a proxy. In this case you may set `HTTP_PROXY` or `HTTPS_PROXY`, which is used by python `requests`.
Some sites provide their meta data (`mirrorz.json`) in a place that is hard to access, which you may use a proxy. In this case you may set `HTTP_PROXY` or `HTTPS_PROXY`, which is used by python `urllib.request`.

In some settings you may use a proxy to do actual downloading, in this case you may set `http_proxy` or `https_proxy`. Note that these lowercase envs are also read by python `requests`.
In some settings you may use a proxy to do actual downloading, in this case you may set `http_proxy` or `https_proxy`. Note that these lowercase envs are also read by python `urllib.request`.
16 changes: 10 additions & 6 deletions oh-my-mirrorz.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# vim: expandtab ts=4 sts=4 sw=4

import subprocess
import requests
import sys
import urllib.request
import json
import os
import argparse

Expand Down Expand Up @@ -45,10 +45,14 @@ def check_curl():
return -1

def site_info(url):
return requests.get(url,
headers={
'User-Agent': 'oh-my-mirrorz/%s (+https://github.com/mirrorz-org/oh-my-mirrorz) %s %s' % (VERSION, UA_URL, requests.utils.default_user_agent())
}, timeout=10).json()
user_agent = 'oh-my-mirrorz/%s (+https://github.com/mirrorz-org/oh-my-mirrorz) %s %s' % (VERSION, UA_URL, "urllib/" + urllib.request.__version__)
headers = {
'User-Agent': user_agent
}

request = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(request, timeout=10) as response:
return json.loads(response.read().decode('utf-8'))


def speed_test(url, args):
Expand Down

0 comments on commit 2f1e7dd

Please sign in to comment.