forked from epequeno/python-for-informatics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.py
More file actions
39 lines (33 loc) · 975 Bytes
/
02.py
File metadata and controls
39 lines (33 loc) · 975 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 24 15:58:54 2014
@author: Estevan Adrian Pequeno
"""
'''
Change your socket program so that it counts the number of characters it has
received and stops displaying any text after it has shown 3000 characters.
The program should retrieve the entire document and count the total number
of characters and display the count of the number of characters at the end
of the document.
'''
import socket
import os
# user_url = raw_input("Enter url: ")
user_url = 'http://www.py4inf.com/code/romeo.txt'
try:
host_name = user_url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host_name, 80))
mysock.send('GET ' + user_url + ' HTTP/1.0\n\n')
except:
print "Please enter a valid URL"
os.sys.exit(1)
count = 0
while True:
data = mysock.recv(512)
count += len(data)
if (len(data) < 1) or count >= 3000:
break
print data
mysock.close()
print count