-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
69 lines (59 loc) · 2.27 KB
/
server.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
67
68
# 服务端
# -*- coding=utf-8 -*-
import socket
import threading
import sys
import os
import struct
from tkinter import filedialog
import tkinter as tk
import send_choose
import easygui
def deal_data(conn, addr):
print('Accept new connection from {0}'.format(addr)) # 查看发送端的ip和端口
while True:
fileinfo_size = struct.calcsize('128sq') # linux 和 windows 互传 128sl 改为 128sq 机器位数不一样,一个32位一个64位
buf = conn.recv(fileinfo_size) # 接收的文件名字
print('收到的字节流:', buf, type(buf))
if buf:
print(buf, type(buf))
filename, filesize = struct.unpack('128sq', buf)
fn = filename.strip(str.encode('\00'))
if not os.path.exists('./like1'):
os.mkdir('./like1')
sPath = easygui.diropenbox()
#easygui.msgbox(sPath)
print('\n存储的文件地址:', sPath)
new_filename = os.path.join(str.encode(sPath), str.encode('new_') + fn)
print('file new name is {0}, filesize if {1}'.format(new_filename, filesize))
recvd_size = 0 # 定义已接收文件的大小
with open(new_filename, 'wb') as fp:
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)
print("end receive...")
conn.close()
break
def socket_service():
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# 修改ip,此处ip必须为服务器端的ip ,linux做服务器输入ifconfig得到ip
s.bind(('127.0.0.1', 5555))
s.listen(10)
except socket.error as msg:
print(msg)
sys.exit(1)
print("Waiting...")
while True:
conn, addr = s.accept()
t = threading.Thread(target=deal_data, args=(conn, addr))
t.start()
if __name__ == '__main__':
socket_service()