Refactory url_open
This commit is contained in:
parent
1702529ca8
commit
ce93d84c08
|
@ -3207,62 +3207,6 @@ def zip_content(path):
|
||||||
return names
|
return names
|
||||||
|
|
||||||
|
|
||||||
def popen(command, stdin=None):
|
|
||||||
try:
|
|
||||||
proc = subprocess.Popen(shlex.split(command), shell=IS_WIN,
|
|
||||||
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
|
||||||
for line in proc.stdout:
|
|
||||||
yield line.decode().rstrip()
|
|
||||||
except Exception as e:
|
|
||||||
error(e)
|
|
||||||
yield (e.errno, e.strerror)
|
|
||||||
|
|
||||||
|
|
||||||
def url_open(url, options={}, verify=True, json=False):
|
|
||||||
data = ''
|
|
||||||
err = ''
|
|
||||||
req = Request(url)
|
|
||||||
try:
|
|
||||||
if verify:
|
|
||||||
response = urlopen(req)
|
|
||||||
else:
|
|
||||||
context = ssl._create_unverified_context()
|
|
||||||
response = urlopen(req, context=context)
|
|
||||||
except HTTPError as e:
|
|
||||||
error(e)
|
|
||||||
err = str(e)
|
|
||||||
except URLError as e:
|
|
||||||
error(e.reason)
|
|
||||||
err = str(e.reason)
|
|
||||||
else:
|
|
||||||
if json:
|
|
||||||
data = json_loads(response.read())
|
|
||||||
else:
|
|
||||||
data = response.read()
|
|
||||||
|
|
||||||
return data, err
|
|
||||||
|
|
||||||
|
|
||||||
def run(command, wait=False):
|
|
||||||
try:
|
|
||||||
if wait:
|
|
||||||
result = subprocess.check_output(command, shell=True)
|
|
||||||
else:
|
|
||||||
p = subprocess.Popen(shlex.split(command), stdin=None,
|
|
||||||
stdout=None, stderr=None, close_fds=True)
|
|
||||||
result, er = p.communicate()
|
|
||||||
except subprocess.CalledProcessError as e:
|
|
||||||
msg = ("%s\nrun [ERROR]: output = %s, error code = %s\n"
|
|
||||||
% (command, e.output, e.returncode))
|
|
||||||
error(msg)
|
|
||||||
return False
|
|
||||||
|
|
||||||
if result is None:
|
|
||||||
return True
|
|
||||||
|
|
||||||
return result.decode()
|
|
||||||
|
|
||||||
|
|
||||||
def _zippwd(source, target, pwd):
|
def _zippwd(source, target, pwd):
|
||||||
if IS_WIN:
|
if IS_WIN:
|
||||||
return False
|
return False
|
||||||
|
|
|
@ -33,6 +33,7 @@ import re
|
||||||
import shlex
|
import shlex
|
||||||
import shutil
|
import shutil
|
||||||
import socket
|
import socket
|
||||||
|
import ssl
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -48,6 +49,8 @@ from pathlib import Path
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
from string import Template
|
from string import Template
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
from urllib.request import Request, urlopen
|
||||||
|
from urllib.error import URLError, HTTPError
|
||||||
|
|
||||||
import smtplib
|
import smtplib
|
||||||
from smtplib import SMTPException, SMTPAuthenticationError
|
from smtplib import SMTPException, SMTPAuthenticationError
|
||||||
|
@ -687,6 +690,35 @@ def get_size_screen():
|
||||||
return res.strip()
|
return res.strip()
|
||||||
|
|
||||||
|
|
||||||
|
def url_open(url, data=None, headers={}, verify=True, json=False):
|
||||||
|
data = ''
|
||||||
|
err = ''
|
||||||
|
req = Request(url)
|
||||||
|
for k, v in headers.items():
|
||||||
|
req.add_header(k, v)
|
||||||
|
try:
|
||||||
|
if verify:
|
||||||
|
if not data is None and isinstance(data, str):
|
||||||
|
data = data.encode()
|
||||||
|
response = urlopen(req, data=data)
|
||||||
|
else:
|
||||||
|
context = ssl._create_unverified_context()
|
||||||
|
response = urlopen(req, context=context)
|
||||||
|
except HTTPError as e:
|
||||||
|
error(e)
|
||||||
|
err = str(e)
|
||||||
|
except URLError as e:
|
||||||
|
error(e.reason)
|
||||||
|
err = str(e.reason)
|
||||||
|
else:
|
||||||
|
headers = dict(response.info())
|
||||||
|
data = response.read()
|
||||||
|
if json:
|
||||||
|
data = json.loads(data)
|
||||||
|
|
||||||
|
return data, headers, err
|
||||||
|
|
||||||
|
|
||||||
def _get_key(password):
|
def _get_key(password):
|
||||||
from cryptography.hazmat.primitives import hashes
|
from cryptography.hazmat.primitives import hashes
|
||||||
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
|
||||||
|
|
Loading…
Reference in New Issue