手机qpython与电脑python传文件
尽管有微信之类的,可以手机与电脑传文件,但是比较烦,对我来说因为我没有WiFi。旧电脑也比较卡512的内存。所以为了简化这些操作,我做了一个简单的python脚本。手机上和电脑上都能运行的一个脚本。
之前遇到了一些问题,已经都解决了。
其中一个问题就是文件名与数据之间区别,一开始费了老大劲。总是编码错误之类的。因为文件名发送之后就直接发送数据了,接收端接收的时候就没有做一个同步处理。导致接收文件名的时候,也接收了文件的数据一部分。所以导致编码错误。因此在服务端与客户端之间收发完文件名的时候,都做一个返回确认。这样文件名和数据之间就可以区别了,编码问题也就没有了。
另一个问题就是一个格式化的问题。一开始重复文件的时候。在文件名前面加一个日期,时间做区别。避免把文件覆盖了。但是一开始格式串是直接在函数内拼接字符串的。导致返回的字符串总是为空。有时候又能返回,也不知道怎么回事。后来把拼接字符串改到函数外。就解决这问题了。主要是调试纠错的时间太长。
还有一个问题,一开始想用命令行输入之类的解决的。但是手机端输入字符串还是有点麻烦的。所以我用提问式的菜单来代替输入命令字符串。这样比较快捷一点。一些文件和目录的选择也是这个道理。避免去输入,浪费时间。
基本功能已经满足需要了。也不想改进了。
代码如下:
#-*-coding:utf-8;-*-
#qpy:console
import socket
import os.path
import time
# 用于手机Qpython 与电脑python简单传文件2021.10.19
def gohome(): # 这个主要用于手机版,电脑版其实不需要
oldpath = os.getcwd()
print('原工作目录:', oldpath)
if '\\' in file:
psn = file.rpartition('\\')
elif '/' in file:
psn = file.rpartition('/')
newpath = psn[0]
os.chdir(newpath)
print('现工作目录:', newpath)
class Menu:
def init(self):
self.loopping = True
self.filename = ''
self.tips = {
'0': '退出',
'1': '显示<当前目录>',
'2': '显示当前目录下的<子目录>',
'3': '显示当前目录下的<文件>',
'4': '改变<当前目录>',
'5': '通过选择改变<当前目录>',
'6': '选择要发送的<文件>',
'7': '执行<发送文件>[客户端]',
'8': '执行<接收文件>[服务端]',
'9': '显示<帮助>'
}
self.funs = {
'0': self.on_exit,
'1': self.on_showcwd,
'2': self.on_showdirs,
'3': self.on_showfiles,
'4': self.on_chdir,
'5': self.on_seldir,
'6': self.on_selfile,
'7': self.on_dosend,
'8': self.on_dorecv,
'9': self.showtips
}
def showtips(self):
ks = list(self.tips.keys())
ks.sort()
print('='*40)
for k in ks:
print(k,'---',self.tips[k])
print('='*40)
def doloop(self):
#self.showtips()
while self.loopping:
tmp = input('输入菜单选项[ 退出0 ; 帮助9 ]:').strip()
try:
fun = self.funs[tmp]
except KeyError:
print('无效输入!', tmp)
continue
fun() # 执行功能
def on_exit(self): # 退出
self.loopping = False
def on_showcwd(self): # 显示当前目录
d = os.getcwd()
print('当前目录为:', d)
def on_showdirs(self): # 列出目录
print('以下显示当前目录下的<子目录>:')
d = os.getcwd()
for root, dirs, files in os.walk(d):
if len(dirs) == 0:
print('[-] 无目录可显示!')
return
for d in dirs:
print(d)
break
def on_showfiles(self): # 列出文件
print('以下显示当前目录下的<文件>:')
d = os.getcwd()
for root, dirs, files in os.walk(d):
if len(files) == 0:
print('[-] 无文件可显示!')
return
for f in files:
print(f)
def on_chdir(self): # 改变当前目录
tmp = input('输入目录[返回父目录..]:').strip()
if tmp == '': return
os.chdir(tmp)
d = os.getcwd()
print('当前目录改变为:', d)
def on_seldir(self): # 通过选择改变当前目录
tmpdict = {}
d = os.getcwd()
for root, dirs, files in os.walk(d):
if len(dirs) == 0:
print('[-] 无子目录可显示!')
return
for i,v in enumerate(dirs):
tmpdict[repr(i)] = v
print(i,'___',v)
break
idx=input('选择要改变的当前目录:').strip()
if idx in tmpdict.keys():
tmp = tmpdict[idx]
os.chdir(tmp)
d = os.getcwd()
print('当前目录改变为:', d)
def on_selfile(self): # 选择要发送的文件
tmpdict = {}
d = os.getcwd()
for root, dirs, files in os.walk(d):
if len(files) == 0:
print('[-] 无文件可显示!')
return
for i,v in enumerate(files):
tmpdict[repr(i)] = v
print(i,'___',v)
break
idx=input('选择要发送的文件:').strip()
if idx in tmpdict.keys():
self.filename = tmpdict[idx]
print('要发送的文件为:', self.filename)
def on_dosend(self): # 发送
HOST = '192.168.43.85'
PORT = 50007
HOST=input('连接到服务端,请输入服务端ip:')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
fn=self.filename
if fn =='':
s.close()
return
f1=open(fn, 'rb')
buf = fn.encode('gbk')
s.send(buf)
buf2=s.recv(256)
print('返回的文件名解码前:', buf2)
fn = buf2.decode('gbk').strip()
print('返回的文件名文件名解码后:', fn)
buf = b''
while True:
buf = f1.read(1024)
if not buf: break
s.send(buf)
s.close()
f1.close()
def on_dorecv(self): # 接收
HOST = ''
PORT = 50007
name = socket.gethostname()
ip = socket.gethostbyname(name)
print('服务端name=', name, '服务端ip=', ip)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('连接来自:', addr)
buf=b''
buf = conn.recv(256)
print('接收的文件名解码前:', buf)
fn = buf.decode('gbk').strip()
if fn == '':
conn.close()
return
print('接收的文件名解码后:', fn)
conn.send(buf) ##
buf=b''
if os.path.exists(fn) is True:
fn = time.strftime('%y%m%d_%H%M%S_')+fn
print('[-]文件重名! 改名为:',fn)
f2=open(fn, 'wb')
while True:
buf = conn.recv(1024)
if not buf: break
f2.write(buf)
conn.close()
f2.close()
############################################################################################
gohome()
mn = Menu()
print('===用于手机Qpython与电脑python简单传文件===')
mn.doloop()