-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket-05.py
61 lines (49 loc) · 1.43 KB
/
socket-05.py
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
#!/usr/bin/python
#
# Universidad del valle - Octubre 10 2017
# Asignatura: Fundamentos de Redes
# Autor: Juan Felipe Orozco Escobar
# Codigo: 1426244
# Proposito: Complete el codigo, en la seccion que dice
# COMPLETE de acuerdo al enunciado dado en
# este enlace https://goo.gl/1uQqiB, item 'socket-http-client'
import socket
import sys
try: # esta estructura permite capturar comportamientos anomalos
# COMPLETE (1)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg: # si es del tipo socket.error
print "Failed to create socket. Error code: " + str(msg[0]) + ", error message: " + msg[1]
sys.exit()
print "Socket created"
host = "www.google.com"
# defina una variable port y almacene alli el numero 80
# COMPLETE (2)
port = 80
try:
# COMPLETE (3)
remote_ip = socket.gethostbyname(host)
except socket.gaierror:
print "Hostname could not be resolved. Exiting"
sys.exit()
# COMPLETE (4)
print "La direccion IP de " + host + " es " + remote_ip
# COMPLETE (5)
endpoint = (remote_ip, port)
# COMPLETE (6)
s.connect(endpoint)
print "Socket connected to " + host + " on ip " + remote_ip
# Datos a enviarse
message = "GET / HTTP/1.1\r\n\r\n"
try:
# COMPLETE (7)
s.sendall(message.encode('ascii'))
except socket.error:
print "Send failed"
sys.exit()
print "Message send successfullly"
# Recibiendo datos
# COMPLETE (8)
reply = s.recv(2048)
print reply
s.close()