-
Notifications
You must be signed in to change notification settings - Fork 3
/
fn_recv.py
63 lines (52 loc) · 1.79 KB
/
fn_recv.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
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import socket
import threading
import time
import sys
import os
import struct
def socket_service(own_addr, own_port, dir_path):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((own_addr, own_port))
s.listen(10)
except socket.error as msg:
print(msg)
sys.exit(1)
print('Waiting connection...')
while 1:
conn, addr = s.accept()
t = threading.Thread(target=deal_data, args=(conn, addr, dir_path))
t.start()
def deal_data(conn, addr, dir_path):
print('Accept new connection from {0}'.format(addr))
#conn.settimeout(500)
conn.send(b'Hi, Welcome to the server!')
while 1:
fileinfo_size = struct.calcsize('128sl')
buf = conn.recv(fileinfo_size)
if buf:
filename, filesize = struct.unpack('128sl', buf)
fn = str(filename.strip(bytes('\00'.encode('utf-8'))))[2:-1]
new_filename = os.path.join(dir_path,fn)
print('file new name is {0}, filesize if {1}'.format(new_filename,
filesize))
recvd_size = 0
fp = open(new_filename, 'wb')
print('start receiving...')
while not recvd_size == filesize:
if filesize - recvd_size > 1024:
data = conn.recv(1024)
recvd_size += len(data)
else:
data = conn.recv(filesize - recvd_size)
recvd_size = filesize
fp.write(data)
fp.close()
print('end receive...')
conn.close()
break
if __name__ == '__main__':
socket_service()