Skip to content

Instantly share code, notes, and snippets.

@MindPatch
Last active January 24, 2021 00:42
Show Gist options
  • Save MindPatch/91e4e1da68fedd29ab40f95e9af02c6a to your computer and use it in GitHub Desktop.
Save MindPatch/91e4e1da68fedd29ab40f95e9af02c6a to your computer and use it in GitHub Desktop.
http requests class
#!/usr/bin/env python3
# author: Khaled Nassar @knassar702
# from scant3r project - https://github.com/knassar702/scant3r
from requests import get,post,put,packages # http request module
from fake_useragent import UserAgent # random useragents module
from requests_toolbelt.utils import dump # dump request/response body/header
import sys # system module
ua = UserAgent(verify_ssl=False,use_cache_server=True)
packages.urllib3.disable_warnings()
class NewRequest:
def __init__():
pass
"""
setup your connection settings
cookies/timeout/proxy
follow redirects / custom headers / dump the request & response
"""
def Setup(redirect=False,dump=None,cookie=None,header={},timeout=None,proxies=None,random_agents=None):
global cookies,headers,Dump,Timeout,random_Agents,allow_redirects,proxy
allow_redirects = redirect
cookies = cookie
headers = header
Dump = dump
proxy = proxies
random_Agents = random_agents
Timeout = timeout
return {
'redirect':allow_redirects,
'cookies':cookies,
'headers':headers,
'timeout':Timeout,
'proxy':proxy,
'dump':Dump,
'random_agents':random_Agents
}
# update connection settings
def Update(redirect=False,dump=None,cookie=None,header={},timeout=None,proxies=None,random_agents=None):
global allow_redirects,cookies,Dump,Timeout,random_Agents,proxy,headers
update = {}
if dump:
Dump = dump
update['dump'] = Dump
if redirect:
allow_redirects = redirect
update['redirect'] = redirect
if cookie:
cookies = cookie
update['cookies'] = cookie
if header:
headers = header
update['headers'] = header
if proxies:
proxy = proxies
update['proxy'] = proxies
if random_agents:
random_Agents = random_agents
update['random_agents'] = random_agents
if timeout:
Timeout = timeout
update['timeout'] = timeout
return update
# dump all settings
def Dump():
return {
'redirect':allow_redirects,
'cookies':cookies,
'headers':headers,
'timeout':Timeout,
'proxy':proxy,
'dump':Dump,
'random_agents':random_Agents
}
# send http request with GET method
def Get(url):
try:
if random_Agents:
headers['User-agent'] = ua.random
r = get(url,allow_redirects=allow_redirects,cookies=cookies,headers=headers,timeout=Timeout,proxies=proxy,verify=False)
if Dump:
print(dump.dump_all(r).decode('utf-8'))
return r
except:
return 0
# send http request with POST method
# Post(URL,BODY)
def Post(url,data):
try:
if random_Agents:
headers['User-agent'] = ua.random
r = post(url,allow_redirects=allow_redirects,cookies=cookies,headers=headers,timeout=Timeout,data=data,proxies=proxy,verify=False)
if Dump:
print(dump.dump_all(r).decode())
return r
except:
return 0
# send http request with PUT method
# Put(URL,BODY)
def Put(url,data):
try:
if random_Agents:
headers['User-agent'] = ua.random
r = put(url,allow_redirects=allow_redirects,cookies=cookies,headers=headers,timeout=Timeout,data=data,proxies=proxy,verify=False)
if Dump:
print(dump.dump_all(r).decode())
return r
except:
return 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment