首页 > 代码库 > 服务器传输文件

服务器传输文件

服务器端:

#!/usr/bin/env python
import socket
import re
import time
import threading
def dic():
    dic = {}
    with open(‘/script/ip.txt‘) as f:
        for i in f.readlines():
            if ‘#‘ not in i:
                name,ip = i.split(‘=‘)
                dic[ip.split(‘\n‘)[0]] = name
    return dic

def con(ip,dic):
    for i in dic.keys():
        if ip == i:
            return True,dic[ip]
            break
        else:
            continue
    return False,‘Null‘

def get_data():
    res = {}
    n = 5
    addr=(‘0.0.0.0‘,9999)
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind(addr)
    s.listen(1)
    n = len(dic())
    while True:
        conn,add = s.accept()
        Result,name = con(add[0],dic())
        if Result:
             data = http://www.mamicode.com/conn.recv(1024)
             res[name] = eval(data)
             if n > 1:
                n -= 1
             else:
                 break
        else:
           conn.sendall(‘ip no in txt file...‘)
           break
    return res
    s.close()

def all_num():
    dic = get_data()
    for x in dic.keys():
        for j in dic.keys():
            if x[:-1] == j[:-1] and x[-1] == ‘a‘ and j[-1] == ‘b‘:
                 sys_num = dic[x][‘sys_num‘] + dic[j][‘sys_num‘]
                 err_num = dic[x][‘err_num‘] + dic[j][‘err_num‘]
                 acc_time = dic[x][‘acc_time‘] + dic[j][‘acc_time‘]
                 with open(‘/script/%s%s%s.log‘ % (YY,MM,DD),‘a+‘) as f:
                      f.write(‘server: %s system_error: %s page_error: %s access_time: %s\n‘ % (x[:-2], sys_num, err_num,acc_time))
                 break

all_num()



客户端:



#!/usr/bin/env python
import time
import gzip
import socket
import sys
def read_gz_file(path):
    err_num = 0
    sys_num = 0
    acc_time = 0
    with gzip.open(path,‘r‘) as pf:
        for line in pf.readlines():
            err = line.split(‘|‘)[2]
            acc = line.split(‘|‘)[4].split(‘.‘)[0]
            if  err == ‘404‘:
                err_num += 1
            elif err == ‘500‘:
                sys_num += 1
            if int(acc) >= 10:
                acc_time += 1
        dic[‘err_num‘] = err_num
        dic[‘sys_num‘] = sys_num
        dic[‘acc_time‘] = acc_time
    return dic

def sock_con(data):
    addr = (‘ipaddress‘,9999)
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.connect(addr)
    s.sendall(‘%s‘ % data)
    s.close()

if __name__ == ‘__main__‘:
    YY = time.strftime(‘%Y‘)
    MM = time.strftime(‘%m‘)
    DD = time.strftime(‘%d‘)
    dic = {}
    data = http://www.mamicode.com/read_gz_file(‘/var/log/nginx/access_xmht.log-%s%s%s.gz‘ % (YY,MM,int(DD)-1))
    sock_con(data)


本文出自 “linuxyy” 博客,请务必保留此出处http://linuxyy.blog.51cto.com/9490250/1875743

服务器传输文件