-
Notifications
You must be signed in to change notification settings - Fork 0
/
pclient.py
66 lines (57 loc) · 1.83 KB
/
pclient.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
62
63
64
65
66
from socket import *
import urllib
from HTMLParser import HTMLParser
import parser
import os, time
import multiprocessing
def recv_size(sock,size):
total = ''
while True:
print 'receive image data'
data = sock.recv(8192)
total += data
if len(total)>=size:
break
return data
def requestFile(filename, sock):
sock.send(filename) # send file name
print 'send file name'
size = sock.recv(1024) # wait for server to send back filesize
if size == '0': # file not found
sock.send('FNF') # send 'fnf
print size + ' aabb'
data =sock.recv(1024)
else: #file found
sock.send('ACK') # send ack
print size + ' aabb'
data = recv_size(sock,int(size))
#receive file from server
print 'writing'
return data
serverName = 'localhost'
serverPort = 12000
clientSocket = socket(AF_INET, SOCK_STREAM)
clientSocket.connect((serverName,serverPort))
f = open('index.html','a')
data = requestFile('index.html',clientSocket) #request index
f.write(data)
f.close()
IParser = parser.parselinks() #parse index
IParser.feed(urllib.urlopen('file:///Users/jianshenhe/desktop/network/index.html').read().decode('utf-8'))
imgarr = IParser.getresult() #get all img files
newImgarr = []
for item in imgarr: # preProcess img url
item = item[1:]
newImgarr.append(item)
pool = multiprocessing.Pool(processes=5)
for img in newImgarr:
print 'down img'+img
#pool.apply_async(requestFile,(img,clientSocket,))
try:
requestFile(img,clientSocket)
except IOError as e:
print "I/O error({0}): {1}".format(e.errno, e.strerror)
pool.close()
pool.join()
print 'receive all img'
clientSocket.close()