GVKun编程网logo

Python: Monitoring a Directory

1

想了解Python:MonitoringaDirectory的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于310重定向.htaccess:everityng在/directory/s

想了解Python: Monitoring a Directory的新动态吗?本文将为您提供详细的信息,此外,我们还将为您介绍关于310 重定向 .htaccess :everityng 在 /directory/subdirectory 到 /directory/subdirectory 之后、active-directory – .net5 MVC6应用程序上的Active Directory / LDAP、active-directory – Active Directory Web编程、active-directory – Active Directory,Linux和用户专用组的新知识。

本文目录一览:

Python: Monitoring a Directory

Python: Monitoring a Directory

Basic

mtime + checksum + directory traversal

Code

#!/usr/bin/env python

''''''
monitor a directory
print the changed files in the directory
strategy: mtime + checksum
''''''

import sys
import os
import stat
import cPickle as pickle

def print_usage():
    print ''monitor.py init [dir]''
    print ''monitor.py check [dir]''
    print ''monitor.py refresh [dir]''
    print ''[dir] defaults to .''

def main(argv=None):
    if len(argv) == 1:
        action = argv[0]
        directory = ''.''
    elif len(argv) == 2:
        action = argv[0]
        directory = argv[1]
    else:
        print_usage()
        return 1
    
    if action != ''init'' and action != ''check'' and action != ''refresh'':
        print_usage()
        return 1

    directory = os.path.abspath(directory)
    monitor = Monitor(directory)
    if action == ''init'':
        monitor.init_dir()
        return 0
    elif action == ''check'':
        monitor.check_dir()
        return 0
    elif action == ''refresh'':
        monitor.refresh_dir()
        return 0
    else:
        print ''Unexpeceted Error!''
        return 1
    

class Monitor(object):
    def __init__(self, directory):
        self.directory = directory

    def construct_cache(self):
        fileCacheList = []
        # recursively traverse the directory, cache each files''s mtime and checksum
        # {filename1:(mtime, checksum), filename2:(mtime, checksum), ....}
        for dirpath, dirnames, filenames in os.walk(self.directory):
            for f in filenames:
                if f == ''.cache'':
                    continue    # exclude .cache file
                f = os.path.join(dirpath, f)
                print ''dealing with'', f
                filecache = FileCache(f)
                fileCacheList.append(filecache)
        # dump fileCacheList to .cache
        cacheFilePath = os.path.abspath(os.path.join(self.directory, ''.cache''))
        with open(cacheFilePath, ''wb'') as cacheFile:
            pickler = pickle.Pickler(cacheFile)
            for filecache in fileCacheList:
                pickler.dump(filecache)

        
    def init_dir(self):
        ''''''
        init directory
        cache the mtime and checksum of all files in the directory
        dump the cache to .cache in the direcotry
        ''''''
        print ''init_dir''
        self.construct_cache()
        print ''init'', self.directory, ''success''
        return
    
    def check_dir(self):
        ''''''
        check directory to determine which files have changed
        ''''''
 #       print ''check_dir''
        # make sure the directory has been initialized
        # i.e. there''s a .cache file under this direcotry
        files = os.listdir(self.directory)
        if not ''.cache'' in files:
            print self.directory, ''has not been initialized yet''
            return
        # reconstruct fileCacheList
        fileCacheList = []
        cacheFilePath = os.path.abspath(os.path.join(self.directory, ''.cache''))
        with open(cacheFilePath, ''rb'') as cache:
            pickled = pickle.Unpickler(cache)
            while cache:
                try:
                    filecache = pickled.load()
                except:
                    break
                if isinstance(filecache, FileCache):
                    fileCacheList.append(filecache)
        
        # construct a dict from fileCacheList
        dictFiles = {}
        for fc in fileCacheList:
            dictFiles[fc.filepath] = (fc.mtime, fc.checksum)

        # traverse the target directory and determine which files have changed
        for dirpath, dirnames, filenames in os.walk(self.directory):
            for f in filenames:
                if f == ''.cache'':
                    continue    # exclude .cache file
                f = os.path.join(dirpath, f)
#                print ''checking'', f
                if f not in dictFiles:
                    print ''[ADD]:'', f
                else:           # f in dictFiles
                    smtime = dictFiles[f][0]
                    cmtime = os.stat(f)[stat.ST_MTIME]
                    if cmtime == smtime:
                        pass
                    else:
#                        print ''file %s changed mtime, recompute checksum'' % f
                        schecksum = dictFiles[f][1]
                        cchecksum = md5_file(f)
                        if schecksum == cchecksum:
                            pass
                        else:
                            print ''[CHANGED]:'', f
                    # remove entry f from dictFiles
                    dictFiles.pop(f)

        # tranverse ended
        if len(dictFiles) != 0:
            for f in dictFiles:
                print ''[REMOVE]:'', f
        

    def refresh_dir(self):
        print ''refresh_dir''
        self.construct_cache()
        print ''refresh %s success'' % self.directory


def md5_file(filename):
    try:
        import hashlib
        m = hashlib.md5()
    except ImportError:
        import md5
        m = md5.new()
        
    for line in open(filename):
        m.update(line)
    return m.hexdigest()

class FileCache(object):
    def __init__(self, f):
        self.filepath = os.path.abspath(f)
        self.mtime = os.stat(f)[stat.ST_MTIME]
        self.checksum = md5_file(f)

    def __str__(self):
        return self.filepath+str(self.mtime)+str(self.checksum)

if __name__ == ''__main__'':
    main(sys.argv[1:])

Test

chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py init
init_dir
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py~
dealing with /home/chenqi/mypro/python/monitorDir/test1/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/2.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/5.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/6.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/4.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/3.txt
init /home/chenqi/mypro/python/monitorDir success
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ touch monitor.py
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ touch 1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
[ADD]: /home/chenqi/mypro/python/monitorDir/1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py refresh
refresh_dir
dealing with /home/chenqi/mypro/python/monitorDir/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py
dealing with /home/chenqi/mypro/python/monitorDir/monitor.py~
dealing with /home/chenqi/mypro/python/monitorDir/test1/1.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/2.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/5.txt
dealing with /home/chenqi/mypro/python/monitorDir/test1/test_sub1/6.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/4.txt
dealing with /home/chenqi/mypro/python/monitorDir/test2/3.txt
refresh /home/chenqi/mypro/python/monitorDir success
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ rm 1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$ ./monitor.py check
[REMOVE]: /home/chenqi/mypro/python/monitorDir/1.txt
chenqi@chenqi-OptiPlex-760:~/mypro/python/monitorDir$

310 重定向 .htaccess :everityng 在 /directory/subdirectory 到 /directory/subdirectory 之后

310 重定向 .htaccess :everityng 在 /directory/subdirectory 到 /directory/subdirectory 之后

如何解决310 重定向 .htaccess :everityng 在 /directory/subdirectory 到 /directory/subdirectory 之后

您好,我的旧网站有很多链接,例如: https://www.siteurl.com/portfolio/web-design-portfolio/old-link1 https://www.siteurl.com/portfolio/web-design-portfolio/old-link2 抄送

在我的新网站中,我想将 /portfolio/web-design-portfolio/ 之后的所有旧链接 301 重定向到 https://www.siteurl.com/portfolio/web-design-portfolio/

你能帮忙吗?我尝试了一些教程,但运气不佳。

这是我的最新尝试,但出现“重定向过多”错误:

# BEGIN LSCACHE
# END LSCACHE
# BEGIN NON_LSCACHE
# END NON_LSCACHE

<IfModule mod_rewrite.c>
  RewriteEngine On
  
  # /portfolio/web-design-portfolio/any-link to portfolio/web-design-portfolio/
  RewriteRule ^portfolio/web-design-portfolio/(.*)$ /portfolio/web-design-portfolio/ [L,R=301]

</IfModule>

# BEGIN rlRSSslReallySimpleSSL RSSsl_version[4.0.15]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:CF-Visitor} ''"scheme":"http"''
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlRSSslReallySimpleSSL
# BEGIN wordpress
# Le direttive (linee) tra `BEGIN wordpress` e `END wordpress` sono
# generate dinamicamente,e dovrebbero essere modificate solo tramite i filtri di wordpress.
# Ogni modifica alle direttive tra questi marcatori verrà sovrascritta.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\\.PHP$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.PHP [L]
</IfModule>

# END wordpress


# PHP -- BEGIN cPanel-generated handler,do not edit
# Set the “ea-PHP74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-PHP74 .PHP .PHP7 .phtml
</IfModule>
# PHP -- END cPanel-generated handler,do not edit

# BEGIN ShortPixelWebp
# Le direttive (linee) tra `BEGIN ShortPixelWebp` e `END ShortPixelWebp` sono
# generate dinamicamente,e dovrebbero essere modificate solo tramite i filtri di wordpress.
# Ogni modifica alle direttive tra questi marcatori verrà sovrascritta.

# END ShortPixelWebp

解决方法

您可以在 .htaccess 文件的顶部添加此规则

<IfModule mod_rewrite.c>
  RewriteEngine On
  
  # /portfolio/web-design-portfolio/any-link to portfolio/web-design-portfolio/
  RewriteRule ^portfolio/web-design-portfolio/(.*)$ /portfolio/web-design-portfolio/ [L,R=301]

</IfModule>

active-directory – .net5 MVC6应用程序上的Active Directory / LDAP

active-directory – .net5 MVC6应用程序上的Active Directory / LDAP

我正在尝试找到连接到LDAP / Active目录的解决方案,以检查.Net5 MVC6 Web应用程序上的用户身份验证.
我看到有一个名为ADAL的解决方案,但我希望能够在没有Azure的情况下使用它.
我正在寻找一个类似于System.DirectoryServices的解决方案(只有一个与dnxcore50一起使用的解决方案).
谁有人能够让它工作?

解决方法

至少你问题的部分答案来自 How to use active Directory for ASP.Net 5 (MVC6) Intranet application

基本上使用“Windows身份验证”(活动目录)以及[授权]属性.

active-directory – Active Directory Web编程

active-directory – Active Directory Web编程

场景:
我正在使用集成身份验证来授予对ASP.net Intranet站点的访问权限.当该用户访问该站点时,某些信息将从其活动目录配置文件加载并可供其使用.此外,该站点还为其运行的Web应用程序维护该用户的某些信息.

从Active Directory检索信息(我使用LDAP)的最佳方法是什么,然后将活动目录配置文件链接到Web应用程序配置文件. Web应用程序正在使用自己的数据库,因此它不会在Active Directory中存储任何内容.

不建议使用用户名,因为名称更改可以并且确实发生,我正在尝试使用Active Directory中的某种形式的唯一标识符.唯一标识符必须与我的sql Server 2005数据库兼容.

解决方法

不要在LDAP中查询其域/用户ID和SID.该信息已从AD中提取,并在ASP.NET中的安全令牌中可用.

使用SID链接到本地​​数据库中的配置文件.完成.

这是一个很有用的资源:

Joe Kaplan的.NET开发人员目录服务编程指南

http://www.amazon.com/dp/0321350170

active-directory – Active Directory,Linux和用户专用组

active-directory – Active Directory,Linux和用户专用组

我们正在从 Linux系统上的NIS迁移到将所有内容绑定到Active Directory. NIS环境遵循许多Linux发行版使用的通用标准,即用户的主要组是与用户同名的组(并且用户通常是唯一的成员).

我被告知在Active Directory环境中,您可能没有与用户同名的组名(具体而言,没有两个AD安全对象可能具有相同的名称).这似乎使我们将组定义移动到AD的过程变得复杂.看起来我们可以仅使用POSIX属性(例如,不是实际的AD安全对象)来维护AD中的NIS组信息,但这似乎是次优修复(因为我们确实希望在两者中具有相同的组成员身份视图) Unix和AD世界).

您是否已将大型旧版NIS环境移至Active Directory中?你是怎么处理这种情况的?

解决方法

我也遇到了同样的问题.在阅读了很多文档之后,我想出了以下“解决方案”:

>要解决名称冲突,我通过在开头添加“g”字符重命名用户私人组.例如:User = erik,Group = erik.现在,在活动目录中,我将组命名为“gerik”.通过这种方式,我可以继续专注于AD迁移,而无需立即考虑用户私有组问题.
>慢慢停止使用用户专用组,因为这似乎不是首选 – 至少在使用Active Directory时.这可以通过创建一个像“unixgrp”这样的新组或使用“Domain Users”来完成,但我不喜欢“Domain Users”,因为当显示带有“ls”的文件时,该名称太长了.
>从用户私人群组迁移到“unixgrp”等公共群组时要小心.不要只将所有文件的组更改为“unixgrp”.例如,如果用户对其用户专用组拥有的文件具有组写权限,并且您将组更改为“unixgrp”,则“unixgrp”中的所有用户也将具有该文件的写入权限.所以某种脚本必须以正确的方式修改权限……玩得开心!

我承认,在使用活动目录时,唯一真正的解决方案是以某种方式支持用户私有组.但是我不知道怎么做…

我们今天的关于Python: Monitoring a Directory的分享就到这里,谢谢您的阅读,如果想了解更多关于310 重定向 .htaccess :everityng 在 /directory/subdirectory 到 /directory/subdirectory 之后、active-directory – .net5 MVC6应用程序上的Active Directory / LDAP、active-directory – Active Directory Web编程、active-directory – Active Directory,Linux和用户专用组的相关信息,可以在本站进行搜索。

本文标签:

上一篇Python: Monitoring a Directory

下一篇Python: instant markup, naive implementation