File: pyi_lib_requests.py

package info (click to toggle)
pyinstaller 6.13.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,520 kB
  • sloc: python: 41,347; ansic: 11,334; makefile: 176; sh: 136; xml: 19
file content (100 lines) | stat: -rw-r--r-- 3,112 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# -----------------------------------------------------------------------------
# Copyright (c) 2014-2023, PyInstaller Development Team.
#
# Distributed under the terms of the GNU General Public License (version 2
# or later) with exception for distributing the bootloader.
#
# The full license is in the file COPYING.txt, distributed with this software.
#
# SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
# -----------------------------------------------------------------------------
import socket

try:
    import BaseHTTPServer
    import SimpleHTTPServer
except ImportError:
    import http.server as BaseHTTPServer
    import http.server as SimpleHTTPServer

import os
import ssl
import sys
import threading
import time

import requests
"""
Note: to re-create the server.pem file use the following commands:

cd /path/to/pyinstaller.git/tests/functional
openssl req -new -x509 -keyout data/requests/server.pem \
    -text -out data/requests/server.pem -days 36500 \
    -nodes -config data/requests/openssl.conf
"""

if getattr(sys, 'frozen', False):
    # We are running in a |PyInstaller| bundle.
    basedir = sys._MEIPASS
else:
    # We are running in a normal Python environment.
    basedir = os.path.dirname(__file__)

SERVER_CERT = os.path.join(basedir, "server.pem")

if not os.path.exists(SERVER_CERT):
    raise SystemExit('Certificate-File %s is missing' % SERVER_CERT)


def main():

    SERVER_PORT = 8443
    httpd = None

    # Since unit tests run in parallel, the port may be in use, so retry creating the server while incrementing
    # the port number.
    while SERVER_PORT < 8493:  # Max 50 retries
        try:
            # SSL server copied from here: http://www.piware.de/2011/01/creating-an-https-server-in-python/
            httpd = BaseHTTPServer.HTTPServer(('localhost', SERVER_PORT), SimpleHTTPServer.SimpleHTTPRequestHandler)
        except socket.error as e:
            if e.errno == 98:  # Address in use
                SERVER_PORT += 1
                continue
            else:
                # Some other socket.error
                raise
        else:
            # Success
            break
    else:
        # Did not break from loop, so we ran out of retries
        assert False, "Could not bind server port: all ports in use."

    # Wrap the server socket using SSL context. The old `ssl.wrap_socket` was deprecated in python 3.7 and removed
    # in python 3.12.
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    ssl_context.load_cert_chain(certfile=SERVER_CERT, keyfile=None)
    httpd.socket = ssl_context.wrap_socket(
        httpd.socket,
        server_side=True,
    )

    def ssl_server():
        httpd.serve_forever()

    # Start the SSL server
    thread = threading.Thread(target=ssl_server)
    thread.daemon = True
    thread.start()

    # Wait a bit for the server to start
    time.sleep(1)

    # Use requests to get a page from the server
    requests.get("https://localhost:{}".format(SERVER_PORT), verify=SERVER_CERT)
    # requests.get("https://github.com")


if __name__ == '__main__':
    main()