File MD5 value is correct API interface development

Source: Internet
Author: User

Use the web.py framework to determine if the MD5 value of a file on the server side is correct. Return the JSON format data with the following code:


#!/usr/bin/env python

#-*-Coding:utf-8-*-


# Author:feng Minqiang <[email protected]>

# Maintainer:feng Minqiang <[email protected]>


Import Web

Import Hashlib

Import Os,sys

#简单的测试一个字符串的MD5值

def GetStrMd5 (SRC):

M0=HASHLIB.MD5 ()

M0.update (SRC)

Print M0.hexdigest ()

#大文件的MD5值

def GetFileMd5 (filename):

If not os.path.isfile (filename):

Return

Myhash = HASHLIB.MD5 ()

f = file (filename, ' RB ')

While True:

b = F.read (8096)

If not B:

Break

Myhash.update (b)

F.close ()

Return Myhash.hexdigest ()

def CALCSHA1 (filepath):

With open (filepath, ' RB ') as F:

Sha1obj = HASHLIB.SHA1 ()

Sha1obj.update (F.read ())

hash = Sha1obj.hexdigest ()

Print (hash)

return Hash

def CalcMD5 (filepath):

With open (filepath, ' RB ') as F:

Md5obj = HASHLIB.MD5 ()

Md5obj.update (F.read ())

hash = Md5obj.hexdigest ()

#print (hash)

return Hash


def diff_md5_file (FILEPATH,MD5_STR):

_TMP_MD5_STR=CALCMD5 (filepath)

if _tmp_md5_str = = Md5_str:

Return True

Else

Return False


def filelist (Filepath=none,var_name=none):

_fd=open (filepath)

_buf=_fd.readlines ()

_fd.close ()

md5re={}

For I in _buf:

I=i.strip (' \ n '). Replace ("", "). Split (')

_MD5_STR=I[0]

_FILE_NAME=I[1]

If Var_name!=none:

_tmp_name=_file_name.split (".") [0]

If _tmp_name! = Var_name:

Continue

_file_path=os.path.join (Os.path.dirname (filepath), _file_name)

If not os.path.exists (_file_path):

Md5re.update ({_file_name:false})

Continue

_re=diff_md5_file (_FILE_PATH,_MD5_STR)

Md5re.update ({_file_name:_re})

Return Md5re


Class Md5sum:

def GET (self):

Data=web.input ()

_version=none

_var_name=none

_len=len (Data.items ())

For I in Data.items ():

If i[0]== "Publiccloud":

_VERSION=I[1]

If i[0]== "war":

_VAR_NAME=I[1]

If _version!=none:

_path= "/opt/download/publiccloud/web/" +_version

md5_file= _path + "/md5.txt"

If not os.path.exists (md5_file):

Return None

If _var_name!=none:

return FileList (Md5_file,_var_name)

return FileList (Md5_file)

Class Index:

def GET (self):

Return True

if __name__ = = "__main__":

URLs = ('/md5 ', ' md5sum ',

'/', ' index ')

App=web.application (URLs, globals ())

App.run ()


use spawn-fcgi to hang the script under the Nginx. The commands are as follows (define your own environment variables yourself):

The directory where the-D script resides

-S socket placement location for the script

-P The location of the PID file placement for the script

--run Script (total entry)


Spawn-fcgi-d $__rootdir-s $PWD/sock/$SOCKFILE-P "$PIDFILE"--$PWD/pid/md5check.py fcgi


Interface Invocation Method:

Version number

War project name (corresponding war package)

/md5? $domainname = $version &war= $projectname

Returns whether the MD5 value of a specific war package is correct.

/md5? $domainname = $version

Returns whether the MD5 value of all packages for the project is correct.


This article is from the "Operation and maintenance Automation" blog, please make sure to keep this source http://bitstone.blog.51cto.com/3586955/1717779

File MD5 value is correct API interface development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.