-
Notifications
You must be signed in to change notification settings - Fork 0
/
fn_send.py
43 lines (34 loc) · 1.21 KB
/
fn_send.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
#!/usr/bin/env python
# -*- coding=utf-8 -*-
import socket
import os
import sys
import struct
def socket_client(source_addr, source_port, filename):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((source_addr, source_port))
except socket.error as msg:
print(msg)
sys.exit(1)
print(s.recv(1024))
while 1:
filepath = filename
if os.path.isfile(filepath):
# 定义定义文件信息。128s表示文件名为128bytes长,l表示一个int或log文件类型,在此为文件大小
fileinfo_size = struct.calcsize('128sl')
# 定义文件头信息,包含文件名和文件大小
fhead = struct.pack('128sl',bytes(os.path.basename(filepath).encode('utf-8')),os.stat(filepath).st_size)
s.send(fhead)
print('client filepath: {0}'.format(filepath))
fp = open(filepath, 'rb')
while 1:
data = fp.read(1024)
if not data:
print('{0} file send over...'.format(filepath))
break
s.send(data)
s.close()
break
if __name__ == '__main__':
socket_client()