Skip to content

Commit cc64732

Browse files
committed
Use requests and quote_plus
1 parent 807b8ef commit cc64732

File tree

1 file changed

+9
-30
lines changed

1 file changed

+9
-30
lines changed

web.py

Lines changed: 9 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,48 +8,27 @@
88
import urllib
99
import urllib.request
1010
import urllib.parse
11+
import requests
1112
import json as jsonlib
1213
from html.entities import html5 as name2codepoint
1314

14-
user_agent = "Mozilla/5.0 (compatible; Phenny; +https://github.com/sfan5/phenny)"
15+
user_agent = "Mozilla/5.0 (compatible; Phenny; +https://github.com/asl97/phenny)"
1516

1617
def get(uri, amount=-1):
1718
global user_agent
18-
req = urllib.request.Request(uri, headers={"User-Agent": user_agent})
19-
try:
20-
f = urllib.request.urlopen(req, cadefault=True)
21-
except urllib.error.HTTPError as e:
22-
return b"", e.code
23-
if amount > 0:
24-
content = f.read(amount)
25-
else:
26-
content = f.read()
27-
f.close()
28-
return content, f.status
19+
r = requests.get(uri, headers={"User-Agent": user_agent})
20+
return r.content, r.status_code
2921

3022
def head(uri):
3123
global user_agent
32-
req = urllib.request.Request(uri, headers={"User-Agent": user_agent}, method="HEAD")
33-
try:
34-
f = urllib.request.urlopen(req, cadefault=True)
35-
except urllib.error.HTTPError as e:
36-
return {}, e.code
37-
headers = dict(f.info().items())
38-
f.close()
39-
return headers, f.status
24+
r = requests.head(uri, headers={"User-Agent": user_agent})
25+
return r.headers, f.status_code
4026

4127

4228
def post(uri, query):
4329
global user_agent
44-
data = bytes(urllib.parse.urlencode(query), 'ascii')
45-
req = urllib.request.Request(uri, data=data, headers={"User-Agent": user_agent})
46-
try:
47-
f = urllib.request.urlopen(req, cadefault=True)
48-
except urllib.error.HTTPError as e:
49-
return b"", e.code
50-
content = f.read()
51-
f.close()
52-
return content, f.status
30+
r = requests.post(uri, data=query, headers={"User-Agent": user_agent})
31+
return r.content, f.status_code
5332

5433
def entity(match):
5534
value = match.group(1).lower()
@@ -70,5 +49,5 @@ def json(text):
7049
return jsonlib.loads(text)
7150

7251
def urlencode(text):
73-
return urllib.parse.urlencode({'a': text})[2:]
52+
return urllib.parse.quote_plus(text)
7453

0 commit comments

Comments
 (0)