Skip to content

4.0.1

Latest
Compare
Choose a tag to compare
@ClericPy ClericPy released this 09 Nov 16:18
  1. add webapp route for /request_get to send get request with headers,proxy
# ======================= server code ===========================
from inspect import getsource

import requests


# 1. request_get demo
print(
    requests.get(
        "http://127.0.0.1:8009/chrome/request_get",
        params={
            "__url": "http://httpbin.org/get?a=1",  # [required] target URL
            "__proxy": "http://127.0.0.1:1080",  # [optional]
            "__timeout": "10",  # [optional]
            "my_query": "OK",  # [optional] params for target URL
        },
        # headers for target URL
        headers={
            "User-Agent": "OK",
            "my_header": "OK",
            "Cookie": "my_cookie1=OK",
        },
        # cookies={"my_cookie2": "OK"}, # [optional] cookies for target URL if headers["Cookie"] is None
    ).text,
    flush=True,
)
# <html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">{
#   "args": {
#     "a": "1", 
#     "my_query": "OK"
#   }, 
#   "headers": {
#     "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7", 
#     "Accept-Encoding": "gzip, deflate", 
#     "Cookie": "my_cookie1=OK", 
#     "Host": "httpbin.org", 
#     "My-Header": "OK", 
#     "Upgrade-Insecure-Requests": "1", 
#     "User-Agent": "OK", 
#     "X-Amzn-Trace-Id": "Root=1-654d0664-74457b4317c2f41d17b6a823"
#   }, 
#   "origin": "1.1.1.1", 
#   "url": "http://httpbin.org/get?a=1&amp;my_query=OK"
# }
# </pre></body></html>
# '"Herman Melville - Moby-Dick"'
# "<html><head><meta name=\"color-scheme\" content=\"light dark\"></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n  \"origin\": \"103.171.177.94\"\n}\n</pre></body></html>"


# 2. test tab_callback
async def tab_callback(self, tab, data, timeout):
    await tab.set_url(data["url"], timeout=timeout)
    return (await tab.querySelector("h1")).text


r = requests.post(
    "http://127.0.0.1:8009/chrome/do",
    json={
        "data": {"url": "http://httpbin.org/html"},
        "tab_callback": getsource(tab_callback),
        "timeout": 10,
    },
)
print(repr(r.text), flush=True)
'"Herman Melville - Moby-Dick"'


async def tab_callback(task, tab, data, timeout):
    await tab.wait_loading(3)
    return await tab.html


# 3. incognito_args demo
print(
    requests.post(
        "http://127.0.0.1:8009/chrome/do",
        json={
            "tab_callback": getsource(tab_callback),
            "timeout": 10,
            "incognito_args": {
                "url": "http://httpbin.org/ip",
                "proxyServer": "http://127.0.0.1:1080",
            },
        },
    ).text
)
# "<html><head><meta name=\"color-scheme\" content=\"light dark\"></head><body><pre style=\"word-wrap: break-word; white-space: pre-wrap;\">{\n  \"origin\": \"103.171.177.94\"\n}\n</pre></body></html>"