1

Basically i tried sending http2.0 headers with hyper for python

https://hyper.readthedocs.io/en/latest/

https://github.com/python-hyper/hyper

Mounting HTTP20Adapter in my request.session but didnt worked as expected.

First i explain thath "from tls_version import MyAdapter" thath is used later in the Main code its these tls_version.py file

from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import ssl


class MyAdapter(HTTPAdapter):
    def init_poolmanager(self, connections, maxsize, block=False):
        self.poolmanager = PoolManager(num_pools=connections,
                                       maxsize=maxsize,
                                       block=block,
                                       ssl_version=ssl.PROTOCOL_TLSv1_2)

Just to force to use tls1.2 nothing more.

The Main code is here but basically im trying to send a get call with http2.0 pseudo headers mounting hyper adapter in request.session and having control over headers order with collections.OrderectDict

import requests
from tls_version import MyAdapter
import json
import collections
from userdata import  UserData
from hyper.contrib import HTTP20Adapter


headers2 = [('Upgrade-Insecure-Requests', '1'),
        ('User-Agent', 'Mozilla/5.0 (Linux; Android 5.1.1; google Pixel 2 Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36'),
        ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3'),
        ('Accept-Encoding', 'gzip, deflate'),
        ('Accept-Language', 'es-ES,es;q=0.9,en-US;q=0.8,en;q=0.7'),

        ]
class post_calls():
        def start(self,headers_pass,body_pass,params,url,method):
            proxies = {
                'http': ip,
                'https': ip
            }
            body = str(body_pass)


            #send the POST request
            session = requests.session()
            session.mount('https://', MyAdapter())
            session.headers =  collections.OrderedDict(headers_pass)
            if method == 'get':
                q = 'https://' + server + '.' + host
                q = q.replace('.www.', '.')
                session.mount('https://', HTTP20Adapter())
                print('q='+q)
                response = session.get(url, proxies=proxies, params=params, verify=charlesproxy)





def login_world2(sid): 

    a = post_calls()
    q ='https://'+server+'.'+ host+'/login.php?mobile&sid='+sid+'&2'
    q = q.replace('.www.','.')
    params = {}
    url = q
    body = '0'
    login = a.start(headers2,body,params,url,'get')
    return login

if __name__ == "__main__":
    login_get = login_world(sid)
    print(login_get)

these is headers sends these file:

:method: GET
:scheme: https
:authority: server.url.com
:path: /login.php?mobile&sid=577f0967545d6acec94716d265dd4867fa4db4a446326ecde7486a97feede14702f4911438f4a4cd097677f0dd962786ef14b3f16f1184ee63a506155d522f53&2
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 5.1.1; google Pixel 2 Build/LMY47I; wv) AppleWebKit/537.36 (KHTML
user-agent: like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36
accept: text/html
accept: application/xhtml+xml
accept: application/xml;q=0.9
accept: image/webp
accept: image/apng
accept: */*;q=0.8
accept: application/signed-exchange;v=b3
accept-encoding: gzip
accept-encoding: deflate
accept-language: es-ES
accept-language: es;q=0.9
accept-language: en-US;q=0.8
accept-language: en;q=0.7

and these is what i need to send cause if i send them like i put above, like these script does, the server rejects my get requests.

:method: GET
:authority: server.url.com
:scheme: https
:path: /login.php?mobile&sid=2ea530a62cb63af6c14be116b7df86ad85cd77c9a11aa3c881b3a460e6c14fbd1fd8b79bd66c9782073705cdff25e890e65b5aeb852fde24c2d54a6e4ee49890&2
upgrade-insecure-requests: 1
user-agent: Mozilla/5.0 (Linux; Android 5.1.1; google Pixel 2 Build/LMY47I; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/74.0.3729.136 Mobile Safari/537.36
accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
accept-encoding: gzip, deflate
accept-language: es-ES,es;q=0.9,en-US;q=0.8,en;q=0.7

Seems thath for each "," i put in headers dict, hyper creates a new header instead of sending them in same value, witouth hyper it works normal but i need to send those headers thath are http 2.0 and witouth hyper or any other alternative i cannot, requests dosnt have support for it

:method: GET
:scheme: https
:authority: server.url.com
:path: /login.php?mobile&sid=577f0967545d6acec94716d265dd4867fa4db4a446326ecde7486a97feede14702f4911438f4a4cd097677f0dd962786ef14b3f16f1184ee63a506155d522f53&2

1 Answers1

0

Set the headers in the HTTP20Adapter instead of in session and it should work.

adapter = HTTP20Adapter(headers=headers)
session.mount(prefix='https://', adapter=adapter)
forgetso
  • 2,194
  • 14
  • 33