如何知道我的自定义HTTP标头是否正在传递? [英] How to know if my Custom HTTP headers are being passed?

查看:95
本文介绍了如何知道我的自定义HTTP标头是否正在传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,一段时间以来,我一直在努力传递自定义HTTP标头。

So, I have been struggling with passing custom HTTP header for some time now.

我正在创建一个脚本(Python)以打开带有自定义标头的URL,例如

I am creating a script (Python) to Open a URL with Custom headers like

    {'Referer': 'https://google.com', 'X-Forwarded-For': '47.29.76.109', 

'User-Agent': 'Mozilla/5.0 (Linux; Android 7.1.1; CPH1723 Build/N6F26Q; 

wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/67.0.3396.87 

Mobile Safari/537.36', 'existing_proxy_port_to_use': '8090'}

我一直在使用BrowserMob-Proxy,但是当我尝试在Google Chrome浏览器中检查网络字段时看不到效果。

I have been using BrowserMob-Proxy for this but I am unable to see the effect when I try checking Network field in Inspect of Google Chrome.

代码:

def automation():
    headers = pd.read_excel('Database/header.xlsx')
    for i in range(0,headers.shape[0]):
        dict = {}
        header = headers.loc[i]
        dict['Referer'] = header['Referrer']
        dict[header['Option']] = header['IP']
        dict['User-Agent'] = header['USERAGENT']
        dict['existing_proxy_port_to_use'] = "8090"
        print(dict)

        URL = 'xyz'
        data = pd.read_csv('Database/data.csv')
        server = Server(path="./browsermob-proxy/bin/browsermob-proxy", options=dict)
        server.start()
        proxy = server.create_proxy()
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) #Configure chrome options
        driver = webdriver.Chrome(chrome_options=chrome_options,executable_path='/home/.../chromedriver')
        proxy.new_har("google")
        for j in range(0,data.shape[0]):
            datum = data.loc[j]
            print(datum)
            driver.get(URL)
        driver.quit()
        server.stop()   
    return None    

automation()

我正在从头文件中读取这些参数,并使用Selenium填写Google表单。

I am reading these Parameters from header file and using Selenium to fill the Google Form.

因此,请帮助我了解如何传递标题正确以及如何知道它们是否正常工作。

So, Please help me in knowing how to pass the headers correctly and how to know if they are working.

推荐答案

我通过删除Browsermob-proxy解决了传递标题的问题而不是使用seleniumwire并使用其 driver._client.set_header_overrides(headers = dict_headers)覆盖默认的HTTP标头。

I solved the problem of passing the header by removing the Browsermob-proxy and instead using seleniumwire and use its driver._client.set_header_overrides(headers=dict_headers) to override the default HTTP headers.

def automation():

    headers = pd.read_excel('Database/header.xlsx')
    data = pd.read_csv('Database/data.csv')

    for i in range(0,headers.shape[0]):
        dict_headers = {}
        header = headers.loc[i]
        dict_headers['Referer'] = header['Referrer']
        dict_headers[header['Option']] = header['IP']
        dict_headers['User-Agent'] = header['USERAGENT']

        URL = 'xyz'

        user_agent = "user-agent="+header['USERAGENT']
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument(user_agent) 
        driver = webdriver.Chrome(
                chrome_options=chrome_options,
                executable_path='/home/.../chromedriver')

        driver._client.set_header_overrides(headers=dict_headers)
        datum = data.loc[i]
        driver.get(URL)
        driver.quit()
    return None    
automation()

这篇关于如何知道我的自定义HTTP标头是否正在传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆