assignment 12.2

修改套接字程序, 统计接收到的字符数与3000个字符之后未显示的文本。该程序应检索整个文档, 统计字符总数并显示在文档结尾

代码如下:

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
import socket
import re

url = raw_input('Enter - ')
if (re.search('^http://[a-zA-Z0-9]+.[a-zA-Z0-9]+.[a-zA-Z0-9]+/',url)):
words = url.split('/')
hostname = words[2]
mysocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
mysocket.connect((hostname, 80))
except:
print(hostname, ' is not a correct server')
exit()
mysocket.send(str.encode('GET ' + url + ' HTTP/1.0nn'))
count = 0
while True:
data = mysocket.recv(3000).decode('utf-8')
if (len(data) < 1):
break
count = count + len(data)
if (count <= 3000):
print (data)
print("The total count of this web is", count)
mysocket.close()

else:
print("The URL that you input is bad format")