Skip to content

Commit

Permalink
pop
Browse files Browse the repository at this point in the history
  • Loading branch information
collinsctk committed May 7, 2016
1 parent d72a727 commit 67c7166
Show file tree
Hide file tree
Showing 11 changed files with 112 additions and 105 deletions.
80 changes: 41 additions & 39 deletions Network/FTP/FTP_FIND.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,51 @@

from ftplib import FTP
import re
def ftp_find(dict, file_type='.py', timeout=1):
def ftp_find(hostname, username, password, dirpath='/', file_type='.py', timeout=1, verbose = True):
if verbose:print('查找整个FTP中的特定文件(递归查询),并且返回清单!')
try:
hostname = dict[0]
username = dict[1]
password = dict[2]
connection = FTP(hostname)
connection.encoding = 'GB18030'
connection.login(username, password)
path = []
def DirRecursive(dirpath):
ls = []
connection.cwd(dirpath)
connection.retrlines('LIST', ls.append)
for line in ls:
patt = '(\d\d-\d\d-\d\d)\s*(\d\d\:\d\d\w\w)\s*(<DIR>|\d*)\s*(\w.*)'
scan_result = re.match(patt, line)
date = scan_result.group(1)
time = scan_result.group(2)
dir_or_length = scan_result.group(3)
dir_or_filename = scan_result.group(4)
connection = FTP(hostname)#连接站点
connection.encoding = 'GB18030'#使用中文编码
connection.login(username, password)#输入用户名和密码进行登录
path = []#所有文件的路径都将最终放入path清单
def DirRecursive(dirpath):#定义目录递归查询的函数
ls = []#LIST命令执行的结果,放入ls清单
connection.cwd(dirpath)#进入特定目录
connection.retrlines('LIST', ls.append)#执行LIST命令,并且把结果添加到ls清单
for line in ls:#逐行读取LIST命令返回结果
#print(line)
#文件的格式
#04-30-16 10:34AM 388 FTP_LIST.py
#目录的格式
#04-30-16 09:39AM <DIR> QYT
patt = '(\d\d-\d\d-\d\d)\s*(\d\d\:\d\d\w\w)\s*(<DIR>|\d*)\s*(\w.*)'#匹配命令返回结果的正则表达式
scan_result = re.match(patt, line)#正则表达式匹配
date = scan_result.group(1)#第一个部分(\d\d-\d\d-\d\d)为日期
time = scan_result.group(2)#第二个部分(\d\d\:\d\d\w\w)为时间
dir_or_length = scan_result.group(3)#第三个部分(<DIR>|\d*)目录或者文件大小
dir_or_filename = scan_result.group(4)#第四个部分文件或者目录名

if dir_or_length != '<DIR>':
if dirpath == '/':
path.append(dirpath + dir_or_filename)
else:
path.append(dirpath + '/' + dir_or_filename)
else:
if dirpath == '/':
DirRecursive(dirpath + dir_or_filename)
else:
DirRecursive(dirpath + '/' + dir_or_filename)
if dir_or_length != '<DIR>':#如果不为目录,当然就是文件了
if dirpath == '/':#如果当前路径在‘/’根目录
path.append(dirpath + dir_or_filename)#把文件路径加入path清单
else:#如果当前路径不在‘/’根目录
path.append(dirpath + '/' + dir_or_filename)#把文件路径加入path清单
else:#如果是目录
if dirpath == '/':#如果当前路径在‘/’根
DirRecursive(dirpath + dir_or_filename)#进行递归查询
else:#如果当前路径不在‘/’根目录
DirRecursive(dirpath + '/' + dir_or_filename)#进行递归查询
DirRecursive(dirpath)#执行函数
connection.close()#退出FTP连接会话
filetype_in_ftp = []#最终返回的,特定类型文件的清单
offset = 0 - len(file_type)#通过文件类型的长度,计算得到文件扩展名的偏移量
for x in path:#遍历整个文件清单
if x[offset:] == file_type:#查找扩展名匹配的文件
filetype_in_ftp.append(x)#把找到的文件放入filetype_in_ftp清单

DirRecursive('/')
connection.close()
filetype_in_ftp = []
offset = 0 - len(file_type)
for x in path:
if x[offset:] == file_type:
filetype_in_ftp.append(x)

return filetype_in_ftp
return filetype_in_ftp#返回filetype_in_ftp清单
except Exception as e:
print(e)

if __name__ == '__main__':
print(ftp_find(('202.100.1.168', 'ftpuser', 'cisco'), '.py'))
print(ftp_find('202.100.1.168', 'ftpuser', 'cisco', '/', '.py'))
26 changes: 13 additions & 13 deletions Network/FTP/FTP_GET.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import ftplib
import os

def downloadfile(site, file, user=('anonymous', '1@2.net'), rdir='.', ldir='.', verbose = True):
if verbose:print('Downloading', file)
os.chdir(ldir)
local = open(file, 'wb')
remote = ftplib.FTP(site)
remote.encoding = 'GB18030'
remote.login(*user)
remote.cwd(rdir)
remote.retrbinary('RETR ' + file, local.write, 1024)
remote.quit()
local.close()
if verbose: print('Downloading ' + file + ' done.')
def downloadfile(hostname, file, username='anonymous', password='1@2.net', rdir='.', ldir='.', verbose = True):
if verbose:print('下载文件:', file)
os.chdir(ldir)#切换本地工作目录
local = open(file, 'wb')#创建文件
remote = ftplib.FTP(hostname)#连接站点
remote.encoding = 'GB18030'#使用中文编码
remote.login(username, password)#输入用户名和密码进行登录
remote.cwd(rdir)#切换FTP目录
remote.retrbinary('RETR ' + file, local.write, 1024)#下载FTP文件,并且写入到本地文件
remote.quit()#退出会话
local.close()#关闭本地文件
if verbose: print('下载文件:' + file + ' 结束!')

if __name__ == '__main__':
downloadfile('202.100.1.168', 'qyttest.txt')
downloadfile('202.100.1.168', 'qyttest.txt')
21 changes: 10 additions & 11 deletions Network/FTP/FTP_LIST.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,16 @@
sys.path.append('../../ExtentionPackages')

import ftplib
import os

def listftpfile(site, dir='.', user=('anonymous', '1@2.net'), verbose = True):
if verbose:print('list file')
remote = ftplib.FTP(site)
remote.encoding = 'GB18030'
remote.login(*user)
remote.cwd(dir)
lst = remote.nlst()
remote.quit()
return lst
def listftpfile(hostname, username='anonymous', password='1@2.net', dir='/', timeout=1, verbose = True):
if verbose:print('罗列一个目录中的所有文件或者目录,并不递归罗列!')
remote = ftplib.FTP(hostname)#连接站点
remote.encoding = 'GB18030'#使用中文编码
remote.login(username, password)#输入用户名和密码进行登录
remote.cwd(dir)#进入特定目录
lst = remote.nlst()#罗列目录内容,并且产生清单
remote.quit()#退出会话
return lst#返回目录内容的清单

if __name__ == '__main__':
print(listftpfile('202.100.1.168'))
print(listftpfile('202.100.1.168'))
26 changes: 13 additions & 13 deletions Network/FTP/FTP_PUT.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
import ftplib
import os

def putfile(site, file, user=('anonymous', '1@2.net'), rdir='.', ldir='.', verbose = True):
if verbose:print('Uploading', file)
os.chdir(ldir)
local = open(file, 'rb')
remote = ftplib.FTP(site)
remote.encoding = 'GB18030'
remote.login(*user)
remote.cwd(rdir)
remote.storbinary('STOR ' + file, local, 1024)
remote.quit()
local.close()
if verbose: print('Upload done.')
def putfile(hostname, file, username='anonymous', password='1@2.net', rdir='.', ldir='.', verbose = True):
if verbose:print('上传文件:', file)
os.chdir(ldir)#切换本地工作目录
local = open(file, 'rb')#读取本地文件
remote = ftplib.FTP(hostname)#连接站点
remote.encoding = 'GB18030'#使用中文编码
remote.login(username, password)#输入用户名和密码进行登录
remote.cwd(rdir)#切换FTP目录
remote.storbinary('STOR ' + file, local, 1024)#上传文件
remote.quit()#退出会话
local.close()#关闭本地文件
if verbose: print('上传文件:' + file + ' 结束!')

if __name__ == '__main__':
putfile('202.100.1.168', 'FTP_LIST.py', user=('ftpuser', 'cisco'))
putfile('202.100.1.168', 'FTP_LIST.py', username='ftpuser', password='cisco')
1 change: 1 addition & 0 deletions Network/FTP/qyttest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
welcome to qytang.com
34 changes: 19 additions & 15 deletions Network/POP3/POP3_REC_MAIL.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@

def qyt_rec_mail(mailserver, mailuser, mailpasswd, mailprefix):
print('Connecting...')
server = poplib.POP3(mailserver)
server.user(mailuser)
server.pass_(mailpasswd)
server = poplib.POP3(mailserver)#连接到邮件服务器
server.user(mailuser)#邮件服务器用户名
server.pass_(mailpasswd)#邮件服务器密码

try:
print(server.getwelcome())
msgCount, msgBytes = server.stat()
print('There are', msgCount, 'mail message in', msgBytes, 'bytes')
print(server.list())
print(server.getwelcome())#打印服务器欢迎信息
msgCount, msgBytes = server.stat()#查询邮件数量与字节数
print('There are', msgCount, 'mail message in', msgBytes, 'bytes')#打印邮件数量与字节数
print(server.list())#打印邮件清单

for i in range(msgCount):
hdr, message, octets = server.retr(i + 1)
mail_file_name = mailprefix + '_' + str(i+1) + '.txt'
mail_file = open(mail_file_name, 'wb')
for i in range(msgCount):#逐个读取邮件
hdr, message, octets = server.retr(i + 1)#读取邮件
mail_file_name = mailprefix + '_' + str(i+1) + '.txt'#本地邮件文件名
mail_file = open(mail_file_name, 'wb')#创建本地邮件文件
for line in message:
mail_file.write(line)
mail_file.close()
mail_file.write(line)#把邮件写入本地邮件文件
mail_file.close()#写入完毕,关闭本地邮件文件
print(mail_file_name + ' Recieved!!!')
finally:
server.quit()
server.quit()#退出服务器
print('Bye.')

if __name__ == '__main__':
qyt_rec_mail('pop.163.com', 'collinsctk@163.com', 'password', 'test1')
import getpass
username = input('请输入用户名: ')
password = getpass.getpass('请输入密码: ')#读取密码,但是不回显!
mailprefix = input('邮件前缀: ')
qyt_rec_mail('pop.163.com', username, password, mailprefix)
1 change: 1 addition & 0 deletions Network/POP3/qytang_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Received: from [192.168.214.200] (unknown [124.205.63.13]) by smtp8 (Coremail) with SMTP id DMCowAB3w9guuy1XZ22eBg--.609S2; Sat, 07 May 2016 17:53:50 +0800 (CST)From: collinsctk@163.comTo: collinsctk@qytang.com;collinsctk@163.comData: Sat, 07 May 2016 09:53:51 -0000Subject: This is a test mailX-CM-TRANSID:DMCowAB3w9guuy1XZ22eBg--.609S2Message-Id:<572DBB2F.1CEAB9.15520@m12-12.163.com>X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvjxU-1xiDUUUUX-Originating-IP: [124.205.63.13]Date: Sat, 7 May 2016 17:53:51 +0800 (CST)X-CM-SenderInfo: xfrozxxqvf3yi6rwjhhfrp/1tbiJg9igFXlS1s8awAAs8
1 change: 0 additions & 1 deletion Network/POP3/test1_1.txt

This file was deleted.

1 change: 0 additions & 1 deletion Network/POP3/test1_2.txt

This file was deleted.

1 change: 0 additions & 1 deletion Network/POP3/test1_3.txt

This file was deleted.

25 changes: 14 additions & 11 deletions Network/SMTP/SMTP_SEND_MAIL.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@
import smtplib, sys, email.utils

def qyt_smtp_sendmail(mailserver, username, password, From, To, Subj):
Tos = To.split(';')
Date = email.utils.formatdate()

Tos = To.split(';')#把多个邮件接受者通过';'分开
Date = email.utils.formatdate()#格式化邮件时间
#print(Date)
text = ('From: %s\nTo: %s\nData: %s\nSubject: %s\n\n' % (From, To, Date, Subj))

server = smtplib.SMTP(mailserver)
server.login(username, password)
failed = server.sendmail(From, Tos, text)
server.quit()
#print(text)
server = smtplib.SMTP(mailserver)#连接邮件服务器
server.login(username, password)#通过用户名和密码登录邮件服务器
failed = server.sendmail(From, Tos, text)#发送邮件
server.quit()#退出会话
if failed:
print('Falied recipients:', failed)
print('Falied recipients:', failed)#如果出现故障,打印故障原因!
else:
print('No errors.')
print('No errors.')#如果没有故障发生,打印‘No errors.’!
print('Bye.')

if __name__ == '__main__':
qyt_smtp_sendmail('smtp.163.com', 'collinsctk@163.com', 'password', 'collinsctk@163.com', 'collinsctk@qytang.com;collinsctk@163.com', 'This is a test mail')
import getpass
username = input('请输入用户名: ')
password = getpass.getpass('请输入密码: ')#读取密码,但是不回显!
qyt_smtp_sendmail('smtp.163.com', username, password, 'collinsctk@163.com', 'collinsctk@qytang.com;collinsctk@163.com', 'This is a test mail')

0 comments on commit 67c7166

Please sign in to comment.