1
1
import socket
2
+ import json
3
+ import ssl
2
4
from http_parser import parse_http_url ,parse_http_response
3
5
from exceptions import NotConnection
4
6
5
7
# GLOBAL VARIABLES
6
8
_versionHttp = 'HTTP/1.1'
7
- default_port = 80
9
+ default_http_port = 80
10
+ default_https_port = 443
8
11
9
12
class HttpClient :
10
13
11
- def __init__ (self ,host , port :int , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT , blocksize = 8192 ):
14
+ def __init__ (self ,host , port :int ,use_https , timeout = socket ._GLOBAL_DEFAULT_TIMEOUT , blocksize = 8192 ):
12
15
13
16
self .host = host
14
17
self .port = port
18
+ self .use_https = use_https
15
19
self .timeout = timeout
16
20
self .mySocket = None
17
21
self .blocksize = blocksize
18
22
19
23
# se hace uso del protocolo TCP/IP
20
24
def connect (self ):
21
25
# se establece una conexion TCP con el servidor
22
- self . mySocket = socket .create_connection ((self .host ,self .port ),self .timeout )
26
+ raw_mySocket = socket .create_connection ((self .host ,self .port ),self .timeout )
23
27
28
+ if self .use_https :
29
+ # si es https se envuelve el socket en una conexion TLS/SSL
30
+ context = ssl .create_default_context ()
31
+ self .mySocket = context .wrap_socket (raw_mySocket ,server_hostname = self .host )
32
+
33
+ else : # si es HTTP
34
+ self .mySocket = raw_mySocket
24
35
25
36
def request (self ,method ,url ,body = "" , headers = None ):
26
37
# construye y envia una solicitud HTTP
@@ -70,8 +81,9 @@ def final_request (method="GET",url="/",headers = None,body =""):
70
81
# extraer la informacion de la URL
71
82
host ,port ,path ,query = parse_http_url (url )
72
83
84
+ use_https = (port == default_https_port ) or url .startswith ("https://" )
73
85
# crear una instancia de HttpClient para establecer una conexion
74
- conn = HttpClient (host ,port )
86
+ conn = HttpClient (host ,port , use_https )
75
87
76
88
data = None
77
89
@@ -90,3 +102,49 @@ def final_request (method="GET",url="/",headers = None,body =""):
90
102
# devuelve la respuesta parseada
91
103
return data
92
104
105
+
106
+ if __name__ == "__main__" :
107
+
108
+ # Case 1 : My API - DownTrack
109
+ host = "http://localhost:5217"
110
+ endpoint = "/api/Authentication/register"
111
+
112
+ body = json .dumps ({
113
+ "id" :33590 ,
114
+ "name" : "User_335" ,
115
+ "userName" : "username_33590" ,
116
+ "email" : "example3@gmail.com" ,
117
+ "password" : "Password_333!" ,
118
+ "userRole" : "Technician" ,
119
+ "specialty" : "mechanic" ,
120
+ "salary" : 19090 ,
121
+ "expYears" : 10 ,
122
+ "departamentId" : 1 ,
123
+ "sectionId" : 1
124
+ })
125
+
126
+ headers = {
127
+ "Content-Type" : "application/json"
128
+ }
129
+
130
+ response = final_request ("POST" , f"{ host } { endpoint } " , headers = headers , body = body )
131
+
132
+ print ("Código de estado:" , response .code )
133
+ print ("Encabezados:" , response .headers )
134
+ print ("Cuerpo:" , response .body [:500 ])
135
+
136
+ # Case 2: HTTPS
137
+
138
+ response = final_request ("GET" ,"https://reqres.in/api/users?page=2" , headers = {}, body = "" )
139
+
140
+ print ("Código de estado:" , response .code )
141
+ print ("Encabezados:" , response .headers )
142
+ print ("Cuerpo:" , response .body [:500 ])
143
+
144
+ # Case 3: HTTPS
145
+ response = final_request ("GET" ,"https://jsonplaceholder.typicode.com" , headers = {}, body = "" )
146
+
147
+ print ("Código de estado:" , response .code )
148
+ print ("Encabezados:" , response .headers )
149
+ print ("Cuerpo:" , response .body [:500 ])
150
+
0 commit comments