1

I have looked at some posts that suggests that to specify the port number with python request, just add it after the host as: 'google.com:443' for example (I want to make https requests port 443).

However, I am not confident what is the case if I am using requests.Session()?

In this case, I specify an adapter (code benefited from this post)

myCiphers = "AES128-SHA" +":"+  "AES256-SHA"

class myAdapter(HTTPAdapter):

  def init_poolmanager(self, *args, **kwargs):
      context = create_urllib3_context(ciphers=myCiphers)
      kwargs['ssl_context'] = context
      return super(myAdapter, self).init_poolmanager(*args, **kwargs)

  def proxy_manager_for(self, *args, **kwargs):
      context = create_urllib3_context(ciphers=defaultCiphers)
      kwargs['ssl_context'] = context
      return super(myAdapter, self).proxy_manager_for(*args, **kwargs)

myhost = 'https://google.com:443'
s = requests.Session()
s.mount(myhost,myAdapter())
response = s.get(myhost,verify=False,timeout=1)

The code seems to work, but these libraries are a bit tricky and I am afraid that this way of specifying the post number is not correct when mounting a host to an adapter.

Can an experienced person confirm please that the above method of specifying the port number with HTTP adapter mounted to a request session, and when making get request is correct?

user9371654
  • 2,160
  • 16
  • 45
  • 78

1 Answers1

0

Registered (mounted) HTTP adapters in a requests.Session() are matched, case-insensitively, by longest string prefix alone. If you want your adapter to apply to urls that start with the string https://google.com:443 then you used it correctly.

The syntax :port after a hostname is otherwise the correct way of specifying the port to use. Without :port the default for the scheme is used. For the https scheme, the default is port 443, so using :443 in the URL is redundant here.

However, when mounting an adapter, no normalisation is done, so the prefix 'https://google.com:443' would not match https://google.com/ even though both address the same hostname and port.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • No. I have many hosts but they do not start wih https://google.com. They just start with `https://` and ends with `:443`. For example, I have 'yahoo.com` and `facebook.com`. I just want to make sure the request is https:// and I want to emphasis using 443 as a port (in case if the server does not use 443 by default, I want to request to specify 443). – user9371654 Mar 14 '19 at 17:52
  • 1
    @user9371654: *in case if the server does not use 443 by default, I want to request to specify 443*: the **client** will always use port `443` when you use `https://` as the scheme in the URL and don't specify a port. It doesn't matter what port the server listens on, if they don't use port 443 then a `https://hostname/` url just won't work. – Martijn Pieters Mar 14 '19 at 17:55
  • To clarify, the hosts are many, like `facebook.com`, `google.com`, `yahoo.com` etc. I added `https://` to them because otherwise requests will throw an error. Then, I realized that I need to emphasis the port number 443. How to correctly do this with adapter and sessions (I use adapter to specify custom ciphersuites) – user9371654 Mar 14 '19 at 17:56
  • @user9371654: no, you don't need to emphasise port number 443. There is **no point** in specifying it explicitly. – Martijn Pieters Mar 14 '19 at 17:56
  • @user9371654: perhaps you are saying you don't want to accept URLs that specify a different port. That's a different problem altogether however, one that a custom transport adapter might be able to handle, just not in the prefix registration. – Martijn Pieters Mar 14 '19 at 17:59
  • ok I will remove the :port. But what about the mounting? If my hosts are going to be read from a file. I think I should change that line to: `s.mount("https://",myAdapter())` ?? Or shall I pass every host to the adapter as well? – user9371654 Mar 14 '19 at 18:00
  • You mean the mount statement becomes like this? `s.mount("https://",myAdapter())` sorry but the previous code worked although it contains mistakes, so I have to be clear. – user9371654 Mar 14 '19 at 18:04
  • `perhaps you are saying you don't want to accept URLs that specify a different port.` My URLS (domains list) does not have a port number, just plain domain name. I just want to make https requests using 443 on each one. From your answer, I understand that it will be 443 by default. – user9371654 Mar 14 '19 at 18:07
  • Sorry last question. Should I remove the `https:` from the `myhost` variable? or keep it? – user9371654 Mar 14 '19 at 18:10
  • @user9371654: You need to [keep it](https://stackoverflow.com/questions/15115328/python-requests-no-connection-adapters). – Martijn Pieters Mar 14 '19 at 18:11