├── ConfigParser ├── cf_init.py ├── musite.py ├── muti-site.ini ├── test.ini └── test.py ├── Jinja2 ├── ghtml.py ├── output.html └── templates │ └── index.html ├── ansible ├── api │ ├── aa.sh │ ├── df_api.py │ ├── dfscript.py │ ├── playbook_api.py │ └── uptime_api.py └── playbook │ ├── debug.yaml │ ├── file │ └── migu_mirror.repo │ └── insalt_rhel7.yaml ├── bin_oct_hex └── convert.py ├── blackhatpython ├── 01tcpclient.py ├── 02udpclient.py ├── 03tcpserver.py └── 04nc.py ├── cs_mode ├── cs_command │ ├── client.py │ ├── readme │ └── server.py └── serversocket │ ├── client.py │ └── server.py ├── excel ├── add_macros.py ├── analyse_spider.py ├── chart_col.py ├── date_report.py ├── diff_excel.py ├── get_img.py ├── openexcel.py └── pyxlchart.py ├── fanyi.py ├── flask ├── PostMail │ ├── LICENSE │ ├── README.md │ ├── postmail.py │ └── run.py ├── chartkick │ ├── readme.txt │ ├── run.py │ ├── static │ │ ├── chartkick.js │ │ ├── highcharts.js │ │ └── jquery.min.js │ └── templates │ │ └── index.html ├── login │ ├── run.py │ ├── static │ │ └── css │ │ │ ├── bootstrap.min.css │ │ │ └── style.css │ └── templates │ │ ├── index.html │ │ └── login.html └── mysql_query │ ├── beautiful │ ├── main.py │ ├── static │ │ ├── bootstrap-3.3.4-dist.zip │ │ ├── css │ │ │ ├── bootstrap-theme.css │ │ │ ├── bootstrap-theme.css.map │ │ │ ├── bootstrap-theme.min.css │ │ │ ├── bootstrap.css │ │ │ ├── bootstrap.css.map │ │ │ └── bootstrap.min.css │ │ ├── fonts │ │ │ ├── glyphicons-halflings-regular.eot │ │ │ ├── glyphicons-halflings-regular.svg │ │ │ ├── glyphicons-halflings-regular.ttf │ │ │ ├── glyphicons-halflings-regular.woff │ │ │ └── glyphicons-halflings-regular.woff2 │ │ └── js │ │ │ ├── bootstrap.js │ │ │ ├── bootstrap.min.js │ │ │ ├── jquery-2.1.4.min.js │ │ │ └── npm.js │ └── templates │ │ └── new.html │ └── simple │ ├── demo.py │ ├── main.py │ ├── readme.txt │ ├── static │ └── jquery.min.js │ └── templates │ ├── index.html │ ├── index.html.bak │ └── layout.html ├── flask_monitor ├── monitems.py └── web │ ├── flask_web.py │ ├── static │ ├── exporting.js │ ├── highstock.js │ └── jquery.min.js │ └── templates │ └── mon.html ├── get_pid.py ├── get_user.py ├── getattr ├── funab.py ├── get.py ├── get2.py ├── main.py ├── map.py ├── web │ ├── backend │ │ ├── __init__.py │ │ ├── account.py │ │ └── admin.py │ ├── index.py │ └── web.py └── xg.py ├── gevent ├── echoserver.py ├── mgeturl.py └── talkserver.py ├── grab ├── 51cto │ ├── 51cto.tar.gz │ ├── img.py │ ├── insert.py │ ├── moudle │ │ ├── cto.py │ │ ├── curl.py │ │ ├── curl.py.bak │ │ ├── curl.pyc │ │ ├── iconv.py │ │ ├── iconv.pyc │ │ ├── test.html │ │ └── test.py │ ├── py.tar.gz │ ├── python │ │ ├── pycurl │ │ │ ├── async.py │ │ │ ├── init.py │ │ │ ├── muit.py │ │ │ ├── split.py │ │ │ ├── test.py │ │ │ ├── url.py │ │ │ ├── wordpress.py │ │ │ └── wp.html │ │ ├── title_url.py │ │ ├── url.py │ │ └── urlib3.py │ └── split.py ├── mirror_rpmpackage.py ├── reconn.py ├── segment │ ├── cto.py │ ├── curl.py │ ├── pre.py │ ├── tag.py │ └── title_url.py └── xisai.py ├── leetcode └── 01sum.py ├── mail ├── linux │ ├── httpdisk.sh │ ├── mail.py │ ├── mail2.py │ ├── pngmail.py │ ├── sendPmail.py │ └── sslmail.py └── windows │ └── mail.py ├── md5sum.py ├── netstat ├── netinfo ├── pynetstat.py ├── pyss.py ├── pyss_ex.py └── ss++.cc ├── ocr └── baiduocr.py ├── oracle_awr_analys ├── awr_backup.py ├── awr_configure.py ├── awr_constant.py ├── awr_constant2.py ├── awr_db.py ├── awr_debug.py ├── awr_main.py ├── awr_regular.py ├── awr_regular2.py ├── doc │ ├── create_awr.sql │ └── readme.txt ├── exec_awr_analysed.sh └── readme.txt ├── os ├── log_timedRotate.py ├── smem └── tcping.py ├── pcmd ├── agent-python2_201712212138.zip ├── config.conf ├── key.txt ├── p2agent.py └── pcmd.py ├── peewee ├── 1-createdb.py ├── 2-mode-query.py └── 3-date-notes.py ├── persist ├── cpickle │ ├── exp1.py │ └── exp2.py └── shelve │ └── m_shelve.py ├── pexecpt ├── 01pipe.py ├── 02logfile.py ├── ftpsync.py └── sysinfo.py ├── queue ├── fifo.py ├── lifo.py ├── priority.py ├── queue_thread.py ├── queue_thread1.py └── queue_thread2.py ├── requests ├── addcookies.py ├── headers.py ├── multipart.py ├── postjson.py └── urlencoded.py ├── selenium ├── def_selenium.py ├── front.py ├── open_switch.py ├── random │ ├── 1.py │ ├── fenbianlv.py │ ├── flower.html │ ├── proxy - ����.txt │ ├── proxy-test.py │ ├── proxy.py │ ├── proxy.txt │ ├── randomchoice - ����.py │ ├── randomchoice.py │ ├── siteurl.txt │ └── test.py ├── selenium_tab.py ├── selenium_test.py ├── sendkeys.py └── win32_test.py ├── stock ├── all.py ├── all_data_excel.py ├── calc │ ├── prettytable.py │ ├── prettytable.pyc │ └── rate.py ├── diagstock.py ├── hismoney.py ├── htmltable_xlsx.py ├── last.py ├── moneyin-multiprocess.py ├── moneyin.py ├── moneyin │ ├── moneyin.py │ └── prettytable.pyc ├── mult-in-excel.py ├── pe │ ├── get_pe.py │ ├── pe.py │ ├── stock_exp.txt │ └── thread_pe.py ├── readme │ ├── 163.txt │ ├── dfcf.txt │ ├── qq.txt │ ├── readme.txt │ ├── sina.txt │ ├── ths.txt │ └── zqzx.txt ├── she_codes.py ├── stock-wave.py ├── talib │ ├── 01-kdj.py │ ├── 02-kdjall.py │ ├── 03-macd.py │ ├── 04-macdall.py │ ├── 05-kdj-macd.py │ └── 06-kdj-macd-weixin.py ├── toexcel.py └── tosql.py ├── thread ├── Thread-Pool.py ├── condition │ ├── con1.py │ ├── con2.py │ └── con3.py └── lock │ ├── dead-lock1.py │ ├── dead-lock2.py │ ├── lock1.py │ └── lock2.py ├── tomcat_deamon ├── cfg │ └── config.ini ├── logs │ └── output.log └── monitor.py └── tree.py /ConfigParser/cf_init.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | #--------------------- 4 | # mysite: www.361way.com 5 | #--------------------- 6 | import sys,os,time 7 | import ConfigParser 8 | 9 | class Config: 10 | def __init__(self, path): 11 | self.path = path 12 | self.cf = ConfigParser.ConfigParser() 13 | self.cf.read(self.path) 14 | def get(self, field, key): 15 | result = "" 16 | try: 17 | result = self.cf.get(field, key) 18 | except: 19 | result = "" 20 | return result 21 | def set(self, filed, key, value): 22 | try: 23 | self.cf.set(field, key, value) 24 | cf.write(open(self.path,'w')) 25 | except: 26 | return False 27 | return True 28 | 29 | def read_config(config_file_path, field, key): 30 | cf = ConfigParser.ConfigParser() 31 | try: 32 | cf.read(config_file_path) 33 | result = cf.get(field, key) 34 | except: 35 | sys.exit(1) 36 | return result 37 | 38 | def write_config(config_file_path, field, key, value): 39 | cf = ConfigParser.ConfigParser() 40 | try: 41 | cf.read(config_file_path) 42 | cf.set(field, key, value) 43 | cf.write(open(config_file_path,'w')) 44 | except: 45 | sys.exit(1) 46 | return True 47 | 48 | if __name__ == "__main__": 49 | if len(sys.argv) < 4: 50 | sys.exit(1) 51 | 52 | config_file_path = sys.argv[1] 53 | field = sys.argv[2] 54 | key = sys.argv[3] 55 | if len(sys.argv) == 4: 56 | print read_config(config_file_path, field, key) 57 | else: 58 | value = sys.argv[4] 59 | write_config(config_file_path, field, key, value) 60 | -------------------------------------------------------------------------------- /ConfigParser/musite.py: -------------------------------------------------------------------------------- 1 | 2 | from ConfigParser import SafeConfigParser 3 | 4 | parser = SafeConfigParser() 5 | parser.read('multisection.ini') 6 | 7 | for section_name in parser.sections(): 8 | print 'Section:', section_name 9 | print ' Options:', parser.options(section_name) 10 | for name, value in parser.items(section_name): 11 | print ' %s = %s' % (name, value) 12 | print 13 | -------------------------------------------------------------------------------- /ConfigParser/muti-site.ini: -------------------------------------------------------------------------------- 1 | [site] 2 | url = http://www.361way.com/ 3 | username = 361way 4 | password = nothing 5 | 6 | [site2] 7 | url = http://www.91it.org/ 8 | username = 91it 9 | password = org 10 | -------------------------------------------------------------------------------- /ConfigParser/test.ini: -------------------------------------------------------------------------------- 1 | [site] 2 | url = http://www.361way.com/ 3 | username = 361way 4 | password = SECRET 5 | -------------------------------------------------------------------------------- /ConfigParser/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | from ConfigParser import SafeConfigParser 4 | 5 | parser = SafeConfigParser() 6 | parser.read('test.ini') 7 | 8 | print parser.get('site', 'url') 9 | -------------------------------------------------------------------------------- /Jinja2/ghtml.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | import os 5 | from jinja2 import Environment, FileSystemLoader 6 | 7 | PATH = os.path.dirname(os.path.abspath(__file__)) 8 | TEMPLATE_ENVIRONMENT = Environment( 9 | autoescape=False, 10 | loader=FileSystemLoader(os.path.join(PATH, 'templates')), 11 | trim_blocks=False) 12 | 13 | 14 | def render_template(template_filename, context): 15 | return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context) 16 | 17 | 18 | def create_index_html(): 19 | fname = "output.html" 20 | urls = ['http://www.361way.com/tag/python', 'http://www.361way.com/tag/linux', 'http://www.361way.com/tag/mysql'] 21 | context = { 22 | 'urls': urls 23 | } 24 | # 25 | with open(fname, 'w') as f: 26 | html = render_template('index.html', context) 27 | f.write(html) 28 | 29 | 30 | def main(): 31 | create_index_html() 32 | 33 | ######################################## 34 | 35 | if __name__ == "__main__": 36 | main() 37 | -------------------------------------------------------------------------------- /Jinja2/output.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | generating html 6 | 7 | 8 |
9 |

generating html

10 |

3 links

11 |
12 |
    13 |
  1. http://www.361way.com/tag/python
  2. 14 |
  3. http://www.361way.com/tag/linux
  4. 15 |
  5. http://www.361way.com/tag/mysql
  6. 16 |
17 | 18 | -------------------------------------------------------------------------------- /Jinja2/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | generating html 6 | 7 | 8 |
9 |

generating html

10 |

{{ urls|length }} links

11 |
12 |
    13 | {% for url in urls -%} 14 |
  1. {{ url }}
  2. 15 | {% endfor -%} 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /ansible/api/aa.sh: -------------------------------------------------------------------------------- 1 | #/bin/bash 2 | # author : www.361way.com 3 | IP=`ip add show|grep inet|grep 10|awk '{print $2}'` 4 | df -hl|grep '^/'|sed 's/%//g'|awk '{if($5>30) print $0}'|while read line 5 | do 6 | echo $IP `hostname` $line 7 | done -------------------------------------------------------------------------------- /ansible/api/df_api.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## site: www.361way.com 4 | ## mail: itybku@139.com 5 | ## desc: get more then 30% mount point of all hosts 6 | 7 | import ansible.runner 8 | #import json 9 | runner = ansible.runner.Runner( 10 | module_name='shell', 11 | module_args="df -hP|awk 'NR>1 && int($5)>30'", 12 | pattern='all', 13 | forks=10 14 | ) 15 | results = runner.run() 16 | #print results 17 | for (hostname, result) in results['contacted'].items(): 18 | if not 'failed' in result: 19 | for line in result['stdout'].split('\n'): 20 | print "%s %s" % (hostname, line) 21 | 22 | 23 | -------------------------------------------------------------------------------- /ansible/api/dfscript.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## author : www.361way.com 4 | ## mail : itybku@139.com 5 | ## desc : get host(IP hostname mount point use) 6 | import ansible.runner 7 | #import json 8 | runner = ansible.runner.Runner( 9 | module_name='script', 10 | module_args="aa.sh", 11 | pattern='all', 12 | forks=10 13 | ) 14 | results = runner.run() 15 | #print results 16 | for (hostname, result) in results['contacted'].items(): 17 | if not 'failed' in result: 18 | for line in result['stdout'].split('\r\n'): 19 | #print "%s %s" % (hostname, line) 20 | print lin -------------------------------------------------------------------------------- /ansible/api/playbook_api.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import ansible.playbook 5 | from ansible import callbacks 6 | from ansible import utils 7 | import json 8 | 9 | stats = callbacks.AggregateStats() 10 | playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) 11 | runner_cb = callbacks.PlaybookRunnerCallbacks(stats,verbose=utils.VERBOSITY) 12 | res=ansible.playbook.PlayBook( 13 | #playbook='/etc/ansible/playbooks/user.yml', 14 | playbook='/etc/ansible/playbooks/user_del.yml', 15 | stats=stats, 16 | callbacks=playbook_cb, 17 | runner_callbacks=runner_cb 18 | 19 | ).run() 20 | data = json.dumps(res,indent=4) 21 | print data 22 | -------------------------------------------------------------------------------- /ansible/api/uptime_api.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding=utf-8 3 | 4 | import ansible.runner 5 | import sys 6 | 7 | # construct the ansible runner and execute on all hosts 8 | results = ansible.runner.Runner( 9 | pattern='*', forks=10, 10 | module_name='command', module_args='/usr/bin/uptime', 11 | ).run() 12 | 13 | if results is None: 14 | print "No hosts found" 15 | sys.exit(1) 16 | 17 | print "UP ***********" 18 | for (hostname, result) in results['contacted'].items(): 19 | if not 'failed' in result: 20 | print "%s >>> %s" % (hostname, result['stdout']) 21 | 22 | print "FAILED *******" 23 | for (hostname, result) in results['contacted'].items(): 24 | if 'failed' in result: 25 | print "%s >>> %s" % (hostname, result['msg']) 26 | 27 | print "DOWN *********" 28 | for (hostname, result) in results['dark'].items(): 29 | print "%s >>> %s" % (hostname, result) 30 | -------------------------------------------------------------------------------- /ansible/playbook/debug.yaml: -------------------------------------------------------------------------------- 1 | # verbosity(added in 2.1),如果使用的的该本低于该版本,使用时会报错 2 | - name: debug test one host 3 | hosts: 200.200.6.53 4 | tasks: 5 | - debug: 6 | msg: "System {{ inventory_hostname }} has uuid {{ ansible_product_uuid }}" 7 | 8 | - debug: 9 | msg: "System {{ inventory_hostname }} has gateway {{ ansible_default_ipv4.gateway }}" 10 | when: ansible_default_ipv4.gateway is defined 11 | 12 | - shell: /usr/bin/uptime 13 | register: result 14 | 15 | - debug: 16 | var: result 17 | verbosity: 2 18 | 19 | - name: Display all variables/facts known for a host 20 | debug: 21 | var: hostvars[inventory_hostname] 22 | verbosity: 4 23 | -------------------------------------------------------------------------------- /ansible/playbook/file/migu_mirror.repo: -------------------------------------------------------------------------------- 1 | [migumirror] 2 | name=migumirror 3 | enabled=1 4 | baseurl=http://192.168.149.204/rhel7.3 5 | gpgcheck=0 6 | -------------------------------------------------------------------------------- /ansible/playbook/insalt_rhel7.yaml: -------------------------------------------------------------------------------- 1 | - name: Fetch configuration from all testservers 2 | hosts: all 3 | vars: 4 | ID: "id:" 5 | masterip: "master: 192.168.95.232" 6 | 7 | tasks: 8 | - name: copy migu repo to hosts 9 | copy: src=./file/migu_rhel7.repo dest=/etc/yum.repos.d/migumirror.repo 10 | 11 | - name: install salt repo 12 | yum: name=https://repo.saltstack.com/yum/redhat/salt-repo-latest-2.el7.noarch.rpm state=present 13 | 14 | - name: install salt minion 15 | yum: name=salt-minion state=latest 16 | 17 | - name: set the salt master ip 18 | shell: echo {{ masterip }} >>/etc/salt/minion 19 | 20 | - name: Get config 21 | #shell: sed -i '/^#id:/a\id: {{ ansible_ens192.ipv4.address }}' /etc/salt/minion 22 | lineinfile: dest=/etc/salt/minion regexp="^#id" line="{{ ID }} {{ inventory_hostname }}" 23 | 24 | - name: enable and start minion 25 | shell: "systemctl enable salt-minion.service && systemctl start salt-minion.service" 26 | -------------------------------------------------------------------------------- /bin_oct_hex/convert.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2018 www.361way.com site All rights reserved. 5 | # Filename :convert.py 6 | # Author :yangbk 7 | # Create Time :2017-08-15 10:41 8 | # Description :bin oct dec hex convert (进制转换) 9 | # =============================================================================== 10 | ''' 11 | bin 二进制 12 | oct 八进制 13 | dec 十进制 14 | hex 十六进制 15 | -------------------- 16 | >>> hex(10) 17 | '0xa' 18 | >>> oct(8) 19 | '010' 20 | >>> oct(10) 21 | '012' 22 | >>> bin(10) 23 | '0b1010' 24 | >>> int('0xa',16) 25 | 10 26 | >>> int('010',8) 27 | 8 28 | >>> int('0b1010',2) 29 | 10 30 | >>> int('0xA',16) 31 | 10 32 | ''' 33 | 34 | import os,sys 35 | 36 | # global definition 37 | # base = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F] 38 | base = [str(x) for x in range(10)] + [ chr(x) for x in range(ord('A'),ord('A')+6)] 39 | 40 | # bin2dec 41 | # 二进制 to 十进制: int(str,n=10) 42 | def bin2dec(string_num): 43 | return str(int(string_num, 2)) 44 | 45 | # hex2dec 46 | # 十六进制 to 十进制 47 | def hex2dec(string_num): 48 | return str(int(string_num.upper(), 16)) 49 | 50 | # dec2bin 51 | # 十进制 to 二进制: bin() 52 | def dec2bin(string_num): 53 | num = int(string_num) 54 | mid = [] 55 | while True: 56 | if num == 0: break 57 | num,rem = divmod(num, 2) 58 | mid.append(base[rem]) 59 | 60 | return ''.join([str(x) for x in mid[::-1]]) 61 | 62 | # dec2hex 63 | # 十进制 to 八进制: oct() 64 | # 十进制 to 十六进制: hex() 65 | def dec2hex(string_num): 66 | num = int(string_num) 67 | mid = [] 68 | while True: 69 | if num == 0: break 70 | num,rem = divmod(num, 16) 71 | mid.append(base[rem]) 72 | 73 | return ''.join([str(x) for x in mid[::-1]]) 74 | 75 | # hex2tobin 76 | # 十六进制 to 二进制: bin(int(str,16)) 77 | def hex2bin(string_num): 78 | return dec2bin(hex2dec(string_num.upper())) 79 | 80 | # bin2hex 81 | # 二进制 to 十六进制: hex(int(str,2)) 82 | def bin2hex(string_num): 83 | return dec2hex(bin2dec(string_num)) 84 | -------------------------------------------------------------------------------- /blackhatpython/01tcpclient.py: -------------------------------------------------------------------------------- 1 | #encoding: utf-8 2 | import socket 3 | 4 | target_host = "www.baidu.com" 5 | target_port = 80 6 | # create a socket object 7 | client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 8 | # connect the client 9 | client.connect((target_host,target_port)) 10 | # send some data 11 | client.send("GET / HTTP/1.1\r\nHost: baidu.com\r\n\r\n") 12 | # receive some data 13 | response = client.recv(4096) 14 | print response 15 | -------------------------------------------------------------------------------- /blackhatpython/02udpclient.py: -------------------------------------------------------------------------------- 1 | import socket 2 | target_host = "127.0.0.1" 3 | target_port = 80 4 | # create a socket object 5 | client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | # send some data 7 | client.sendto("AAABBBCCC",(target_host,target_port)) 8 | # receive some data 9 | data, addr = client.recvfrom(4096) 10 | print data 11 | 12 | -------------------------------------------------------------------------------- /blackhatpython/03tcpserver.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import threading 3 | bind_ip = "0.0.0.0" 4 | bind_port = 9999 5 | server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 6 | server.bind((bind_ip,bind_port)) 7 | server.listen(5) 8 | print "[*] Listening on %s:%d" % (bind_ip,bind_port) 9 | # this is our client-handling thread 10 | def handle_client(client_socket): 11 | # print out what the client sends 12 | request = client_socket.recv(1024) 13 | print "[*] Received: %s" % request 14 | # send back a packet 15 | client_socket.send("ACK!") 16 | client_socket.close() 17 | while True: 18 | client,addr = server.accept() 19 | print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1]) 20 | # spin up our client thread to handle incoming data 21 | client_handler = threading.Thread(target=handle_client,args=(client,)) 22 | client_handler.start() 23 | -------------------------------------------------------------------------------- /cs_mode/cs_command/client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import socket,os,sys 3 | #host=sys.argv[1] 4 | port=4567 5 | for host in os.popen('cat ip.list').readlines(): 6 | print host 7 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 8 | s.connect((host,port)) 9 | while 1: 10 | data=sys.argv[2] 11 | if not data: 12 | break 13 | s.sendall(data) 14 | data=s.recv(1024) 15 | if not data: 16 | break 17 | print data 18 | s.close() 19 | -------------------------------------------------------------------------------- /cs_mode/cs_command/readme: -------------------------------------------------------------------------------- 1 | 一、用法 2 | ip.list里写server端的IP地址,每行一个,如: 3 | 1.1.1.1 4 | 2.2.2.2 5 | 6 | 使用方法,将server.py部署于目标主机上,执行server.py;客户端执行 7 | python client.py ip.list "command"即可 8 | -------------------------------------------------------------------------------- /cs_mode/cs_command/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import socket,os 3 | host='' 4 | port=4567 5 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 6 | s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1) 7 | s.bind((host,port)) 8 | s.listen(1) 9 | print "Server is running on port %d; press ctrl-c to terminate." % port 10 | while 1: 11 | clientsock,clientaddr=s.accept() 12 | print "connect from %s" % str(clientaddr) 13 | clientfile=clientsock.makefile('rw',0) 14 | data=clientsock.recv(1024) 15 | command=os.popen(data).read() 16 | clientfile.write("%s" % command) 17 | clientfile.close() 18 | clientsock.close() 19 | -------------------------------------------------------------------------------- /cs_mode/serversocket/client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | import commands 4 | import time 5 | import os 6 | import socket 7 | import pickle 8 | def get_ip(): 9 | ip={} 10 | status,interface = commands.getstatusoutput("ifconfig -s|grep -v Iface|grep -v lo |awk '{print $1}'") 11 | inface = interface.split('\n') 12 | for i in range(len(inface)): 13 | var=inface[i] 14 | os.environ['var']=str(var) 15 | status,inter_ip=commands.getstatusoutput("ifconfig `echo $var`|sed -n 2p|awk '{ print $2 }'|awk -F : '{ print $2 }'") 16 | ip[inface[i]]=inter_ip 17 | return ip 18 | 19 | 20 | #if __name__ == '__main__': 21 | MonitorData=pickle.loads(pickle.dumps(get_ip())) 22 | # local_file = file('/tmp/monitor_data.pickle','w') 23 | # pickle.dump(ip_data,local_file) 24 | 25 | 26 | 27 | def UpTime(): 28 | Uptime = time.strftime('%Y-%m-%d %H:%M:%S') 29 | return Uptime 30 | 31 | 32 | def Write_local_data(): 33 | try: 34 | local_file = file('/tmp/monitor_data.pickle','w') 35 | pickle.dump(MonitorData,local_file) 36 | except IOError: 37 | print "Write Error" 38 | 39 | def check_local_data(): 40 | try: 41 | local_file = file('/tmp/monitor_data.pickle') 42 | local_data = pickle.load(local_file) 43 | print local_data 44 | except EOFError: 45 | Write_local_data() 46 | print 'Write monitor to file' 47 | return False 48 | if MonitorData == local_data: 49 | Update = {} 50 | Update['Uptime'] = UpTime() 51 | return Update 52 | else: 53 | Write_local_data() 54 | return False 55 | 56 | 57 | 58 | 59 | 60 | def SendMonitorData(): 61 | 62 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 63 | s.connect(('192.168.0.103', 9999)) 64 | check_data = check_local_data() 65 | print check_data 66 | if check_data is False: 67 | MonitorData['Uptime'] = UpTime() 68 | monitor_data = pickle.dumps(MonitorData) 69 | while True: 70 | s.sendall('InsertData') 71 | data = s.recv(1024) 72 | if data == 'ReadyToReceiveFile': 73 | s.sendall(monitor_data) 74 | time.sleep(30) 75 | s.sendall('DataSendDone') 76 | break 77 | s.close() 78 | 79 | else: 80 | monitor_data = pickle.dumps(check_data) 81 | print monitor_data 82 | while True: 83 | s.sendall('UpdateData') 84 | data = s.recv(1024) 85 | if data == 'ReadyToReceiveFile': 86 | s.sendall(monitor_data) 87 | time.sleep(30) 88 | s.sendall('DataSendDone') 89 | break 90 | s.close() 91 | 92 | if __name__ == '__main__': 93 | SendMonitorData() 94 | -------------------------------------------------------------------------------- /cs_mode/serversocket/server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | import multiprocessing 3 | import os,sys 4 | import SocketServer,time 5 | import pickle 6 | 7 | 8 | 9 | class MyTCPHandler(SocketServer.BaseRequestHandler): 10 | def handle(self): 11 | print 'got a connection from:',self.client_address[0] 12 | self.data = self.request.recv(1024).strip() 13 | if not self.data: 14 | print 'Client is disconnected...',self.client_address[0] 15 | if self.data == 'InsertData': 16 | print self.data 17 | print "going to receive date",self.data 18 | self.request.send('ReadyToReceiveFile') 19 | data = self.request.recv(4096) 20 | if data == 'DataSendDone': 21 | print 'Transfer is done.' 22 | else: 23 | Monitor_data = data 24 | Monitor_data = pickle.loads(Monitor_data) 25 | print Monitor_data 26 | # mysql.InsertData(Monitor_data) 27 | print "INserDatadone" 28 | if self.data == 'UpdateData': 29 | print "going to receive date",self.data 30 | self.request.send('ReadyToReceiveFile') 31 | data = self.request.recv(4096) 32 | print data 33 | if data == 'DataSendDone': 34 | print 'Transfer is done.' 35 | else: 36 | Monitor_data = data 37 | Monitor_data = pickle.loads(Monitor_data) 38 | # mysql.UpdateData(Monitor_data) 39 | print 'updatedone......' 40 | 41 | 42 | if __name__ == "__main__": 43 | 44 | HOST,PORT = "",9999 45 | server = SocketServer.ThreadingTCPServer((HOST, PORT), MyTCPHandler) 46 | jobs=[] 47 | for i in range(3): 48 | server_thread = multiprocessing.Process(target=server.serve_forever) 49 | server_thread.daemon = True 50 | jobs.append(server_thread) 51 | server_thread.start() 52 | time.sleep(1) 53 | 54 | for j in jobs: 55 | j.join() 56 | -------------------------------------------------------------------------------- /excel/add_macros.py: -------------------------------------------------------------------------------- 1 | import xlsxwriter 2 | 3 | # Note the file extension should be .xlsm. 4 | workbook = xlsxwriter.Workbook('macros.xlsm') 5 | worksheet = workbook.add_worksheet() 6 | 7 | worksheet.set_column('A:A', 30) 8 | 9 | # Add the VBA project binary. 10 | workbook.add_vba_project('./vbaProject.bin') 11 | 12 | # Show text for the end user. 13 | worksheet.write('A3', 'Press the button to say hello.') 14 | 15 | # Add a button tied to a macro in the VBA project. 16 | worksheet.insert_button('B3', {'macro': 'Test_Macro', 17 | 'caption': 'Press Me', 18 | 'width': 80, 19 | 'height': 30}) 20 | 21 | workbook.close() 22 | -------------------------------------------------------------------------------- /excel/analyse_spider.py: -------------------------------------------------------------------------------- 1 | #coding: utf-8 2 | import xlsxwriter 3 | import random 4 | 5 | def get_num(): 6 | return random.randrange(0, 201, 2) 7 | 8 | workbook = xlsxwriter.Workbook('analyse_spider.xlsx') #创建一个Excel文件 9 | worksheet = workbook.add_worksheet() #创建一个工作表对象 10 | chart = workbook.add_chart({'type': 'column'}) #创建一个图表对象 11 | #定义数据表头列表 12 | title = [u'业务名称',u'星期一',u'星期二',u'星期三',u'星期四',u'星期五',u'星期六',u'星期日',u'平均流量'] 13 | buname= [u'时光网',u'汽车之家',u'weixin.com',u'163.com',u'baidu.com'] #定义频道名称 14 | #定义5频道一周7天流量数据列表 15 | data = [] 16 | for i in range(5): 17 | tmp = [] 18 | for j in range(7): 19 | tmp.append(get_num()) 20 | data.append(tmp) 21 | 22 | format=workbook.add_format() #定义format格式对象 23 | format.set_border(1) #定义format对象单元格边框加粗(1像素)的格式 24 | 25 | format_title=workbook.add_format() #定义format_title格式对象 26 | format_title.set_border(1) #定义format_title对象单元格边框加粗(1像素)的格式 27 | format_title.set_bg_color('#cccccc') #定义format_title对象单元格背景颜色为 28 | #'#cccccc'的格式 29 | format_title.set_align('center') #定义format_title对象单元格居中对齐的格式 30 | format_title.set_bold() #定义format_title对象单元格内容加粗的格式 31 | 32 | format_ave=workbook.add_format() #定义format_ave格式对象 33 | format_ave.set_border(1) #定义format_ave对象单元格边框加粗(1像素)的格式 34 | format_ave.set_num_format('0.00') #定义format_ave对象单元格数字类别显示格式 35 | 36 | #下面分别以行或列写入方式将标题、业务名称、流量数据写入起初单元格,同时引用不同格式对象 37 | worksheet.write_row('A1',title,format_title) 38 | worksheet.write_column('A2', buname,format) 39 | worksheet.write_row('B2', data[0],format) 40 | worksheet.write_row('B3', data[1],format) 41 | worksheet.write_row('B4', data[2],format) 42 | worksheet.write_row('B5', data[3],format) 43 | worksheet.write_row('B6', data[4],format) 44 | 45 | #定义图表数据系列函数 46 | def chart_series(cur_row): 47 | worksheet.write_formula('I'+cur_row, \ 48 | '=AVERAGE(B'+cur_row+':H'+cur_row+')',format_ave) #计算(AVERAGE函数)频 49 | #道周平均流量 50 | chart.add_series({ 51 | 'categories': '=Sheet1!$B$1:$H$1', #将“星期一至星期日”作为图表数据标签(X轴) 52 | 'values': '=Sheet1!$B$'+cur_row+':$H$'+cur_row, #频道一周所有数据作 53 | #为数据区域 54 | 'line': {'color': 'black'}, #线条颜色定义为black(黑色) 55 | 'name': '=Sheet1!$A$'+cur_row, #引用业务名称为图例项 56 | }) 57 | 58 | for row in range(2, 7): #数据域以第2~6行进行图表数据系列函数调用 59 | chart_series(str(row)) 60 | 61 | chart.set_size({'width': 577, 'height': 287}) #设置图表大小 62 | chart.set_title ({'name': u'爬虫分析'}) #设置图表(上方)大标题 63 | chart.set_y_axis({'name': 'count'}) #设置y轴(左侧)小标题 64 | 65 | worksheet.insert_chart('A8', chart) #在A8单元格插入图表 66 | workbook.close() #关闭Excel文档 67 | -------------------------------------------------------------------------------- /excel/date_report.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # site: www.361way.com 4 | # mail: itybku@139.com 5 | 6 | import sys 7 | reload(sys) 8 | sys.setdefaultencoding('utf-8') 9 | 10 | import glob 11 | import xlrd 12 | import xlsxwriter 13 | import re 14 | 15 | workbook = xlsxwriter.Workbook('date_report.xlsx') 16 | worksheet = workbook.add_worksheet('Sheet1') 17 | bold = workbook.add_format({'bold': 1}) 18 | headings = [u'时间', u'问题描述', u'责任人'] 19 | worksheet.write_row('A1', headings, bold) 20 | 21 | 22 | def get_xlsx_data(file): 23 | #data = xlrd.open_workbook(u"D:\\2016\\杭州软件服务部管理服务工作日报(2016-07-13).xlsx") 24 | data = xlrd.open_workbook(file) 25 | table = data.sheets()[0] 26 | nrows = table.nrows 27 | ncols = table.ncols 28 | timedata = table.row_values(0)[0] 29 | 30 | timedata = re.split('(',timedata.encode("utf-8",'ignore')) 31 | timedata = timedata[1].replace(')','') 32 | 33 | 34 | #print timedata 35 | for i in range(7,nrows): 36 | #print table.row_values(i)[0] 37 | if table.row_values(i)[0] == 1.0: 38 | maxv = i 39 | #print maxv 40 | if maxv: 41 | for i in range(6,(maxv-2)): 42 | if table.row_values(i)[2]: 43 | #print timedata + '|' + table.row_values(i)[2].encode("utf-8",'ignore') 44 | data_arry.append([timedata,table.row_values(i)[2].encode("utf-8",'ignore'),'361way']) 45 | else: 46 | for i in range(6,nrows): 47 | if table.row_values(i)[2]: 48 | #print timedata + '|' + table.row_values(i)[2].encode("utf-8",'ignore') 49 | data_arry.append([timedata,table.row_values(i)[2].encode("utf-8",'ignore'),'361way']) 50 | 51 | data_arry = [] 52 | for file in glob.glob("d:\\2016\\*.xlsx"): 53 | get_xlsx_data(file) 54 | 55 | row = 1 56 | col = 0 57 | for linev in data_arry: 58 | #print linev 59 | worksheet.write_row(row,col,linev) 60 | row += 1 61 | 62 | workbook.close() 63 | -------------------------------------------------------------------------------- /excel/get_img.py: -------------------------------------------------------------------------------- 1 | from pyxlchart import Pyxlchart 2 | xl = Pyxlchart() 3 | xl.WorkbookDirectory = "D:\\" 4 | xl.WorkbookFilename = "chart_column.xlsx" 5 | xl.SheetName = "" 6 | #xl.ImageFilename = "MyChart1" 7 | xl.ExportPath = "d:\\" 8 | xl.ChartName = "" 9 | xl.start_export() 10 | -------------------------------------------------------------------------------- /excel/openexcel.py: -------------------------------------------------------------------------------- 1 | import win32com.client as win32 2 | excel = win32.gencache.EnsureDispatch('Excel.Application') 3 | wb = excel.Workbooks.Open(r'c:\checklist.xlsx') 4 | # Alternately, specify the full path to the workbook 5 | # wb = excel.Workbooks.Open(r'C:\myfiles\excel\workbook2.xlsx') 6 | excel.Visible = True 7 | -------------------------------------------------------------------------------- /fanyi.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import pycurl 4 | import StringIO 5 | import json 6 | import sys 7 | 8 | 9 | 10 | def FanYi(words): 11 | c = pycurl.Curl() 12 | buf = StringIO.StringIO() 13 | Url = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=PGcC40pcK5PVqN87d9VwfcXj&q=" + words + "&from=auto&to=auto" 14 | #c.setopt(pycurl.URL, "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=PGcC40pcK5PVqN87d9VwfcXj&q=today&from=auto&to=auto") 15 | c.setopt(pycurl.URL, Url) 16 | c.setopt(pycurl.WRITEFUNCTION, buf.write) 17 | c.perform() 18 | data = buf.getvalue() 19 | #jdata = json.loads(data,encoding='utf-8') 20 | jdata = json.loads(data,encoding='utf-8') 21 | print json.dumps(jdata,ensure_ascii=False,indent=2) 22 | 23 | 24 | if len(sys.argv) < 2: 25 | print 'No anything input ,please input your word' 26 | sys.exit() 27 | 28 | else : 29 | words = '%0A'.join(sys.argv[1:]) 30 | FanYi(words) 31 | sys.exit() 32 | 33 | -------------------------------------------------------------------------------- /flask/PostMail/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Yinzo 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /flask/PostMail/run.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify 2 | from postmail import MailSender 3 | 4 | app = Flask(__name__) 5 | mail_sender = MailSender() 6 | 7 | 8 | @app.route('/') 9 | def homepage(): 10 | return "MailSender is online!" 11 | 12 | @app.route('/mail', methods=['POST']) 13 | def mail(): 14 | try: 15 | mail_sender.send(**request.form.to_dict()) 16 | return jsonify({'status': 'succeed', 'msg': ""}) 17 | except Exception as ex: 18 | return jsonify({'status': 'failed', 'msg': str(ex)}) 19 | 20 | if __name__ == '__main__': 21 | app.run(host="0.0.0.0") 22 | -------------------------------------------------------------------------------- /flask/chartkick/readme.txt: -------------------------------------------------------------------------------- 1 | first of all ,you must be install flask、chartkick.py python module 2 | you can see the demo: 3 | [see the useage page](http://www.361way.com/chartkick-flask/4477.html) 4 | -------------------------------------------------------------------------------- /flask/chartkick/run.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | from flask import Flask, jsonify, render_template, request 4 | import chartkick 5 | import json 6 | 7 | #app = Flask(__name__, static_folder=chartkick.js()) 8 | app = Flask(__name__) 9 | app.jinja_env.add_extension("chartkick.ext.charts") 10 | 11 | @app.route('/') 12 | @app.route('/index') 13 | def index(): 14 | value = {'谷歌': 52.9, 'Opera': 1.6, 'Firefox': 27.7} 15 | return render_template('index.html', data=json.dumps(value, encoding='utf-8',indent=4)) 16 | 17 | 18 | if __name__ == "__main__": 19 | app.run(host="0.0.0.0",debug=True) 20 | -------------------------------------------------------------------------------- /flask/chartkick/templates/index.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | {% bar_chart data with style="width:200px; height:20px;" %} 9 | {% pie_chart data %} 10 | -------------------------------------------------------------------------------- /flask/login/run.py: -------------------------------------------------------------------------------- 1 | from flask import * 2 | 3 | app = Flask(__name__,static_url_path='/static') 4 | 5 | @app.route("/login",methods=['POST','GET']) 6 | 7 | def login(): 8 | error = None 9 | if request.method == 'POST': 10 | if request.form['username'] != 'admin' or request.form['password'] != 'admin123': 11 | error= "sorry" 12 | else: 13 | return redirect(url_for('index')) 14 | return render_template('login.html',error=error) 15 | 16 | 17 | @app.route("/index") 18 | def index(): 19 | return render_template('index.html') 20 | 21 | 22 | if __name__ == "__main__": 23 | app.run( 24 | host="0.0.0.0", 25 | port=80, 26 | debug=True) 27 | -------------------------------------------------------------------------------- /flask/login/static/css/style.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/login/static/css/style.css -------------------------------------------------------------------------------- /flask/login/templates/index.html: -------------------------------------------------------------------------------- 1 | welcome to www.361way.com 2 | -------------------------------------------------------------------------------- /flask/login/templates/login.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Bootstrap响应式登录界面模板 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 56 |
57 | 58 |
59 |

来源:运维之路

60 |
61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | from flask import Flask, request, render_template, jsonify, json 4 | from flask.ext.sqlalchemy import SQLAlchemy 5 | 6 | app = Flask(__name__) 7 | db = SQLAlchemy(app,use_native_unicode="utf8") 8 | 9 | app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:cacti@localhost/web12306' 10 | 11 | 12 | 13 | 14 | class session(db.Model): 15 | __tablename__ = 'web12306' 16 | user_id = db.Column(db.String(100), primary_key = True) 17 | user_email = db.Column(db.String(100)) 18 | user_pass = db.Column(db.String(100)) 19 | user_nic = db.Column(db.String(100)) 20 | user_phone = db.Column(db.String(100)) 21 | user_name = db.Column(db.String(100)) 22 | 23 | 24 | 25 | 26 | 27 | @app.route('/scan/', methods=['GET']) 28 | def scan(user_id): 29 | result = session.query.filter_by(user_id=user_id).first() 30 | if result is None: 31 | json_result={'user_id':None} 32 | return json.dumps(json_result,ensure_ascii=False) 33 | else: 34 | json_result = {'user_id': result.user_id, 'user_email': result.user_email, 'user_pass': result.user_pass, 'user_nic': result.user_nic, 'user_phone': result.user_phone, 'user_name': result.user_name} 35 | print json_result 36 | return json.dumps(json_result,ensure_ascii=False) 37 | 38 | 39 | @app.route('/') 40 | def index(): 41 | return render_template('new.html') 42 | 43 | 44 | 45 | if __name__ == '__main__': 46 | app.run(host='0.0.0.0', port = 8080, debug=True) 47 | -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/bootstrap-3.3.4-dist.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/mysql_query/beautiful/static/bootstrap-3.3.4-dist.zip -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/flask/mysql_query/beautiful/static/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /flask/mysql_query/beautiful/static/js/npm.js: -------------------------------------------------------------------------------- 1 | // This file is autogenerated via the `commonjs` Grunt task. You can require() this file in a CommonJS environment. 2 | require('../../js/transition.js') 3 | require('../../js/alert.js') 4 | require('../../js/button.js') 5 | require('../../js/carousel.js') 6 | require('../../js/collapse.js') 7 | require('../../js/dropdown.js') 8 | require('../../js/modal.js') 9 | require('../../js/tooltip.js') 10 | require('../../js/popover.js') 11 | require('../../js/scrollspy.js') 12 | require('../../js/tab.js') 13 | require('../../js/affix.js') -------------------------------------------------------------------------------- /flask/mysql_query/simple/demo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | from flask import Flask, jsonify, render_template, request 4 | app = Flask(__name__) 5 | 6 | @app.route('/') 7 | def index(): 8 | return render_template('index.html') 9 | 10 | @app.route('/echo/', methods=['GET']) 11 | def echo(): 12 | ret_data = {"value": request.args.get('echoValue')} 13 | return jsonify(ret_data) 14 | 15 | if __name__ == '__main__': 16 | app.run(host="0.0.0.0", debug=True) 17 | -------------------------------------------------------------------------------- /flask/mysql_query/simple/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | from flask import Flask, request, render_template, jsonify, json 4 | from flask.ext.sqlalchemy import SQLAlchemy 5 | 6 | app = Flask(__name__) 7 | db = SQLAlchemy(app,use_native_unicode="utf8") 8 | 9 | app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:cacti@localhost/web12306' 10 | 11 | 12 | 13 | 14 | class session(db.Model): 15 | __tablename__ = 'web12306' 16 | user_id = db.Column(db.String(100), primary_key = True) 17 | user_email = db.Column(db.String(100)) 18 | user_pass = db.Column(db.String(100)) 19 | user_nic = db.Column(db.String(100)) 20 | user_phone = db.Column(db.String(100)) 21 | user_name = db.Column(db.String(100)) 22 | 23 | 24 | 25 | 26 | 27 | @app.route('/scan/', methods=['GET']) 28 | def scan(user_id): 29 | result = session.query.filter_by(user_id=user_id).first() 30 | if result is None: 31 | json_result={'user_id':None} 32 | return json.dumps(json_result,ensure_ascii=False) 33 | else: 34 | json_result = {'user_id': result.user_id, 'user_email': result.user_email, 'user_pass': result.user_pass, 'user_nic': result.user_nic, 'user_phone': result.user_phone, 'user_name': result.user_name} 35 | print json_result 36 | return json.dumps(json_result,ensure_ascii=False) 37 | 38 | 39 | @app.route('/') 40 | def index(): 41 | return render_template('index.html') 42 | 43 | 44 | 45 | if __name__ == '__main__': 46 | app.run(host='0.0.0.0', debug=True) 47 | -------------------------------------------------------------------------------- /flask/mysql_query/simple/readme.txt: -------------------------------------------------------------------------------- 1 | 1、create database and tables 2 | 3 | #建库 4 | CREATE DATABASE `web12306` DEFAULT CHARACTER SET utf8; 5 | 6 | #建表 7 | web12306 | CREATE TABLE `web12306` ( 8 | `user_email` varchar(100) NOT NULL DEFAULT '', 9 | `user_pass` varchar(100) NOT NULL DEFAULT '', 10 | `user_name` varchar(100) NOT NULL DEFAULT '', 11 | `user_id` varchar(100) NOT NULL DEFAULT '', 12 | `user_nic` varchar(100) NOT NULL DEFAULT '', 13 | `user_phone` varchar(100) NOT NULL DEFAULT '' 14 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 15 | 16 | #测试sql data 17 | mysql> insert into web12306 values 18 | ('test@361way.com','test','运维之路','410221','www.361way.com','13800000000'); 19 | 20 | 21 | 2、run the code 22 | python main.py 23 | 24 | 3、open the browser and insert the http://ip:5000 25 | query the information use the user_id 26 | -------------------------------------------------------------------------------- /flask/mysql_query/simple/templates/index.html: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% block content %} 3 | 10 | Enter a value to echo back: 11 | 12 |

13 |
14 | {% endblock %} 15 | -------------------------------------------------------------------------------- /flask/mysql_query/simple/templates/index.html.bak: -------------------------------------------------------------------------------- 1 | {% extends "layout.html" %} 2 | {% block content %} 3 | 16 | Enter a value to echo back: 17 | 18 |

19 |
20 | {% endblock %} 21 | -------------------------------------------------------------------------------- /flask/mysql_query/simple/templates/layout.html: -------------------------------------------------------------------------------- 1 | Flask AJAX Demo 2 | 3 | 6 | 7 | 8 | 11 | 12 | 13 | {% block content %}{% endblock %} 14 | -------------------------------------------------------------------------------- /flask_monitor/monitems.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding=utf-8 3 | import inspect 4 | import time 5 | import urllib, urllib2 6 | import json 7 | import socket 8 | 9 | class mon: 10 | def __init__(self): 11 | self.data = {} 12 | 13 | def getTime(self): 14 | return str(int(time.time()) + 8 * 3600) 15 | 16 | def getHost(self): 17 | return socket.gethostname() 18 | 19 | def getLoadAvg(self): 20 | with open('/proc/loadavg') as load_open: 21 | a = load_open.read().split()[:3] 22 | return ','.join(a) 23 | 24 | def getMemTotal(self): 25 | with open('/proc/meminfo') as mem_open: 26 | a = int(mem_open.readline().split()[1]) 27 | return a / 1024 28 | 29 | def getMemUsage(self, noBufferCache=True): 30 | if noBufferCache: 31 | with open('/proc/meminfo') as mem_open: 32 | T = int(mem_open.readline().split()[1]) 33 | F = int(mem_open.readline().split()[1]) 34 | B = int(mem_open.readline().split()[1]) 35 | C = int(mem_open.readline().split()[1]) 36 | return (T-F-B-C)/1024 37 | else: 38 | with open('/proc/meminfo') as mem_open: 39 | a = int(mem_open.readline().split()[1]) - int(mem_open.readline().split()[1]) 40 | return a / 1024 41 | 42 | def getMemFree(self, noBufferCache=True): 43 | if noBufferCache: 44 | with open('/proc/meminfo') as mem_open: 45 | T = int(mem_open.readline().split()[1]) 46 | F = int(mem_open.readline().split()[1]) 47 | B = int(mem_open.readline().split()[1]) 48 | C = int(mem_open.readline().split()[1]) 49 | return (F+B+C)/1024 50 | else: 51 | with open('/proc/meminfo') as mem_open: 52 | mem_open.readline() 53 | a = int(mem_open.readline().split()[1]) 54 | return a / 1024 55 | 56 | def runAllGet(self): 57 | #自动获取mon类里的所有getXXX方法,用XXX作为key,getXXX()的返回值作为value,构造字典 58 | for fun in inspect.getmembers(self, predicate=inspect.ismethod): 59 | if fun[0][:3] == 'get': 60 | self.data[fun[0][3:]] = fun[1]() 61 | return self.data 62 | 63 | if __name__ == "__main__": 64 | while True: 65 | m = mon() 66 | data = m.runAllGet() 67 | print data 68 | req = urllib2.Request("http://127.0.0.1:8888", json.dumps(data), {'Content-Type': 'application/json'}) 69 | f = urllib2.urlopen(req) 70 | response = f.read() 71 | print response 72 | f.close() 73 | time.sleep(60) 74 | -------------------------------------------------------------------------------- /flask_monitor/web/flask_web.py: -------------------------------------------------------------------------------- 1 | import MySQLdb as mysql 2 | import json 3 | from flask import Flask, request, render_template 4 | app = Flask(__name__) 5 | db = mysql.connect(user="root", passwd="cacti", \ 6 | db="falcon", charset="utf8") 7 | db.autocommit(True) 8 | c = db.cursor() 9 | 10 | @app.route("/", methods=["GET", "POST"]) 11 | def hello(): 12 | sql = "" 13 | if request.method == "POST": 14 | data = request.json 15 | try: 16 | sql = "INSERT INTO `stat` (`host`,`mem_free`,`mem_usage`,`mem_total`,`load_avg`,`time`) VALUES('%s', '%d', '%d', '%d', '%s', '%d')" % (data['Host'], data['MemFree'], data['MemUsage'], data['MemTotal'], data['LoadAvg'], int(data['Time'])) 17 | ret = c.execute(sql) 18 | except mysql.IntegrityError: 19 | pass 20 | return "OK" 21 | else: 22 | return render_template("mon.html") 23 | 24 | @app.route("/data", methods=["GET"]) 25 | def getdata(): 26 | c.execute("SELECT `time`,`mem_usage` FROM `stat`") 27 | ones = [[i[0]*1000, i[1]] for i in c.fetchall()] 28 | return "%s(%s);" % (request.args.get('callback'), json.dumps(ones)) 29 | 30 | 31 | if __name__ == "__main__": 32 | app.run(host="0.0.0.0", port=8888, debug=True) 33 | -------------------------------------------------------------------------------- /flask_monitor/web/templates/mon.html: -------------------------------------------------------------------------------- 1 | memory monitor 2 | 3 | 4 | 5 | 6 | Highstock Example 7 | 8 | 9 | 10 | 13 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 |
50 | 51 | 52 | -------------------------------------------------------------------------------- /get_pid.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | 3 | import os 4 | from collections import namedtuple 5 | 6 | PAGESIZE = os.sysconf("SC_PAGE_SIZE") 7 | print PAGESIZE 8 | pextmem = namedtuple('pextmem', 'rss vms shared text lib data dirty') 9 | 10 | 11 | def pids(): 12 | return [int(x) for x in os.listdir(b'/proc') if x.isdigit()] 13 | 14 | def memory_info_ex(pid): 15 | # ============================================================ 16 | # | FIELD | DESCRIPTION | AKA | TOP | 17 | # ============================================================ 18 | # | rss | resident set size | | RES | 19 | # | vms | total program size | size | VIRT | 20 | # | shared | shared pages (from shared mappings) | | SHR | 21 | # | text | text ('code') | trs | CODE | 22 | # | lib | library (unused in Linux 2.6) | lrs | | 23 | # | data | data + stack | drs | DATA | 24 | # | dirty | dirty pages (unused in Linux 2.6) | dt | | 25 | # ============================================================ 26 | with open("/proc/%s/statm" % pid, "rb") as f: 27 | vms, rss, shared, text, lib, data, dirty = \ 28 | [int(x) * PAGESIZE for x in f.readline().split()[:7]] 29 | return pextmem(rss, vms, shared, text, lib, data, dirty) 30 | 31 | pids = pids() 32 | 33 | for pid in pids: 34 | mem_info = memory_info_ex(pid) 35 | print pid,mem_info 36 | 37 | -------------------------------------------------------------------------------- /get_user.py: -------------------------------------------------------------------------------- 1 | import pwd 2 | 3 | def get_user(): 4 | all_user = {} 5 | for user in pwd.getpwall(): 6 | all_user[user[0]] = all_user[user[2]] = user 7 | return all_user 8 | 9 | def userinfo(uid): 10 | return get_user()[uid] 11 | 12 | print userinfo(0) 13 | print userinfo('root') 14 | -------------------------------------------------------------------------------- /getattr/funab.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | def funa(): 4 | print 'running funa ......' 5 | 6 | def funb(): 7 | print 'running funb ......' 8 | -------------------------------------------------------------------------------- /getattr/get.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## author: yangbk(itybku@139.com) 4 | ## site: www.361way.com 5 | 6 | class getfun: 7 | def funa(self): 8 | print 'processNavigate' 9 | 10 | def funb(self): 11 | print 'processCreateTab' 12 | 13 | 14 | action = raw_input('请输入地址:') 15 | newfun = getfun() 16 | fun = getattr(newfun,action) 17 | fun() 18 | -------------------------------------------------------------------------------- /getattr/get2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | action = raw_input('请输入地址:') 5 | newfun = __import__('funab') 6 | fun = getattr(newfun,action) 7 | fun() 8 | -------------------------------------------------------------------------------- /getattr/main.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## author: yangbk(itybku@139.com) 4 | ## site: www.361way.com 5 | 6 | def funa(): 7 | print 'running funa ......' 8 | 9 | def funb(): 10 | print 'running funb ......' 11 | 12 | action = raw_input('请输入地址:') 13 | if action == "funa": 14 | funa() 15 | elif action == "funb": 16 | funb() 17 | -------------------------------------------------------------------------------- /getattr/map.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## author: yangbk(itybku@139.com) 4 | ## site: www.361way.com 5 | 6 | def funa(): 7 | print 'processNavigate' 8 | 9 | def funb(): 10 | print 'processCreateTab' 11 | 12 | action = raw_input('请输入地址:') 13 | actionMap = {"processNavigate":funa ,"processCreateTab":funb} 14 | actionMap[action]() 15 | -------------------------------------------------------------------------------- /getattr/web/backend/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/getattr/web/backend/__init__.py -------------------------------------------------------------------------------- /getattr/web/backend/account.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | def login(): 4 | print 'login now ******* ' 5 | 6 | def logout(): 7 | print 'logout now ******* ' 8 | -------------------------------------------------------------------------------- /getattr/web/backend/admin.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | def index(): 4 | print 'welcome to your, administrator !' 5 | -------------------------------------------------------------------------------- /getattr/web/index.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | from backend import account 5 | data = raw_input('请输入地址:') 6 | if data == 'account/login': 7 | account.login() 8 | elif data == 'account/logout': 9 | account.logout() 10 | -------------------------------------------------------------------------------- /getattr/web/web.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | ## author: yangbk(itybku@139.com) 4 | ## site: www.361way.com 5 | 6 | ''' 7 | from backend import account 8 | data = raw_input('请输入地址:') 9 | if data == 'account/login': 10 | account.login() 11 | elif data == 'account/logout': 12 | account.logout() 13 | ''' 14 | data = raw_input('请输入地址:') 15 | array = data.split('/') 16 | 17 | userspance = __import__('backend.'+array[0]) 18 | model = getattr(userspance , array[0]) 19 | func = getattr(model,array[1]) 20 | func() 21 | -------------------------------------------------------------------------------- /getattr/xg.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # coding=utf-8 3 | ## author: yangbk(itybku@139.com) 4 | ## site: www.361way.com 5 | ## desc: test inspect module || hasattr、setattr 、delattr and getattr 6 | 7 | class test: 8 | def __init__(self): 9 | self.name = 'www.361way.com' 10 | def setName(self,name): 11 | self.name = name 12 | def getName(self): 13 | return self.name 14 | def greet(self): 15 | print "Hello,I'm %s" %self.name 16 | foo = test() 17 | 18 | # test for inspect 19 | im = foo.greet 20 | import inspect 21 | if inspect.isroutine(im): 22 | im() 23 | 24 | # test for hasattr getattr setattr delattr 25 | print hasattr(foo,'abc') 26 | print hasattr(foo,'getName') 27 | 28 | setattr(foo,'age',18) 29 | print getattr(foo,'age') 30 | 31 | print getattr(foo,'name') 32 | 33 | setattr(foo,'name','newname') 34 | print getattr(foo,'name') 35 | 36 | delattr(foo,'name') 37 | print getattr(foo,'name','not find') 38 | 39 | 40 | -------------------------------------------------------------------------------- /gevent/echoserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | from gevent.server import StreamServer 5 | 6 | def connection_handler(socket, address): 7 | for l in socket.makefile('r'): 8 | socket.sendall(l) 9 | 10 | if __name__ == '__main__': 11 | server = StreamServer(('0.0.0.0', 8000), connection_handler) 12 | server.serve_forever() 13 | -------------------------------------------------------------------------------- /gevent/mgeturl.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | from gevent import monkey 5 | monkey.patch_all() 6 | 7 | import urllib2 8 | from gevent.pool import Pool 9 | 10 | def geturls(): 11 | urls = [] 12 | for v in range(0,1000,10): 13 | url = 'http://www.baidu.com/s?wd=site%3A361way.com&pn=' + str(v) 14 | urls.append(url) 15 | return urls 16 | def download(url): 17 | return urllib2.urlopen(url).read() 18 | 19 | if __name__ == '__main__': 20 | urls = geturls() 21 | pool = Pool(20) 22 | print pool.map(download, urls) 23 | -------------------------------------------------------------------------------- /gevent/talkserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | import gevent 5 | from gevent.queue import Queue 6 | from gevent.server import StreamServer 7 | 8 | users = {} # mapping of username -> Queue 9 | 10 | def broadcast(msg): 11 | msg += '\n' 12 | for v in users.values(): 13 | v.put(msg) 14 | 15 | def reader(username, f): 16 | for l in f: 17 | msg = '%s> %s' % (username, l.strip()) 18 | broadcast(msg) 19 | 20 | def writer(q, sock): 21 | while True: 22 | msg = q.get() 23 | sock.sendall(msg) 24 | 25 | def read_name(f, sock): 26 | while True: 27 | sock.sendall('Please enter your name: ') 28 | name = f.readline().strip() 29 | if name: 30 | if name in users: 31 | sock.sendall('That username is already taken.\n') 32 | else: 33 | return name 34 | 35 | def handle(sock, client_addr): 36 | f = sock.makefile() 37 | 38 | name = read_name(f, sock) 39 | 40 | broadcast('## %s joined from %s.' % (name, client_addr[0])) 41 | 42 | q = Queue() 43 | users[name] = q 44 | 45 | try: 46 | r = gevent.spawn(reader, name, f) 47 | w = gevent.spawn(writer, q, sock) 48 | gevent.joinall([r, w]) 49 | finally: 50 | del(users[name]) 51 | broadcast('## %s left the chat.' % name) 52 | 53 | if __name__ == '__main__': 54 | import sys 55 | try: 56 | myip = sys.argv[1] 57 | except IndexError: 58 | myip = '0.0.0.0' 59 | 60 | print 'To join, telnet %s 8001' % myip 61 | s = StreamServer((myip, 8001), handle) 62 | s.serve_forever() 63 | -------------------------------------------------------------------------------- /grab/51cto/51cto.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/grab/51cto/51cto.tar.gz -------------------------------------------------------------------------------- /grab/51cto/img.py: -------------------------------------------------------------------------------- 1 | # ImageDownloader.py 2 | # Finds and downloads all images from any given URL recursively. 3 | # FB - 20140223 4 | import sys 5 | import os 6 | import urllib2 7 | from os.path import basename 8 | import urlparse 9 | from BeautifulSoup import BeautifulSoup # for HTML parsing 10 | 11 | urlList = [] 12 | 13 | # recursively download images starting from the root URL 14 | def downloadImages(url, level): # the root URL is level 0 15 | # do not go to other websites 16 | global website 17 | netloc = urlparse.urlsplit(url).netloc.split('.') 18 | if netloc[-2] + netloc[-1] != website: 19 | return 20 | 21 | global urlList 22 | if url in urlList: # prevent using the same URL again 23 | return 24 | 25 | try: 26 | urlContent = urllib2.urlopen(url).read() 27 | urlList.append(url) 28 | print url 29 | except: 30 | return 31 | 32 | soup = BeautifulSoup(''.join(urlContent)) 33 | # find and download all images 34 | imgTags = soup.findAll('img') 35 | for imgTag in imgTags: 36 | imgUrl = imgTag['src'] 37 | imgUrl = url[ : url.find(".com") + 4] + imgUrl if (imgUrl[ : 4] != "http") else imgUrl 38 | # download only the proper image files 39 | if imgUrl.lower().endswith('.jpeg') or \ 40 | imgUrl.lower().endswith('.jpg') or \ 41 | imgUrl.lower().endswith('.gif') or \ 42 | imgUrl.lower().endswith('.png') or \ 43 | imgUrl.lower().endswith('.bmp'): 44 | try: 45 | imgData = urllib2.urlopen(imgUrl).read() 46 | global minImageFileSize 47 | if len(imgData) >= minImageFileSize: 48 | print " " + imgUrl 49 | fileName = basename(urlparse.urlsplit(imgUrl)[2]) 50 | output = open(os.path.join(downloadLocationPath, fileName),'wb') 51 | output.write(imgData) 52 | output.close() 53 | except Exception, e: 54 | print str(e) 55 | # pass 56 | print 57 | print 58 | 59 | # if there are links on the webpage then recursively repeat 60 | if level > 0: 61 | linkTags = soup.findAll('a') 62 | if len(linkTags) > 0: 63 | for linkTag in linkTags: 64 | try: 65 | linkUrl = linkTag['href'] 66 | downloadImages(linkUrl, level - 1) 67 | except Exception, e: 68 | print str(e) 69 | # pass 70 | 71 | # MAIN 72 | cla = sys.argv # command line arguments 73 | if len(cla) != 5: 74 | print "USAGE:" 75 | print "[python] ImageDownloader.py URL MaxRecursionDepth DownloadLocationPath MinImageFileSize" 76 | os._exit(1) 77 | 78 | rootUrl = cla[1] 79 | maxRecursionDepth = int(cla[2]) 80 | downloadLocationPath = cla[3] # absolute path 81 | if not os.path.isdir(downloadLocationPath): 82 | print downloadLocationPath + " is not an existing directory!" 83 | os._exit(2) 84 | 85 | minImageFileSize = long(cla[4]) # in bytes 86 | netloc = urlparse.urlsplit(rootUrl).netloc.split('.') 87 | website = netloc[-2] + netloc[-1] 88 | downloadImages(rootUrl, maxRecursionDepth) 89 | -------------------------------------------------------------------------------- /grab/51cto/insert.py: -------------------------------------------------------------------------------- 1 | # encoding: utf-8 2 | #!/usr/bin/python 3 | 4 | import MySQLdb 5 | 6 | # 打开数据库连接 7 | db = MySQLdb.connect("localhost","root","111111","test" ) 8 | 9 | # 使用cursor()方法获取操作游标 10 | cursor = db.cursor() 11 | 12 | # SQL 插入语句 13 | sql = """INSERT INTO EMPLOYEE(FIRST_NAME, 14 | LAST_NAME, AGE, SEX, INCOME) 15 | VALUES ('Mac', 'Mohan', 20, 'M', 2000)""" 16 | try: 17 | # 执行sql语句 18 | cursor.execute(sql) 19 | # 提交到数据库执行 20 | db.commit() 21 | except: 22 | # Rollback in case there is any error 23 | db.rollback() 24 | 25 | # 关闭数据库连接 26 | db.close() 27 | -------------------------------------------------------------------------------- /grab/51cto/moudle/curl.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/grab/51cto/moudle/curl.pyc -------------------------------------------------------------------------------- /grab/51cto/moudle/iconv.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | 4 | from ctypes import * 5 | from ctypes.util import find_library 6 | 7 | __all__ = ["iconv"] 8 | 9 | def iconv(from_code, to_code, string): 10 | """ 11 | from_code: 解码前字符串字符集 12 | to_code: 解码后字符串字符集 13 | string: 需要解码的数据 14 | """ 15 | 16 | # 加载libc库 17 | _libc = CDLL(find_library("libc.6.so")) 18 | 19 | # 定义iconv_t指针类型 20 | _iconv_t = c_void_p 21 | 22 | # 声明iconv_open(3)原型 23 | _iconv_open = _libc.iconv_open 24 | _iconv_open.argtypes = [c_char_p, c_char_p] 25 | _iconv_open.restype = _iconv_t 26 | 27 | # 打开iconv句柄 28 | cp = _iconv_open(c_char_p(to_code), c_char_p(from_code)) 29 | 30 | # 初始化输入/输出缓存 31 | inlen = len(string) 32 | inbuf = create_string_buffer(string, inlen) 33 | inbytesleft = c_size_t(len(inbuf)) 34 | p_inbuf = pointer(inbuf) 35 | 36 | outlen = inlen * 4 + 4 37 | outbuf = create_string_buffer(outlen) 38 | outbytesleft = c_size_t(len(outbuf)) 39 | p_outbuf = pointer(outbuf) 40 | 41 | # 声明iconv(3)原型 42 | _iconv = _libc.iconv 43 | _iconv.argtypes = [_iconv_t, 44 | POINTER(POINTER(ARRAY(c_char, inlen))), POINTER(c_size_t), 45 | POINTER(POINTER(ARRAY(c_char, outlen))), POINTER(c_size_t)] 46 | _iconv.restype = c_size_t 47 | 48 | # iconv编码转换 49 | _iconv(cp, byref(p_inbuf), byref(inbytesleft), byref(p_outbuf), byref(outbytesleft)) 50 | result = outbuf.value[:outbytesleft.value] 51 | 52 | # 声明iconv_close(3)原型 53 | _iconv_close = _libc.iconv_close 54 | _iconv_close.argtypes = [_iconv_t] 55 | _iconv_close.restype = c_int 56 | 57 | # 关闭iconv句柄 58 | _iconv_close(cp) 59 | 60 | return result 61 | 62 | if __name__ == "__main__": 63 | 64 | # 声明本地语言环境 65 | from locale import setlocale, LC_ALL 66 | 67 | setlocale(LC_ALL, 'en_US.UTF-8') 68 | 69 | t = u"你好,世界!" 70 | i = iconv("UTF-8", "GB18030", t.encode('UTF-8')) 71 | print u"TEST: IN: '%s'" % t 72 | print u"TEST: OUT: '%s'" % i.decode('GB18030') 73 | -------------------------------------------------------------------------------- /grab/51cto/moudle/iconv.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/grab/51cto/moudle/iconv.pyc -------------------------------------------------------------------------------- /grab/51cto/moudle/test.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import sys 4 | reload(sys) 5 | sys.setdefaultencoding('utf-8') 6 | import time 7 | import urllib3 8 | import re 9 | import string 10 | import StringIO 11 | import urllib2 12 | import datetime 13 | import random 14 | import os 15 | #from tools import * 16 | from curl import * 17 | #from iconv import iconv 18 | 19 | title_urls = [] 20 | now = time.time() 21 | ''' 22 | for id in range(1,2): 23 | url = 'http://redking.blog.51cto.com/all/27212/5/page/%s' % id 24 | pool = urllib3.PoolManager() 25 | r = pool.request('GET', url, assert_same_host=False) 26 | title_url = re.findall(".*?\/a>", r.data, re.S) 27 | title_urls.extend(title_url) 28 | ''' 29 | #title_urls = list(set(title_urls)) 30 | title_urls = ['RHCE课程-RH131Linux管理笔记四-Linux的计划任务'] 31 | 32 | 33 | 34 | for i in title_urls: 35 | pre_url = re.findall(r'href=[\'"]?([^">]+)', i) 36 | title = re.findall(r'\d+">?([^"<]+)', i) 37 | if str(pre_url[0]).startswith('/'): 38 | url = 'http://redking.blog.51cto.com' + pre_url[0] 39 | else: 40 | url = 'http://redking.blog.51cto.com/' + pre_url[0] 41 | print 'now start sprid the page' 42 | print str(url),title[0].decode('gb2312','ignore').encode('utf-8', 'ignore') 43 | blog_url = str(url) 44 | c = initCurl() 45 | html = GetDate(c, blog_url) 46 | html = html.decode('gb2312','ignore').encode('utf-8', 'ignore') 47 | #before = html.split('
',1)[1] 48 | before = html.split('',1)[1] 49 | content = before.split('',1)[0] 50 | ############################get the imgs ,replace the img url################################ 51 | img=re.compile(r"""""",re.I) 52 | img_urls = img.findall(content) 53 | img_urls = list(set(img_urls)) 54 | content = formatHtml(content) 55 | content = herf_Img(content) 56 | print content 57 | -------------------------------------------------------------------------------- /grab/51cto/py.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/grab/51cto/py.tar.gz -------------------------------------------------------------------------------- /grab/51cto/python/pycurl/async.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding=utf-8 3 | 4 | import threading 5 | import pycurl 6 | from cStringIO import StringIO 7 | 8 | class UrlOpen(threading.Thread): 9 | """异步下载网页""" 10 | 11 | def __init__(self): 12 | super(UrlOpen,self).__init__() 13 | self.opener = pycurl.CurlMulti() 14 | self.handle_list=[] 15 | 16 | def add(self,url,recall,writer=StringIO()): 17 | """ 18 | 参数:网址,回调函数,存放临时数据的对象 19 | """ 20 | c = pycurl.Curl() 21 | 22 | #可以传给回调函数 23 | c.url=url 24 | c.content = writer 25 | c.recall = recall 26 | c.setopt(c.URL,url) 27 | c.setopt(c.WRITEFUNCTION,c.content.write) 28 | 29 | self.handle_list.append(c) 30 | self.opener.add_handle(c) 31 | 32 | def _remove(self,c): 33 | c.close() 34 | self.opener.remove_handle(c) 35 | self.handle_list.remove(c) 36 | 37 | 38 | def run(self): 39 | num_handle=len(self.handle_list) 40 | while 1: 41 | ret = self.opener.select(10.0) 42 | if ret == -1: continue 43 | while 1: 44 | num_handle_pre=num_handle 45 | ret, num_handle =self.opener.perform() 46 | #活动的连接数改变时 47 | if num_handle!=num_handle_pre: 48 | result=self.opener.info_read() 49 | print result 50 | for i in result[1]: 51 | #成功 52 | i.http_code = i.getinfo(i.HTTP_CODE) 53 | self._remove(i) 54 | i.recall(i) 55 | for i in result[2]: 56 | #失败,应该记录一下 57 | self._remove(i) 58 | 59 | if ret != pycurl.E_CALL_MULTI_PERFORM: 60 | break 61 | 62 | _opener=None 63 | def urlopen(*arg,**key): 64 | global _opener 65 | if _opener is None: 66 | _opener=UrlOpen() 67 | _opener.add(*arg,**key) 68 | _opener.start() 69 | else: 70 | _opener.add(*arg,**key) 71 | 72 | def show(x): 73 | print x.content.getvalue() 74 | if __name__=="__main__": 75 | urlopen("http://blog.linuxeye.com/400.html",show) 76 | _opener.join() 77 | -------------------------------------------------------------------------------- /grab/51cto/python/pycurl/init.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding: utf-8 3 | import pycurl 4 | import StringIO 5 | 6 | def initCurl(): 7 | c = pycurl.Curl() 8 | c.setopt(pycurl.COOKIEFILE, "cookie_file_name")#把cookie保存在该文件中 9 | c.setopt(pycurl.COOKIEJAR, "cookie_file_name") 10 | c.setopt(pycurl.FOLLOWLOCATION, 1) #允许跟踪来源 11 | c.setopt(pycurl.MAXREDIRS, 5) 12 | #设置代理 如果有需要请去掉注释,并设置合适的参数 13 | #c.setopt(pycurl.PROXY, ‘http://11.11.11.11:8080′) 14 | #c.setopt(pycurl.PROXYUSERPWD, ‘aaa:aaa’) 15 | return c 16 | 17 | 18 | 19 | def GetDate(curl, url): 20 | head = ['Accept:*/*', 21 | 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'] 22 | buf = StringIO.StringIO() 23 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 24 | curl.setopt(pycurl.URL, url) 25 | curl.setopt(pycurl.HTTPHEADER, head) 26 | curl.perform() 27 | the_page =buf.getvalue() 28 | buf.close() 29 | return the_page 30 | 31 | 32 | def PostData(curl, url, data): 33 | head = ['Accept:*/*', 34 | 'Content-Type:application/xml', 35 | 'render:json', 36 | 'clientType:json', 37 | 'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3', 38 | 'Accept-Encoding:gzip,deflate,sdch', 39 | 'Accept-Language:zh-CN,zh;q=0.8', 40 | 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'] 41 | buf = StringIO.StringIO() 42 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 43 | curl.setopt(pycurl.POSTFIELDS, data) 44 | curl.setopt(pycurl.URL, url) 45 | curl.setopt(pycurl.HTTPHEADER, head) 46 | curl.perform() 47 | the_page = buf.getvalue() 48 | #print the_page 49 | buf.close() 50 | return the_page 51 | 52 | 53 | c = initCurl() 54 | html = GetDate(c, 'http://blog.linuxeye.com/400.html') 55 | print html 56 | -------------------------------------------------------------------------------- /grab/51cto/python/pycurl/split.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding: utf-8 3 | import pycurl 4 | import StringIO 5 | 6 | def initCurl(): 7 | c = pycurl.Curl() 8 | c.setopt(pycurl.COOKIEFILE, "cookie_file_name")#把cookie保存在该文件中 9 | c.setopt(pycurl.COOKIEJAR, "cookie_file_name") 10 | c.setopt(pycurl.FOLLOWLOCATION, 1) #允许跟踪来源 11 | c.setopt(pycurl.MAXREDIRS, 5) 12 | #设置代理 如果有需要请去掉注释,并设置合适的参数 13 | #c.setopt(pycurl.PROXY, ‘http://11.11.11.11:8080′) 14 | #c.setopt(pycurl.PROXYUSERPWD, ‘aaa:aaa’) 15 | return c 16 | 17 | 18 | 19 | def GetDate(curl, url): 20 | head = ['Accept:*/*', 21 | 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'] 22 | buf = StringIO.StringIO() 23 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 24 | curl.setopt(pycurl.URL, url) 25 | curl.setopt(pycurl.HTTPHEADER, head) 26 | curl.perform() 27 | the_page =buf.getvalue() 28 | buf.close() 29 | return the_page 30 | 31 | 32 | def PostData(curl, url, data): 33 | head = ['Accept:*/*', 34 | 'Content-Type:application/xml', 35 | 'render:json', 36 | 'clientType:json', 37 | 'Accept-Charset:GBK,utf-8;q=0.7,*;q=0.3', 38 | 'Accept-Encoding:gzip,deflate,sdch', 39 | 'Accept-Language:zh-CN,zh;q=0.8', 40 | 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'] 41 | buf = StringIO.StringIO() 42 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 43 | curl.setopt(pycurl.POSTFIELDS, data) 44 | curl.setopt(pycurl.URL, url) 45 | curl.setopt(pycurl.HTTPHEADER, head) 46 | curl.perform() 47 | the_page = buf.getvalue() 48 | #print the_page 49 | buf.close() 50 | return the_page 51 | 52 | 53 | c = initCurl() 54 | html = GetDate(c, 'http://blog.linuxeye.com/400.html') 55 | before = html.split('',1)[1] 56 | content = before.split('',1)[0] 57 | #print "'''%s'''" %(content) 58 | print '"""%s"""' %(content) 59 | -------------------------------------------------------------------------------- /grab/51cto/python/pycurl/url.py: -------------------------------------------------------------------------------- 1 | import pycurl 2 | import StringIO 3 | 4 | url = "http://www.361way.com/python-mysqldb/3842.html" 5 | crl = pycurl.Curl() 6 | crl.setopt(pycurl.USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0)") 7 | crl.setopt(pycurl.VERBOSE,1) 8 | crl.setopt(pycurl.FOLLOWLOCATION, 1) 9 | crl.setopt(pycurl.MAXREDIRS, 5) 10 | crl.fp = StringIO.StringIO() 11 | crl.setopt(pycurl.URL, url) 12 | crl.setopt(crl.WRITEFUNCTION, crl.fp.write) 13 | crl.perform() 14 | print crl.fp.getvalue() 15 | -------------------------------------------------------------------------------- /grab/51cto/python/title_url.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | #import sys,os 4 | import time 5 | import urllib3 6 | import re 7 | 8 | #url = "http://www.361way.com/python-mysqldb/3842.html" 9 | #url = "http://segmentfault.com/blog/wangbinke/1190000000351425" 10 | url = "http://segmentfault.com/t/php/blogs?page=25" 11 | now = time.time() 12 | pool = urllib3.PoolManager() 13 | r = pool.request('GET', url, assert_same_host=False) 14 | #title = re.findall("
  • ]+)', r.data) 19 | #urls = re.findall(r'href=[\'"]?([^">]+)', r.data) 20 | title_urls = re.findall(r'
  • ]+)', i) 23 | # title = re.findall(r'title=[\'"].*?\"',i) 24 | # title = re.findall(r'title=".*?"',i) 25 | title = re.findall(r'title=[\'"]?([^">]+)"',i) 26 | 27 | print str(url[0]),title[0] 28 | 29 | #print title_urls[0] 30 | #print urls[169] 31 | elapsed = time.time() - now 32 | #print r.data , elapsed 33 | print elapsed 34 | -------------------------------------------------------------------------------- /grab/51cto/python/url.py: -------------------------------------------------------------------------------- 1 | import pycurl 2 | import StringIO 3 | import time 4 | 5 | now = time.time() 6 | url = "http://www.361way.com/python-mysqldb/3842.html" 7 | crl = pycurl.Curl() 8 | crl.setopt(pycurl.USERAGENT,"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0)") 9 | crl.setopt(pycurl.VERBOSE,1) 10 | crl.setopt(pycurl.FOLLOWLOCATION, 1) 11 | crl.setopt(pycurl.MAXREDIRS, 5) 12 | crl.fp = StringIO.StringIO() 13 | crl.setopt(pycurl.URL, url) 14 | crl.setopt(crl.WRITEFUNCTION, crl.fp.write) 15 | crl.perform() 16 | costtime = time.time() - now 17 | print crl.fp.getvalue(),costtime 18 | -------------------------------------------------------------------------------- /grab/51cto/python/urlib3.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import time 3 | import urllib3 4 | #url = "http://www.361way.com/python-mysqldb/3842.html" 5 | url = "http://blog.linuxeye.com/sitemap.html" 6 | now = time.time() 7 | pool = urllib3.PoolManager() 8 | r = pool.request('GET', url, assert_same_host=False) 9 | elapsed = time.time() - now 10 | print r.data , elapsed 11 | -------------------------------------------------------------------------------- /grab/mirror_rpmpackage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding: utf-8 3 | # site: www.361way.com 4 | # mail: itybku@139.com 5 | # desc: get the rpm package from yum site 6 | 7 | import pycurl 8 | import StringIO 9 | import urllib 10 | import urllib2 11 | import re 12 | import time 13 | import random 14 | import os 15 | 16 | 17 | def initCurl(): 18 | c = pycurl.Curl() 19 | c.setopt(pycurl.COOKIEFILE, "cookie_file_name")#把cookie保存在该文件中 20 | c.setopt(pycurl.COOKIEJAR, "cookie_file_name") 21 | c.setopt(pycurl.FOLLOWLOCATION, 1) #允许跟踪来源 22 | c.setopt(pycurl.MAXREDIRS, 5) 23 | #设置代理 如果有需要请去掉注释,并设置合适的参数 24 | #c.setopt(pycurl.PROXY, ‘http://11.11.11.11:8080′) 25 | #c.setopt(pycurl.PROXYUSERPWD, ‘aaa:aaa’) 26 | return c 27 | 28 | 29 | 30 | def GetDate(curl, url): 31 | head = ['Accept:*/*', 32 | 'User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0'] 33 | buf = StringIO.StringIO() 34 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 35 | curl.setopt(pycurl.URL, url) 36 | curl.setopt(pycurl.HTTPHEADER, head) 37 | curl.perform() 38 | the_page =buf.getvalue() 39 | buf.close() 40 | return the_page 41 | 42 | 43 | def Schedule(a,b,c): 44 | ''''' 45 | a:已经下载的数据块 46 | b:数据块的大小 47 | c:远程文件的大小 48 | ''' 49 | per = 100.0 * a * b / c 50 | if per > 100 : 51 | per = 100 52 | print '%.2f%%' % per 53 | 54 | c = initCurl() 55 | html = GetDate(c, 'http://repo.saltstack.com/yum/rhel6/') 56 | #print type(html) 57 | #print html 58 | 59 | rpmpack = re.compile(r'href="(.*?)\.rpm',re.I) 60 | rpmpack_url = rpmpack.findall(html) 61 | #print rpmpack_url 62 | 63 | for rpm in rpmpack_url: 64 | rpm_url = 'http://repo.saltstack.com/yum/rhel6/' + rpm + '.rpm' 65 | print rpm_url 66 | #urllib.urlretrieve(rpm_url, rpm + '.rpm',Schedule) 回调Schedule函数,显示进度 67 | try: 68 | urllib.urlretrieve(rpm_url, rpm + '.rpm') 69 | except Exception,e: 70 | continue 71 | -------------------------------------------------------------------------------- /grab/reconn.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import MySQLdb 4 | 5 | 6 | def getTerm(db,tag): 7 | cursor = db.cursor() 8 | query = "SELECT term_id FROM wp_terms where name=%s " 9 | count = cursor.execute(query,tag) 10 | rows = cursor.fetchall() 11 | db.commit() 12 | #db.close() 13 | if count: 14 | term_id = [int(rows[id][0]) for id in range(count)] 15 | print term_id 16 | return term_id 17 | else:return None 18 | 19 | def addTerm(db,tag): 20 | cursor = db.cursor() 21 | query = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)" 22 | data = (tag,tag) 23 | cursor.execute(query,data) 24 | db.commit() 25 | term_id = cursor.lastrowid 26 | sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag',%s) " 27 | value = (term_id,tag) 28 | cursor.execute(sql,value) 29 | db.commit() 30 | db.close() 31 | return int(term_id) 32 | 33 | 34 | dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='useso', port=3306, charset='utf8', init_command='set names utf8') 35 | tags = ['mysql','1111','aaaa','bbbb','ccccc','php','abc','python','java'] 36 | 37 | if __name__ == "__main__": 38 | tagids = [] 39 | for tag in tags: 40 | try: 41 | dbconn.ping() 42 | except: 43 | print 'mysql connect have been close' 44 | dbconn = MySQLdb.connect(host='localhost', user='root', passwd='123456', db='useso', port=3306, charset='utf8', init_command='set names utf8') 45 | termid = getTerm(dbconn,tag) 46 | 47 | if termid: 48 | print tag, 'tag id is ',termid 49 | tagids.extend(termid) 50 | else: 51 | termid = addTerm(dbconn,tag) 52 | print 'add tag',tag,'id is ' ,termid 53 | tagids.append(termid) 54 | print 'All tags id is ',tagids 55 | 56 | 57 | 58 | dbconn = torndb.Connection('localhost:3306','blog',user='root',password='cacti') 59 | tag = "mysql" 60 | def getTerm(db,tag): 61 | query = "SELECT term_id FROM wp_terms where name=%s " 62 | count = db.execute_rowcount(query,tag) 63 | print count 64 | rows = db.query(query,tag) 65 | print rows 66 | for row in rows: 67 | print row.values() 68 | 69 | getTerm(dbconn,tag) 70 | 71 | def getTerm(db,tag): 72 | query = "SELECT term_id FROM wp_terms where name=%s " 73 | rows = db.query(query,tag) 74 | termid = [] 75 | for row in rows: 76 | termid.extend(row.values()) 77 | return termid 78 | 79 | test = getTerm(dbconn,tag) 80 | -------------------------------------------------------------------------------- /grab/segment/pre.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import re 4 | text = ''' 5 |
    #ifndef PHP_ARRAY_H
     6 | #define PHP_ARRAY_H
     7 | PHP_FUNCTION(uasort);
     8 | PHP_FUNCTION(uksort);
     9 | ……
    10 | 
    11 |

    上面定义的排序函数:

    12 |
    /* {{{ proto bool sort(array &array_arg [, int sort_flags])
    13 |    Sort an array */
    14 | PHP_FUNCTION(sort)
    15 | {
    16 |     zval *array;
    17 |     long sort_type = PHP_SORT_REGULAR;
    18 | 
    19 |     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|l", &array, &sort_type) == FAILURE) {
    20 |         RETURN_FALSE;
    21 |     }
    22 | 
    23 |     php_set_compare_func(sort_type TSRMLS_CC);
    24 | 
    25 |     if (zend_hash_sort(Z_ARRVAL_P(array), zend_qsort, php_array_data_compare, 1 TSRMLS_CC) == FAILURE) {
    26 |         RETURN_FALSE;
    27 |     }
    28 |     RETURN_TRUE;
    29 | }
    30 | /* }}} */
    31 | 
    32 | 33 |

    在代码中,看到了

    34 | ''' 35 | 36 | #print re.sub(r'(?ims)
    "(.+?)', r'\1', text)
    37 | r = re.compile(r'
    (.+?)
    ',re.S|re.I|re.M) 38 | x = r.sub(r'
    \1
    ', text) 39 | print x 40 | 41 | 42 | string = '''
    43 | Bla blabla 44 | 45 | bla bla bla''' 46 | 47 | print re.sub(r'', r'
    ', string) 48 | -------------------------------------------------------------------------------- /grab/segment/tag.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import time 4 | import urllib3 5 | import re 6 | import MySQLdb 7 | 8 | def Title_Escape(s): 9 | s = s.replace('"','"') 10 | s = s.replace('&','&') 11 | s = s.replace('<','<') 12 | s = s.replace('>','>') 13 | s = s.replace(' ',' ') 14 | s = s.replace(' - SegmentFault','') 15 | return s 16 | 17 | def Gtag_ID(db,tags): 18 | #tag_list = [] 19 | for tag in tags: 20 | cursor = db.cursor() 21 | id_qsql = "SELECT term_id FROM wp_terms where name=%s " 22 | count = cursor.execute(id_qsql,tag) 23 | if count == 0: 24 | try: 25 | sql = "INSERT into wp_terms (name,slug,term_group) values (%s,%s,0)" 26 | data = (tag,tag) 27 | cursor.execute(sql,data) 28 | db.commit() 29 | tag_id = cursor.lastrowid 30 | print 'Add the new tag %s ,tag_id is ' %(tag),tag_id 31 | tag_list.append(int(tag_id)) 32 | 33 | taxonomy_sql = "INSERT into wp_term_taxonomy (term_id,taxonomy,description) values (%s,'post_tag','') " % (tag_id) 34 | cursor.execute(taxonomy_sql) 35 | db.commit() 36 | db.close() 37 | except: 38 | db.rollback() 39 | db.close() 40 | else: 41 | id = cursor.fetchall() 42 | for tag_id in range(count): 43 | print "%s have been in wp_terms ,tag_id is " %(tag),int(id[tag_id][0]) 44 | tag_list.append(int(id[tag_id][0])) 45 | return list(set(tag_list)) 46 | 47 | 48 | db = MySQLdb.connect("localhost","root","111111","test" ) 49 | url = "http://segmentfault.com/blog/wangbinke/1190000000351425" 50 | now = time.time() 51 | pool = urllib3.PoolManager() 52 | r = pool.request('GET', url, assert_same_host=False) 53 | tags = re.findall(r'data-original-title=[\'"](.*?)[\'"]',r.data) 54 | print tags 55 | #tags = ['php','java','91it'] 56 | tag_list = [100] 57 | Gtag_ID(db,tags) 58 | -------------------------------------------------------------------------------- /grab/segment/title_url.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | #import sys,os 4 | import time 5 | import urllib3 6 | import re 7 | 8 | #url = "http://www.361way.com/python-mysqldb/3842.html" 9 | #url = "http://segmentfault.com/blog/wangbinke/1190000000351425" 10 | url = "http://segmentfault.com/t/php/blogs?page=25" 11 | now = time.time() 12 | pool = urllib3.PoolManager() 13 | r = pool.request('GET', url, assert_same_host=False) 14 | #title = re.findall("
  • ]+)', r.data) 19 | #urls = re.findall(r'href=[\'"]?([^">]+)', r.data) 20 | title_urls = re.findall(r'class="title">',r.data) 21 | #print title_urls 22 | 23 | for i in title_urls: 24 | i_split=i.split('">') 25 | print i_split[0],i_split[1] 26 | 27 | 28 | ''' 29 | for i in title_urls: 30 | url = re.findall(r'href=[\'"]?([^">]+)', i) 31 | # title = re.findall(r'title=[\'"].*?\"',i) 32 | # title = re.findall(r'title=".*?"',i) 33 | title = re.findall(r'title=[\'"]?([^">]+)"',i) 34 | 35 | print str(url[0]),title[0] 36 | 37 | #print title_urls[0] 38 | #print urls[169] 39 | elapsed = time.time() - now 40 | #print r.data , elapsed 41 | print elapsed 42 | ''' 43 | -------------------------------------------------------------------------------- /grab/xisai.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | # encoding: utf-8 3 | # desc: 采集希赛网历年考题,通过抓手机APP访问包里的cookice字段信息,返回json数据,处理后保存为网页 4 | # code from www.361way.com 5 | 6 | import sys 7 | reload(sys) 8 | sys.setdefaultencoding('utf8') 9 | 10 | import pycurl 11 | import StringIO 12 | import zlib 13 | import json 14 | 15 | def initCurl(): 16 | c = pycurl.Curl() 17 | c.setopt(pycurl.COOKIEFILE, "cookie_file_name")#把cookie保存在该文件中 18 | c.setopt(pycurl.COOKIEJAR, "cookie_file_name") 19 | c.setopt(pycurl.FOLLOWLOCATION, 1) #允许跟踪来源 20 | c.setopt(pycurl.MAXREDIRS, 5) 21 | #设置代理 如果有需要请去掉注释,并设置合适的参数 22 | #c.setopt(pycurl.PROXY, ‘http://11.11.11.11:8080′) 23 | #c.setopt(pycurl.PROXYUSERPWD, ‘aaa:aaa’) 24 | return c 25 | 26 | def PostData(curl, url, data): 27 | head = ['Accept:*/*', 28 | 'Accept-Encoding:gzip,deflate', 29 | 'Accept-Language: zh-Hans-CN;q=1', 30 | 'User-Agent: CsaiApp/2.0 (iPhone; iOS 11.3.1; Scale/3.00)'] 31 | buf = StringIO.StringIO() 32 | curl.setopt(pycurl.WRITEFUNCTION, buf.write) 33 | curl.setopt(pycurl.POSTFIELDS, data) 34 | curl.setopt(pycurl.URL, url) 35 | curl.setopt(pycurl.HTTPHEADER, head) 36 | curl.perform() 37 | the_page = buf.getvalue() 38 | #print the_page 39 | buf.close() 40 | return the_page 41 | 42 | def writeinfo(cookie , filename): 43 | 44 | c = initCurl() 45 | html = PostData(c, 'http://app.educity.cn/tiku/tiku_paper_test.ashx',cookie) 46 | 47 | html = zlib.decompress(html, 16+zlib.MAX_WBITS) 48 | #print html 49 | 50 | hjson = json.loads(html) 51 | #print hjson['data']['stlist'][53] 52 | slist = hjson['data']['stlist'] 53 | 54 | for num in range(0,len(slist)): 55 | data = slist[num] 56 | wdata = '' + data['btitle'] + data['sort'] + '
    ' + data['item_text'] + '
    ' + data['ask_text'] + '-'*10 + '
    ' + '答案:' + data['answer'] + '
    ' + data['analyze'] + '

    ' 57 | #filename = "201405_wenda.html" 58 | with open(filename,'a') as f: 59 | f.write(wdata) 60 | 61 | cookie = 'CheckVersions=2.0&GlobalAppType=1&gradeID=9&' 62 | filename = '201511-afternoon.html' 63 | writeinfo(cookie,filename) 64 | -------------------------------------------------------------------------------- /leetcode/01sum.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2018 www.361way.com site All rights reserved. 5 | # 6 | # Filename :01sum.py 7 | # Author :yangbk 8 | # Create Time :2018-08-16 14:33 9 | # Description : 10 | # =============================================================================== 11 | ''' 12 | nums = [2, 7, 11, 15] 13 | target = 9 14 | l = [] 15 | for x in nums: 16 | for y in nums: 17 | if (x + y) == target: 18 | #print x,y 19 | l.append(x) 20 | l.append(y) 21 | print list(set(l)) 22 | ''' 23 | 24 | ''' 25 | nums = [3,2,4] 26 | target = 6 27 | n = range(len(nums)) 28 | l = [] 29 | for x in n: 30 | for y in n: 31 | # [3,3] 6 32 | #if nums[x] != nums[y]: 33 | if x != y: 34 | if nums[x] + nums[y] == target: 35 | print x,y 36 | l.append(x) 37 | l.append(y) 38 | print list(set(l)) 39 | ''' 40 | 41 | ''' 42 | # 超出内存限制 43 | from itertools import combinations 44 | nums = [2, 7, 11, 15] 45 | l = [c for c in combinations(nums, 2)] 46 | 47 | n = [ x for x in l if sum(x)==9] 48 | m = [ x for x in range(len(nums)) if nums[x]==n[0][0] or nums[x]==n[0][1] ] 49 | print m 50 | ''' 51 | d = {} 52 | nums = [3, 2, 4, 15] 53 | #nums = [3, 3, 4, 15] 54 | target = 6 55 | for i in range(len(nums)): 56 | #y = target - nums[x] 57 | x = nums[i] 58 | if target-x in d: 59 | print d 60 | print [d[target-x],i] 61 | d[x] = i 62 | -------------------------------------------------------------------------------- /mail/linux/httpdisk.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | TIME=`date +'%F %T'` 3 | ansible -i /etc/ansible/hosts httpcontent -m raw -a 'df -hP' > /tmp/httpcontent.txt 4 | ansible -i /etc/ansible/hosts httpcontent -m raw -a 'df -iP' > /tmp/httpinode.txt 5 | echo '****************************************************' > /tmp/summary.txt 6 | echo ' http coutent disk use count ' >> /tmp/summary.txt 7 | echo '****************************************************' >> /tmp/summary.txt 8 | grep ':/' /tmp/httpcontent.txt |column -t|sort|uniq >> /tmp/summary.txt 9 | 10 | 11 | echo '****************************************************' >> /tmp/summary.txt 12 | echo ' http coutent inode use count ' >> /tmp/summary.txt 13 | echo '****************************************************' >> /tmp/summary.txt 14 | grep ':/' /tmp/httpinode.txt |column -t|sort|uniq >> /tmp/summary.txt 15 | 16 | grep ':/' /tmp/httpcontent.txt |column -t|sort|uniq|awk 'int($(NF-1)) > 80' > /tmp/alarmdisk 17 | grep ':/' /tmp/httpinode.txt |column -t|sort|uniq|awk 'int($(NF-1)) > 80' > /tmp/alarminode 18 | 19 | 20 | python sendPmail.py 21 | echo "create time $TIME" >> /tmp/httpcontent.txt 22 | echo "create time $TIME" >> /tmp/httpinode.txt 23 | -------------------------------------------------------------------------------- /mail/linux/mail.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #-*- encoding: utf-8 -*- 3 | # 4 | 5 | 6 | import smtplib 7 | from email.mime.text import MIMEText 8 | from email.header import Header 9 | 10 | def send_email(title = '软件监控通知邮件',meial_server = '',from_name = '', from_pwd = '' ,to_name_list=[],message=''): 11 | msg = MIMEText(message,'html',_charset='UTF-8') #设置UTF-8编码 12 | msg['Subject'] =Header(title,"UTF-8") #设置UTF-8编码 13 | msg['From'] = meial_server 14 | msg['To'] = ";".join(to_name_list) 15 | #print msg 16 | try: 17 | s = smtplib.SMTP() 18 | s.connect(meial_server) 19 | s.login(from_name,from_pwd) 20 | s.sendmail(from_name, to_name_list, msg.as_string()) 21 | return True 22 | except Exception, e: 23 | print str(e) 24 | return False 25 | finally: 26 | s.close() 27 | 28 | 29 | if __name__=="__main__": 30 | print send_email(title='软件监控通知邮件',meial_server='smtp.163.com',from_name='itybku@163.com',from_pwd='password',to_name_list=['365474555@qq.com','itybku@139.com'],message='恭喜您,邮件发送成功!') 31 | -------------------------------------------------------------------------------- /mail/linux/mail2.py: -------------------------------------------------------------------------------- 1 | # coding:utf-8 2 | import smtplib 3 | from email.mime.text import MIMEText 4 | from email.header import Header 5 | import time 6 | import os 7 | import sys 8 | 9 | def send_mail(subject, body, mail_to, username, password, mail_type='plain'): 10 | assert isinstance(mail_to, list) == True 11 | msg = MIMEText(body,'html',_charset='UTF-8') 12 | # 定义标题 13 | msg['Subject'] = Header(subject,"UTF-8") 14 | # 定义发信人 15 | msg['From'] = username 16 | # 17 | msg['To'] = ';'.join(mail_to) 18 | # 定义发送时间(不定义的可能有的邮件客户端会不显示发送时间) 19 | msg['date'] = time.strftime('%a, %d %b %Y %H:%M:%S %z') 20 | try: 21 | smtp = smtplib.SMTP() 22 | # 连接SMTP服务器, 23 | smtp.connect('smtp.163.com') 24 | # 用户名密码 25 | smtp.login(username, password) 26 | smtp.sendmail(username, mail_to, msg.as_string()) 27 | smtp.quit() 28 | return True 29 | except Exception as e: 30 | print "send mail error:%s"%e 31 | return False 32 | 33 | if __name__ == "__main__": 34 | 35 | if len(sys.argv) < 2: 36 | print 'Usage : python mail.py object_mail' 37 | sys.exit() 38 | 39 | subject = '你好,这是一封测试邮件' 40 | body = ''' 41 | 这是经过我们的python程序发送的一封邮件,请勿直接回复 42 | ''' 43 | mail_to = [sys.argv[1]] 44 | username = 'itybku@163.com' 45 | #password = os.getenv('PASSWORD') 46 | password = 'password' 47 | send_mail(subject, body, mail_to, username, password) 48 | -------------------------------------------------------------------------------- /mail/linux/pngmail.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | #coding: utf-8 3 | import smtplib 4 | from email.mime.multipart import MIMEMultipart 5 | from email.mime.text import MIMEText 6 | from email.mime.image import MIMEImage 7 | 8 | sender = 'itybku@163.com' 9 | receiver = '365474555@qq.com' 10 | subject = 'python email test' 11 | smtpserver = 'smtp.163.com' 12 | username = 'itybku' 13 | password = 'password' 14 | 15 | # Create message container - the correct MIME type is multipart/alternative. 16 | msg = MIMEMultipart('alternative') 17 | msg['Subject'] = "Link" 18 | 19 | # Create the body of the message (a plain-text and an HTML version). 20 | # 邮件中使用图片的关键是在html页面中引用了图片cid 21 | text = "Hi!\nHow are you?\nHere is the link you wanted:\nhttp://www.python.org" 22 | html = """\ 23 | 24 | 25 | 26 |

    Hi!
    27 | How are you?
    28 | Here is the
    运维之路 you wanted. 29 | 给我捐赠:
    30 |

    31 | 32 | 33 | """ 34 | 35 | def addimg(src,imgid): 36 | fp = open(src,'rb') 37 | msgImage = MIMEImage(fp.read()) 38 | fp.close() 39 | msgImage.add_header('Content-ID',imgid) 40 | return msgImage 41 | 42 | 43 | # Record the MIME types of both parts - text/plain and text/html. 44 | part1 = MIMEText(text, 'plain') 45 | part2 = MIMEText(html, 'html') 46 | 47 | # Attach parts into message container. 48 | # According to RFC 2046, the last part of a multipart message, in this case 49 | # the HTML message, is best and preferred. 50 | msg.attach(part1) 51 | msg.attach(part2) 52 | 53 | #构造附件 54 | #att = MIMEText(open('h:\\python\\1.jpg', 'rb').read(), 'base64', 'utf-8') 55 | msg.attach(addimg("1.png","juanzheng")) 56 | 57 | attach = MIMEText(open('test.xlsx', 'rb').read(), 'base64', 'utf-8') 58 | attach["Content-Type"] = 'application/octet-stream' 59 | # 由于qq邮箱使用gb18030编码,避免乱码这里进行了转码 60 | attach["Content-Disposition"] = 'attachment; filename="测试EXCEL.xlsx"'.decode("utf-8").encode("gb18030") 61 | msg.attach(attach) 62 | 63 | smtp = smtplib.SMTP() 64 | smtp.connect('smtp.163.com') 65 | smtp.login(username, password) 66 | smtp.sendmail(sender, receiver, msg.as_string()) 67 | smtp.quit() 68 | 69 | 70 | -------------------------------------------------------------------------------- /mail/linux/sendPmail.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding:utf8 3 | import sys 4 | reload(sys) 5 | sys.setdefaultencoding("utf-8") 6 | 7 | from email.mime.text import MIMEText 8 | from email.mime.multipart import MIMEMultipart 9 | import smtplib 10 | 11 | msg = MIMEMultipart('alternative') 12 | text = ''' 13 | \n 14 | 内容服务器目前总的disk空间使用情况和inode使用情况见附件:summary.txt 15 | 空间使用超过80%的挂载点见附件:alarmdisk.txt 16 | inode使用空间超过80%挂载点见附件:alarminode.txt 17 | \n 18 | 注:本内容为自动检测并邮件发送,请务回复!有问题可以直接沟通! 19 | ''' 20 | if isinstance(text,unicode): 21 | text = str(text) 22 | 23 | part1 = MIMEText(text, 'plain','utf-8') 24 | msg.attach(part1) 25 | 26 | att1 = MIMEText(open('/tmp/summary.txt', 'rb').read(), 'base64', 'utf8') 27 | att1["Content-Type"] = 'application/octet-stream' 28 | att1["Content-Disposition"] = 'attachment; filename="summary.txt"' 29 | msg.attach(att1) 30 | 31 | att2 = MIMEText(open('/tmp/alarmdisk', 'rb').read(), 'base64', 'utf8') 32 | att2["Content-Type"] = 'application/octet-stream' 33 | att2["Content-Disposition"] = 'attachment; filename="alarmdisk.txt"' 34 | msg.attach(att2) 35 | 36 | att3 = MIMEText(open('/tmp/alarminode', 'rb').read(), 'base64', 'utf8') 37 | att3["Content-Type"] = 'application/octet-stream' 38 | att3["Content-Disposition"] = 'attachment; filename="alarminode.txt"' 39 | msg.attach(att3) 40 | 41 | subject = '内容服务器磁盘和inode巡检结果' 42 | if not isinstance(subject,unicode): 43 | subject = unicode(subject) 44 | 45 | tolist = ['13666680525@139.com','itybku@139.com','18667046156@139.com','conglongyue@huawei.com','18868716501@139.com'] 46 | msg['to'] = ", ".join(tolist) 47 | msg['from'] = 'itybku@163.com' 48 | msg['subject'] = subject 49 | msg["Accept-Language"]="zh-CN" 50 | msg["Accept-Charset"]="ISO-8859-1,utf-8" 51 | 52 | try: 53 | server = smtplib.SMTP() 54 | server.connect('smtp.163.com') 55 | server.login('itybku','mypassword') 56 | server.sendmail(msg['from'], tolist,msg.as_string()) 57 | server.quit() 58 | print '发送成功' 59 | except Exception, e: 60 | print str(e) 61 | -------------------------------------------------------------------------------- /mail/linux/sslmail.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding: utf-8 3 | import smtplib 4 | from email.mime.text import MIMEText 5 | from email.header import Header 6 | sender = '***' 7 | receiver = '***' 8 | subject = 'python email test' 9 | smtpserver = 'smtp.163.com' 10 | username = '***' 11 | password = '***' 12 | 13 | msg = MIMEText('你好','plain','utf-8')#中文需参数‘utf-8',单字节字符不需要 14 | msg['Subject'] = Header(subject, 'utf-8') 15 | 16 | smtp = smtplib.SMTP() 17 | smtp.connect('smtp.163.com') 18 | smtp.ehlo() 19 | smtp.starttls() 20 | smtp.ehlo() 21 | smtp.set_debuglevel(1) 22 | smtp.login(username, password) 23 | smtp.sendmail(sender, receiver, msg.as_string()) 24 | smtp.quit() 25 | 26 | 27 | -------------------------------------------------------------------------------- /mail/windows/mail.py: -------------------------------------------------------------------------------- 1 | # -*- coding: cp936 -*- 2 | import smtplib 3 | from email.mime.text import MIMEText 4 | from email.header import Header 5 | 6 | mailto_list=['365474555@qq.com','itybku@139.com'] 7 | mail_host="smtp.163.com" #设置服务器 8 | mail_user="itybku" #用户名 9 | mail_pass="password" #口令 10 | mail_postfix="163.com" #发件箱的后缀 11 | 12 | def send_mail(to_list,sub,content): 13 | me="itybku@163.com"+"<"+mail_user+"@"+mail_postfix+">" 14 | msg = MIMEText(content,'plain',_charset='gb2312') 15 | msg['Subject'] = Header(sub,"gb2312") 16 | msg['From'] = me 17 | msg['To'] = ";".join(to_list) 18 | try: 19 | server = smtplib.SMTP() 20 | server.connect(mail_host) 21 | server.login(mail_user,mail_pass) 22 | server.sendmail(me, to_list, msg.as_string()) 23 | server.close() 24 | return True 25 | except Exception, e: 26 | print str(e) 27 | return False 28 | if __name__ == '__main__': 29 | if send_mail(mailto_list,"标题!","正常文!"): 30 | print "发送成功" 31 | else: 32 | print "发送失败" 33 | 34 | -------------------------------------------------------------------------------- /md5sum.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #encoding=utf-8 3 | import io 4 | import sys 5 | import hashlib 6 | import string 7 | 8 | def printUsage(): 9 | print ('''Usage: [python] pymd5sum.py ''') 10 | 11 | def main(): 12 | if(sys.argv.__len__()==2): 13 | #print(sys.argv[1]) 14 | 15 | m = hashlib.md5() 16 | file = io.FileIO(sys.argv[1],'r') 17 | bytes = file.read(1024) 18 | while(bytes != b''): 19 | m.update(bytes) 20 | bytes = file.read(1024) 21 | file.close() 22 | 23 | #md5value = "" 24 | md5value = m.hexdigest() 25 | print(md5value+"\t"+sys.argv[1]) 26 | 27 | #dest = io.FileIO(sys.argv[1]+".CHECKSUM.md5",'w') 28 | #dest.write(md5value) 29 | #dest.close() 30 | 31 | else: 32 | printUsage() 33 | main() 34 | -------------------------------------------------------------------------------- /netstat/netinfo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/netstat/netinfo -------------------------------------------------------------------------------- /netstat/pynetstat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | #!/usr/bin/python 4 | 5 | import pwd 6 | import os 7 | import re 8 | import glob 9 | 10 | PROC_TCP = "/proc/net/tcp" 11 | STATE = { 12 | '01':'ESTABLISHED', 13 | '02':'SYN_SENT', 14 | '03':'SYN_RECV', 15 | '04':'FIN_WAIT1', 16 | '05':'FIN_WAIT2', 17 | '06':'TIME_WAIT', 18 | '07':'CLOSE', 19 | '08':'CLOSE_WAIT', 20 | '09':'LAST_ACK', 21 | '0A':'LISTEN', 22 | '0B':'CLOSING' 23 | } 24 | 25 | def _load(): 26 | ''' Read the table of tcp connections & remove header ''' 27 | with open(PROC_TCP,'r') as f: 28 | content = f.readlines() 29 | content.pop(0) 30 | return content 31 | 32 | def _hex2dec(s): 33 | return str(int(s,16)) 34 | 35 | def _ip(s): 36 | ip = [(_hex2dec(s[6:8])),(_hex2dec(s[4:6])),(_hex2dec(s[2:4])),(_hex2dec(s[0:2]))] 37 | return '.'.join(ip) 38 | 39 | def _remove_empty(array): 40 | return [x for x in array if x !=''] 41 | 42 | def _convert_ip_port(array): 43 | host,port = array.split(':') 44 | return _ip(host),_hex2dec(port) 45 | 46 | def netstat(): 47 | ''' 48 | Function to return a list with status of tcp connections at linux systems 49 | To get pid of all network process running on system, you must run this script 50 | as superuser 51 | ''' 52 | 53 | content=_load() 54 | result = [] 55 | for line in content: 56 | line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. 57 | l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal. 58 | r_host,r_port = _convert_ip_port(line_array[2]) 59 | tcp_id = line_array[0] 60 | state = STATE[line_array[3]] 61 | uid = pwd.getpwuid(int(line_array[7]))[0] # Get user from UID. 62 | inode = line_array[9] # Need the inode to get process pid. 63 | pid = _get_pid_of_inode(inode) # Get pid prom inode. 64 | try: # try read the process name. 65 | exe = os.readlink('/proc/'+pid+'/exe') 66 | except: 67 | exe = None 68 | 69 | nline = [tcp_id, uid, l_host+':'+l_port, r_host+':'+r_port, state, pid, exe] 70 | result.append(nline) 71 | return result 72 | 73 | def _get_pid_of_inode(inode): 74 | ''' 75 | To retrieve the process pid, check every running process and look for one using 76 | the given inode. 77 | ''' 78 | for item in glob.glob('/proc/[0-9]*/fd/[0-9]*'): 79 | try: 80 | if re.search(inode,os.readlink(item)): 81 | return item.split('/')[2] 82 | except: 83 | pass 84 | return None 85 | 86 | if __name__ == '__main__': 87 | for conn in netstat(): 88 | print conn 89 | 90 | -------------------------------------------------------------------------------- /netstat/pyss.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # code from www.361way.com 3 | # coding=utf-8 4 | 5 | import pwd 6 | import os 7 | import re 8 | import glob 9 | 10 | PROC_TCP = "/proc/net/tcp" 11 | STATE = { 12 | '01':'ESTABLISHED', 13 | '02':'SYN_SENT', 14 | '03':'SYN_RECV', 15 | '04':'FIN_WAIT1', 16 | '05':'FIN_WAIT2', 17 | '06':'TIME_WAIT', 18 | '07':'CLOSE', 19 | '08':'CLOSE_WAIT', 20 | '09':'LAST_ACK', 21 | '0A':'LISTEN', 22 | '0B':'CLOSING' 23 | } 24 | 25 | def _load(): 26 | ''' Read the table of tcp connections & remove header ''' 27 | with open(PROC_TCP,'r') as f: 28 | content = f.readlines() 29 | content.pop(0) 30 | return content 31 | 32 | def _hex2dec(s): 33 | return str(int(s,16)) 34 | 35 | def _ip(s): 36 | ip = [(_hex2dec(s[6:8])),(_hex2dec(s[4:6])),(_hex2dec(s[2:4])),(_hex2dec(s[0:2]))] 37 | return '.'.join(ip) 38 | 39 | def _remove_empty(array): 40 | return [x for x in array if x !=''] 41 | 42 | def _convert_ip_port(array): 43 | host,port = array.split(':') 44 | return _ip(host),_hex2dec(port) 45 | 46 | def netstat(): 47 | ''' 48 | Function to return a list with status of tcp connections at linux systems 49 | To get pid of all network process running on system, you must run this script 50 | as superuser 51 | ''' 52 | 53 | content=_load() 54 | result = [] 55 | for line in content: 56 | line_array = _remove_empty(line.split(' ')) # Split lines and remove empty spaces. 57 | l_host,l_port = _convert_ip_port(line_array[1]) # Convert ipaddress and port from hex to decimal. 58 | r_host,r_port = _convert_ip_port(line_array[2]) 59 | state = STATE[line_array[3]] 60 | 61 | nline = [ l_host+':'+l_port, r_host+':'+r_port, state ] 62 | result.append(nline) 63 | return result 64 | 65 | if __name__ == '__main__': 66 | for conn in netstat(): 67 | print conn 68 | 69 | -------------------------------------------------------------------------------- /ocr/baiduocr.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # desc: 使用百度AI图片识别模板,进行文字识别,每日限制500次 4 | 5 | import json 6 | from aip import AipOcr 7 | APP_ID = '11241005' 8 | API_KEY = 'key number' 9 | SECRET_KEY = 'secret key' 10 | 11 | client = AipOcr(APP_ID, API_KEY, SECRET_KEY) 12 | def get_file_content(filePath): 13 | with open(filePath, 'rb') as fp: 14 | return fp.read() 15 | 16 | 17 | image = get_file_content('example.jpg') 18 | outdata = client.basicGeneral(image) 19 | for line in outdata['words_result']: 20 | print line['words'] 21 | 22 | #jsdata = json.dumps(outdata,indent=4,encoding='utf-8') 23 | #print jsdata['words_result'] 24 | -------------------------------------------------------------------------------- /oracle_awr_analys/awr_backup.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | import time 3 | import os 4 | import shutil 5 | import awr_configure 6 | import glob 7 | import sys 8 | import traceback 9 | 10 | def awrdir_clearup(): 11 | current_filenames=[] 12 | for dirpath,dirnames,filenames in os.walk(awr_configure.awr_file): 13 | for name in dirnames: 14 | current_filenames.append(os.path.join(dirpath,name)+'/') 15 | 16 | current_filenames = sorted(current_filenames,reverse=True) 17 | for var_dir in current_filenames: 18 | try : 19 | # print glob.glob(var_dir+'*') 20 | if glob.glob(var_dir+'*')==[]: 21 | os.rmdir(var_dir) 22 | except Exception,e: 23 | print e 24 | type, value, tb = sys.exc_info() 25 | awr_awrdir_clearup = traceback.format_exception(type, value, tb)[2] 26 | print awr_awrdir_clearup 27 | os.system('echo "' + awr_awrdir_clearup+'" >>' +awr_configure.awr_log) 28 | 29 | 30 | 31 | 32 | def awrfile_backup(var_awr_filename,var_awr_begin_date) : 33 | 34 | #if backup dir is defined 35 | if os.path.exists(awr_configure.backup_dir)<>True: 36 | awr_backup_dir = os.getcwd() + '/backup' 37 | 38 | else: 39 | awr_backup_dir = awr_configure.backup_dir 40 | 41 | if not awr_backup_dir.endswith('/'): 42 | awr_backup_dir = awr_backup_dir + '/' 43 | print awr_backup_dir 44 | 45 | #create backup dir 46 | # awr_backup_dir= awr_backup_dir+ time.strftime('%Y%m%d',time.localtime(time.time())) 47 | if os.path.exists(awr_backup_dir)<>True: 48 | os.mkdir(awr_backup_dir) 49 | 50 | awr_dir_filename = var_awr_filename[var_awr_filename.rfind('/')+1:] 51 | awr_back_file = awr_backup_dir + awr_dir_filename+'_'+ var_awr_begin_date 52 | 53 | awr_back_log = 'backup ' + var_awr_filename +' to ' + awr_back_file + ' finished!\n' 54 | print awr_back_log 55 | 56 | shutil.move(var_awr_filename,awr_back_file) 57 | 58 | os.system('echo "' + awr_back_log+'" >>' +awr_configure.awr_log) 59 | 60 | 61 | if __name__ == "__main__": 62 | awrdir_clearup() 63 | -------------------------------------------------------------------------------- /oracle_awr_analys/awr_debug.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/python 2 | import awr_configure 3 | 4 | def d_print(input_string,level): 5 | if awr_configure.debug_flag >= level: 6 | print input_string 7 | return 8 | 9 | if __name__ == "__main__": 10 | d_print(1234) 11 | -------------------------------------------------------------------------------- /oracle_awr_analys/doc/readme.txt: -------------------------------------------------------------------------------- 1 | 功能介绍:读取AWR文件获取数据存入ORACLE数据库(Load Profile,Instance 2 | Efficiency,Foreground Wait Events,SQL Statistics) 3 | 测试环境:python 2.6 ,cx_oracle5.1, AWR File(11.1.0.7.0) 4 | 模块介绍: 5 | awr_main.py 6 | 主程序 7 | 8 | awr_regular.py 9 | 通过正则表达式获取数据 10 | 11 | awr_db.py 12 | 判读AWR文件是否分析过; 13 | 保持分析的结果(Load Profile,Instance Efficiency,等)到数据库 14 | 15 | awr_backup.py 16 | 被awr_db.py调用,把分析完的AWR备份 17 | 18 | awr_constant.py 19 | 一些预先定义的正则表达式和SQL语句 20 | 21 | awr_configure.py 22 | 配置信息,包括AWR读取的路径,备份目录(可选项),日志文件等 23 | 24 | create_awr.sql 25 | DB表结构 26 | 27 | 数据库 28 | 29 | eg:日志输出 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /oracle_awr_analys/exec_awr_analysed.sh: -------------------------------------------------------------------------------- 1 | export ORACLE_BASE=/opt/oracle 2 | export ORACLE_HOME=$ORACLE_BASE/product/11gR1/db 3 | export LD_LIBRARY_PATH=$ORACLE_HOME/lib:/lib:/usr/lib:$ORACLE_HOME/rdbms/lib 4 | 5 | /home/oracle/sj/python2.7/bin/python /home/oracle/sj/awr_analysed/bin/awr_main.py 6 | 7 | -------------------------------------------------------------------------------- /oracle_awr_analys/readme.txt: -------------------------------------------------------------------------------- 1 | 功能介绍:读取AWR文件获取数据存入ORACLE数据库(Load Profile,Instance 2 | Efficiency,Foreground Wait Events,SQL Statistics) 3 | 测试环境:python 2.6 ,cx_oracle5.1, AWR File(11.1.0.7.0) 4 | 模块介绍: 5 | awr_main.py 6 | 主程序 7 | 8 | awr_regular.py 9 | 通过正则表达式获取数据 10 | 11 | awr_db.py 12 | 判读AWR文件是否分析过; 13 | 保持分析的结果(Load Profile,Instance Efficiency,等)到数据库 14 | 15 | awr_backup.py 16 | 被awr_db.py调用,把分析完的AWR备份 17 | 18 | awr_constant.py 19 | 一些预先定义的正则表达式和SQL语句 20 | 21 | awr_configure.py 22 | 配置信息,包括AWR读取的路径,备份目录(可选项),日志文件等 23 | 24 | create_awr.sql 25 | DB表结构 26 | 27 | 数据库 28 | 29 | eg:日志输出 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /os/log_timedRotate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # site: www.361way.com 4 | # mail: itybku@139.com 5 | # desc: Rotating logfile by times or size 6 | 7 | 8 | import re 9 | import subprocess 10 | import logging 11 | import socket,time 12 | from logging.handlers import TimedRotatingFileHandler 13 | 14 | LOG_FILE = "/var/log/ping/ping.log" 15 | 16 | #logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',datefmt='%Y-%m-%d %I:%M:%S',filemode='w') #for term print 17 | logger = logging.getLogger() 18 | logger.setLevel(logging.INFO) 19 | fh = TimedRotatingFileHandler(LOG_FILE,when='M',interval=1,backupCount=30) 20 | datefmt = '%Y-%m-%d %H:%M:%S' 21 | format_str = '%(asctime)s %(levelname)s %(message)s ' 22 | #formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') 23 | formatter = logging.Formatter(format_str, datefmt) 24 | fh.setFormatter(formatter) 25 | logger.addHandler(fh) 26 | #logging.info(msg) 27 | #hdlr.flush() 28 | 29 | #---------------------------------------------------------------------- 30 | def pinghost(host): 31 | ping = subprocess.Popen(["ping", "-c", "1",host],stdout = subprocess.PIPE,stderr = subprocess.PIPE) 32 | out, error = ping.communicate() 33 | if "icmp_seq" in out: 34 | icmp_line = re.findall(r'\d+\sbytes(.*?)ms',out) 35 | logging.info('ping ' + host + str(icmp_line)) 36 | else: 37 | logging.info('ping ' + host + ' fail') 38 | 39 | 40 | def tcping(server, port): 41 | ''' Check if a server accepts connections on a specific TCP port ''' 42 | try: 43 | start = time.time() 44 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 45 | s.connect((server, port)) 46 | s.close() 47 | #print server + ':' + str(port) + '/tcp - ' + str(port) + ' port is open' + ' - time=' + str(round((time.time()-start)*10000)/10) + 'ms' 48 | msg = server + ':' + str(port) + '/tcp - ' + str(port) + ' port is open' + ' - time=' + str((time.time()-start)*1000) + 'ms' 49 | logging.info(msg) 50 | except socket.error: 51 | msg = server + ':' + str(port) + ' port not open' 52 | logging.info(msg) 53 | 54 | while 1: 55 | pinghost('passport.migu.cn') 56 | pinghost('112.17.9.72') 57 | tcping('passport.migu.cn',8443) 58 | tcping('112.17.9.72',8443) 59 | #time.sleep(0.5) 60 | 61 | -------------------------------------------------------------------------------- /os/tcping.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | import socket,time 4 | def tcping(server, port): 5 | ''' Check if a server accepts connections on a specific TCP port ''' 6 | try: 7 | start = time.time() 8 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 9 | s.connect((server, port)) 10 | s.close() 11 | #print server + ':' + str(port) + '/tcp - ' + str(port) + ' port is open' + ' - time=' + str(round((time.time()-start)*10000)/10) + 'ms' 12 | print server + ':' + str(port) + '/tcp - ' + str(port) + ' port is open' + ' - time=' + str((time.time()-start)*1000) + 'ms' 13 | except socket.error: 14 | print str(port) + ' port not open' 15 | 16 | while 1: 17 | tcping('192.168.52.252',6556) 18 | time.sleep(1) 19 | -------------------------------------------------------------------------------- /pcmd/agent-python2_201712212138.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/pcmd/agent-python2_201712212138.zip -------------------------------------------------------------------------------- /pcmd/config.conf: -------------------------------------------------------------------------------- 1 | [db] 2 | PORT_NUMBER=8899 3 | path=/home/p2agent/python/key.txt 4 | key=1 5 | IP=192.168.0.116 -------------------------------------------------------------------------------- /peewee/1-createdb.py: -------------------------------------------------------------------------------- 1 | from peewee import * 2 | 3 | 4 | db = SqliteDatabase('student.db') 5 | 6 | class Student(Model): 7 | username = CharField(max_length=255,unique=True) 8 | points = IntegerField(default=0) 9 | 10 | class Meta: 11 | database = db 12 | 13 | 14 | if __name__ == '__main__': 15 | db.connect() 16 | db.create_tables([Student]) 17 | -------------------------------------------------------------------------------- /peewee/2-mode-query.py: -------------------------------------------------------------------------------- 1 | from peewee import * 2 | 3 | 4 | db = SqliteDatabase('school.db') 5 | 6 | class Student(Model): 7 | username = CharField(max_length=255,unique=True) 8 | points = IntegerField(default=0) 9 | 10 | class Meta: 11 | database = db 12 | 13 | students = [ 14 | {'username':'lee','points':100}, 15 | {'username':'jane','points':300}, 16 | {'username':'joe','points':500} 17 | ] 18 | 19 | def add_studnet(): 20 | for student in students: 21 | 22 | try: 23 | Student.create(username=student['username'],points=student['points']) 24 | except IntegrityError: 25 | student_record = Student.get(username=student['username']) 26 | student_record.points = student['points'] 27 | student_record.save() 28 | # 29 | # print 'insert line is exist' 30 | def top_student(): 31 | student = Student.select().order_by(Student.points.desc()).get() 32 | return student 33 | 34 | if __name__ == '__main__': 35 | db.connect() 36 | db.create_tables([Student],safe=True) 37 | add_studnet() 38 | print 'top studnet point is ' + top_student().username 39 | -------------------------------------------------------------------------------- /peewee/3-date-notes.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | import sys,os 3 | from peewee import * 4 | from collections import OrderedDict 5 | from random import choice 6 | from sqlite3.dbapi2 import Timestamp 7 | 8 | 9 | db = SqliteDatabase('notes.db') 10 | 11 | class Note(Model): 12 | content = TextField() 13 | timestamp = DateTimeField(default=datetime.datetime.now) 14 | 15 | 16 | class Meta: 17 | database = db 18 | 19 | def initialize(): 20 | """ create database and table if they exist""" 21 | db.connect() 22 | db.create_tables([Note],safe=True) 23 | 24 | def clear(): 25 | os.system('cls' if os.name == 'nt' else 'clear') 26 | 27 | 28 | def menu_loop(): 29 | """ show menu loop""" 30 | choice = None 31 | while choice !='q': 32 | print '*'*20 33 | print 'Enter q to quit' 34 | for k,v in menu.items(): 35 | print k,v.__doc__ 36 | 37 | print '*'*20 38 | choice = raw_input('Action:').lower().strip() 39 | print '\n' 40 | if choice in menu: 41 | clear() 42 | menu[choice]() 43 | #print '\n' 44 | 45 | def add_note(): 46 | """ add a note""" 47 | print 'Enter your note and press ctrl+d when finished' 48 | data = sys.stdin.read().strip() 49 | if data and raw_input('Save: Y/N ').lower() !='n': 50 | print '\n' 51 | Note.create(content=data) 52 | print 'Saved successfully' 53 | 54 | def view_notes(search_query=None): 55 | """ view notes""" 56 | notes = Note.select().order_by(Note.timestamp.desc()) 57 | if search_query: 58 | notes = notes.where(Note.content.contains(search_query)) 59 | 60 | for note in notes: 61 | timestamp = note.timestamp.strftime('%A %B %d,%Y %I:%M%p') 62 | clear() 63 | print timestamp 64 | print '='*len(timestamp) 65 | print note.content 66 | print '='*len(timestamp) 67 | print '\n' 68 | print '\n\n' + '='*len(timestamp) 69 | print 'n) next note' 70 | print 'd) delete note' 71 | print 'q) return to main menu' 72 | print '*'*20 73 | next_note = raw_input('Action: N/Q: ').lower().strip() 74 | print '\n' 75 | 76 | if next_note == 'q': 77 | break 78 | elif next_note == 'd': 79 | delete_note(note) 80 | def search_note(): 81 | """ search a note""" 82 | view_notes(raw_input('search: ')) 83 | 84 | def delete_note(note): 85 | """ delete a note""" 86 | if raw_input('are you sure ? N/Y: ').lower() == 'y': 87 | print '\n' 88 | Note.delete_instance(note) 89 | print 'note deleted' 90 | 91 | menu = OrderedDict([('a',add_note),('v',view_notes),('s',search_note)]) 92 | 93 | 94 | if __name__ == '__main__': 95 | initialize() 96 | menu_loop() 97 | 98 | 99 | -------------------------------------------------------------------------------- /persist/cpickle/exp1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | try: 5 | import cPickle as pickle 6 | except: 7 | import pickle 8 | import pprint 9 | 10 | data = [ { 'a':'A', 'b':2, 'c':3.0 } ] 11 | print 'DATA:', 12 | pprint.pprint(data) 13 | 14 | data_string = pickle.dumps(data) 15 | print 'PICKLE:', data_string 16 | -------------------------------------------------------------------------------- /persist/cpickle/exp2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # code from www.361way.com 4 | try: 5 | import cPickle as pickle 6 | except: 7 | import pickle 8 | 9 | obj = {'a' : 'b', 'c' : 'd'} 10 | obj2 = [0, 1, 1, 0, 1] 11 | f = open('obj.pkl', 'wb') 12 | pickle.dump(obj, f, protocol=2) 13 | pickle.dump(obj2, f, protocol=2) 14 | f.close() 15 | 16 | f = open('obj.pkl', 'rb') 17 | x1 = pickle.load(f) 18 | x2 = pickle.load(f) 19 | print x1,x2 20 | f.close() 21 | -------------------------------------------------------------------------------- /persist/shelve/m_shelve.py: -------------------------------------------------------------------------------- 1 | #/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import shelve 5 | 6 | 7 | def test_shelve(): 8 | # open 返回一个Shelf类的实例 9 | # 10 | # 参数flag的取值范围: 11 | # 'r':只读打开 12 | # 'w':读写访问 13 | # 'c':读写访问,如果不存在则创建 14 | # 'n':读写访问,总是创建新的、空的数据库文件 15 | # 16 | # protocol:与pickle库一致 17 | # 0: ascii串保存, 默认形式, 方便人读取 18 | # 1: 旧式兼容性较强2进制形式 19 | # 2: 支持新式类的2进制模式,Python2.3开始引入. 20 | # writeback:为True时,当数据发生变化会回写,不过会导致内存开销比较大 21 | d = shelve.open('shelve.db', flag='c', protocol=2, writeback=False) 22 | assert isinstance(d, shelve.Shelf) 23 | 24 | # 在数据库中插入一条记录 25 | d['abc'] = {'name': ['a', 'b']} 26 | d.sync() 27 | 28 | print d['abc'] 29 | 30 | # writeback是False,因此对value进行修改是不起作用的 31 | d['abc']['x'] = 'x' 32 | print d['abc'] # 还是打印 {'name': ['a', 'b']} 33 | 34 | # 当然,直接替换key的value还是起作用的 35 | d['abc'] = 'xxx' 36 | print d['abc'] 37 | 38 | # 还原abc的内容,为下面的测试代码做准备 39 | d['abc'] = {'name': ['a', 'b']} 40 | d.close() 41 | 42 | # writeback 为 True 时,对字段内容的修改会writeback到数据库中。 43 | d = shelve.open('shelve.db', writeback=True) 44 | 45 | # 上面我们已经保存了abc的内容为{'name': ['a', 'b']},打印一下看看对不对 46 | print d['abc'] 47 | 48 | # 修改abc的value的部分内容 49 | d['abc']['xx'] = 'xxx' 50 | print d['abc'] 51 | d.close() 52 | 53 | # 重新打开数据库,看看abc的内容是否正确writeback 54 | d = shelve.open('shelve.db') 55 | print d['abc'] 56 | d.close() 57 | 58 | test_shelve() 59 | -------------------------------------------------------------------------------- /pexecpt/01pipe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | # code from www.361way.com 4 | 5 | import pexpect 6 | child = pexpect.spawn('ls -l /root') 7 | child.expect(pexpect.EOF) 8 | print child.before.decode() 9 | 10 | 11 | ''' 12 | 上面的代码可以正常执行,下面的代码执行的时候,print会打印出EOF捕获的异常, 13 | 因为spawn函数不支持管道、通配符、标志输入、输出、错误重定向 14 | ''' 15 | 16 | child = pexpect.spawn('cat /root/test.file|grep 123') 17 | child.expect(pexpect.EOF) 18 | print child.before.decode() 19 | 20 | 21 | ''' 22 | 如下代码可以正常执行,我们可以通过args参数支持管道,通配符 23 | 等的执行,这些相当于是bash执行的参数 24 | ''' 25 | child = pexpect.spawn('/bin/bash',['-c','cat /root/test.file|grep 123']) 26 | child.expect(pexpect.EOF) 27 | print child.before.decode() 28 | 29 | -------------------------------------------------------------------------------- /pexecpt/02logfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | # code from www.361way.com 4 | 5 | import pexpect 6 | 7 | ''' 8 | pexpect.spawn执行出的日志结果是二进制日志,所以下open 9 | 函数执行写入时,也要是二进制格式,该脚本执行完成后, 10 | cat ttt文件就会发现是ls -l /root执行的结果 11 | ''' 12 | f = open('ttt','wb') 13 | child = pexpect.spawn('ls -l /root',logfile=f) 14 | child.expect(pexpect.EOF) 15 | f.close() 16 | 17 | 18 | ''' 19 | 也可以将执行的日志输出到标准输出,不过需要启用 20 | spawnu方法,u代表unicode 21 | ''' 22 | import sys 23 | child = pexpect.spawnu('ls -l /root',logfile=sys.stdout) 24 | child.expect(pexpect.EOF) 25 | 26 | 27 | ''' 28 | 同时也可以通过cwd参数指定执行该脚本时默认跳到的路径 29 | ''' 30 | child = pexpect.spawnu('ls',logfile=sys.stdout,cwd='/home') 31 | child.expect(pexpect.EOF) 32 | -------------------------------------------------------------------------------- /pexecpt/ftpsync.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | # code from www.361way.com 4 | 5 | import os,sys,re,time 6 | import os.path,pexpect 7 | 8 | def login_ftp(): 9 | ftp = pexpect.spawn('ftp',cwd=cwd) 10 | if ftp.expect(prmpt) !=0: 11 | et cursorlineys.exit() 12 | ftp.sendline(''.join(('open',ftps))) 13 | if ftp.expect('Name') != 0: 14 | sys.exit() 15 | ftp.sendline(ftpuser) 16 | if ftp.expect('230') !=0 or ftp.expect(prmpt) !=0: 17 | sys.exit() 18 | return ftp 19 | 20 | def get_server_files(ftp): 21 | ftp.sendline('ls') 22 | if ftp.expect('226') !=0: 23 | sys.exit() 24 | filelsts = ftp.before 25 | filelsts = filelsts.split('\n') 26 | remtch = re.compile('\s+') 27 | filelsts = [remtch.subn('',item.strip('\r'))[0] for item in filelsts if 28 | 'group' in item] 29 | filedict = dict() 30 | for item in filelsts: 31 | datas = item.split('') 32 | filedict[datas[-1]] = {'mon':mons.index(datas[-4]) + 1,'day':int(datas[-3]),'time':datas[-2]} 33 | return filedict 34 | 35 | def get_local_files(): 36 | localfiles = os.listdir(cwd) 37 | localfilesdict = dict() 38 | for file in localfiles: 39 | t = time.ctime(os.stat(os.path.join(cwd,file)).st_mtime) 40 | datas = t.split('') 41 | localfilesdict[file] = {'mon':mons.index(datas[-4]) + 1,'day':int(datas[-3]), 42 | 'time':datas[-2][:5]} 43 | return localfilesdict 44 | 45 | def sync_files(ftp,localfilesdict,filedict): 46 | addfile = [] 47 | for file in localfilesdict.keys(): 48 | if file not in filedict: 49 | addfile.append(file) 50 | if file in filedict: 51 | if localfilesdict[file]['mon'] > filedict[file]['mon'] or localfilesdict[file]['day'] > filedict[file]['day'] or localfilesdict[file]['time'] > filedict[file]['time']: 52 | addfile.append(file) 53 | 54 | delfile = set(filedict.keys()) - set(localfilesdict.keys()) 55 | if addfile: 56 | for f in addfile: 57 | ftp.sendline('put' + f) 58 | if ftp.expect(['226',pexpect.EOF]) ==0: 59 | print("upload sucess:",f) 60 | else: 61 | sys.exit() 62 | if delfile: 63 | for f in delfile: 64 | ftp.sendline('delete' + f) 65 | if ftp.expect(['250',pexpect.EOF]) == 0: 66 | print('Del:',f) 67 | else: 68 | print('Permission denied!') 69 | sys.exit() 70 | 71 | def exit_ftp(ftp): 72 | if ftp: 73 | ftp.sendcontrol('d') 74 | print(ftp.read().decode()) 75 | 76 | if __name__ == '__main__': 77 | mons = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec') 78 | cwd = '/home/test' 79 | prmpt = ['ftp>',pexpect.EOF] 80 | ftps = '192.168.100.25' 81 | ftpuser = 'anonymous' 82 | ftppw = 'abc' 83 | ftp = login_ftp() 84 | filedict = get_server_files(ftp) 85 | localfilesdict = get_local_files() 86 | sync_files(ftp,localfilesdict,filedict) 87 | exit_ftp(ftp) 88 | 89 | -------------------------------------------------------------------------------- /pexecpt/sysinfo.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #-*- coding: utf-8 -*- 3 | # code from www.361way.com 4 | 5 | from pexpect.pxssh import pxssh 6 | import pexpect 7 | 8 | def login_host(host): 9 | s = pxssh() 10 | if s.login(host[0],host[1],host[2]): 11 | return s 12 | 13 | def get_cpus(sshc): 14 | sshc.sendline('cat /proc/cpuinfo') 15 | res = sshc.expect(['cpu cores.*\r\n',pexpect.EOF]) 16 | if res == 0: 17 | data = sshc.after.decode().split('\r\n') 18 | data = data[0] 19 | data = data[data.index(':') + 1:] 20 | cpucores = int(data) 21 | sshc.prompt() 22 | return cpucores 23 | 24 | 25 | def get_cpu_load(sshc): 26 | sshc.sendline('uptime') 27 | if sshc.prompt(): 28 | data = sshc.after.decode() 29 | data = data.strip('\r\n') 30 | data = data[data.rfind(':') + 1:] 31 | data = data.split(',') 32 | return (float(data[0]),float(data[1]),float(data[2])) 33 | 34 | def get_cpu_stat(sshc): 35 | sshc.sendline('vmstat') 36 | sshc.prompt() 37 | print(sshc.before.decode()) 38 | 39 | def user_deal(host,logfilename): 40 | s = login_host(host) 41 | if not s: 42 | print("login failure:",host[0]) 43 | return 44 | try: 45 | cpucores = get_cpus(s) 46 | if not cpucores: 47 | print("Do not get cpucores:",host[0]) 48 | return 49 | cpu_load = get_cpu_load(s) 50 | if cpu_load[2] >= cpucores: 51 | get_cpu_stat(s) 52 | print("System is not healthy.Do you want to deal?(yes/no)") 53 | yn = input() 54 | if yn == 'yes': 55 | with open(logfilename,'ab+') as f: 56 | s.logfile = f 57 | s.interact() 58 | s.prompt() 59 | s.logfile = None 60 | else: 61 | print("System is healthy:",host[0]) 62 | except: 63 | print('Failure:',host[0]) 64 | finally: 65 | s.logout() 66 | 67 | if __name__ == '__main__': 68 | hosts = [('192.168.1.22','root','123456'),] 69 | logfilename = 'log.txt' 70 | for host in hosts: 71 | user_deal(host,logfilename) 72 | 73 | -------------------------------------------------------------------------------- /queue/fifo.py: -------------------------------------------------------------------------------- 1 | import Queue 2 | q = Queue.Queue() 3 | 4 | for i in range(5): 5 | q.put(i) 6 | 7 | while not q.empty(): 8 | print q.get() 9 | -------------------------------------------------------------------------------- /queue/lifo.py: -------------------------------------------------------------------------------- 1 | import Queue 2 | 3 | q = Queue.LifoQueue() 4 | 5 | for i in range(5): 6 | q.put(i) 7 | 8 | while not q.empty(): 9 | print q.get() 10 | -------------------------------------------------------------------------------- /queue/priority.py: -------------------------------------------------------------------------------- 1 | import Queue 2 | 3 | class Job(object): 4 | def __init__(self, priority, description): 5 | self.priority = priority 6 | self.description = description 7 | print 'New job:', description 8 | return 9 | def __cmp__(self, other): 10 | return cmp(self.priority, other.priority) 11 | 12 | q = Queue.PriorityQueue() 13 | 14 | q.put( Job(3, 'Mid-level job') ) 15 | q.put( Job(10, 'Low-level job') ) 16 | q.put( Job(1, 'Important job') ) 17 | 18 | while not q.empty(): 19 | next_job = q.get() 20 | print 'Processing job:', next_job.description 21 | -------------------------------------------------------------------------------- /queue/queue_thread.py: -------------------------------------------------------------------------------- 1 | from Queue import Queue 2 | from threading import Thread 3 | 4 | def do_stuff(q): 5 | while True: 6 | print q.get() 7 | q.task_done() 8 | 9 | #q = Queue(maxsize=0) 10 | q = Queue() 11 | num_threads = 10 12 | 13 | for i in range(num_threads): 14 | worker = Thread(target=do_stuff, args=(q,)) 15 | worker.setDaemon(True) 16 | worker.start() 17 | 18 | for x in range(100): 19 | q.put(x) 20 | 21 | q.join() 22 | -------------------------------------------------------------------------------- /queue/queue_thread1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | ## DATE: 2011-01-20 3 | ## FILE: queue.py 4 | ## WEBSITE: http://themattreid.com 5 | from Queue import * 6 | from threading import Thread, Lock 7 | 8 | '''this function will process the items in the queue, in serial''' 9 | def processor(): 10 | if queue.empty() == True: 11 | print "the Queue is empty!" 12 | sys.exit(1) 13 | try: 14 | job = queue.get() 15 | print "I'm operating on job item: %s"%(job) 16 | queue.task_done() 17 | except: 18 | print "Failed to operate on job" 19 | 20 | '''set variables''' 21 | queue = Queue() 22 | threads = 4 23 | 24 | '''a list of job items. you would want this to be more advanced, 25 | like reading from a file or database''' 26 | jobs = [ "job1", "job2", "job3" ] 27 | 28 | '''iterate over jobs and put each into the queue in sequence''' 29 | #for job in jobs: 30 | for job in range(100): 31 | print "inserting job into the queue: %s"%(job) 32 | queue.put(job) 33 | 34 | '''start some threads, each one will process one job from the queue''' 35 | for i in range(threads): 36 | th = Thread(target=processor) 37 | th.setDaemon(True) 38 | th.start() 39 | 40 | '''wait until all jobs are processed before quitting''' 41 | queue.join() 42 | -------------------------------------------------------------------------------- /queue/queue_thread2.py: -------------------------------------------------------------------------------- 1 | # System modules 2 | from Queue import Queue 3 | from threading import Thread 4 | import time 5 | 6 | # Local modules 7 | import feedparser 8 | 9 | # Set up some global variables 10 | num_fetch_threads = 2 11 | enclosure_queue = Queue() 12 | 13 | # A real app wouldn't use hard-coded data... 14 | feed_urls = [ 'http://www.castsampler.com/cast/feed/rss/guest', 15 | ] 16 | 17 | 18 | def downloadEnclosures(i, q): 19 | """This is the worker thread function. 20 | It processes items in the queue one after 21 | another. These daemon threads go into an 22 | infinite loop, and only exit when 23 | the main thread ends. 24 | """ 25 | while True: 26 | print '%s: Looking for the next enclosure' % i 27 | url = q.get() 28 | print '%s: Downloading:' % i, url 29 | # instead of really downloading the URL, 30 | # we just pretend and sleep 31 | time.sleep(i + 2) 32 | q.task_done() 33 | 34 | 35 | # Set up some threads to fetch the enclosures 36 | for i in range(num_fetch_threads): 37 | worker = Thread(target=downloadEnclosures, args=(i, enclosure_queue,)) 38 | worker.setDaemon(True) 39 | worker.start() 40 | 41 | # Download the feed(s) and put the enclosure URLs into 42 | # the queue. 43 | for url in feed_urls: 44 | response = feedparser.parse(url, agent='fetch_podcasts.py') 45 | for entry in response['entries']: 46 | for enclosure in entry.get('enclosures', []): 47 | print 'Queuing:', enclosure['url'] 48 | enclosure_queue.put(enclosure['url']) 49 | 50 | # Now wait for the queue to be empty, indicating that we have 51 | # processed all of the downloads. 52 | print '*** Main thread waiting' 53 | enclosure_queue.join() 54 | print '*** Done' 55 | -------------------------------------------------------------------------------- /requests/addcookies.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :addcookies.py 7 | # Author :yangbk 8 | # Description : 9 | # =============================================================================== 10 | # 方式1 11 | import requests 12 | url = 'http://200.200.0.7/Java/jviewer.jnlp?EXTRNIP=200.200.0.7&JNLPSTR=JViewer' 13 | cookies = '''SessionCookie=shfLO755DJlWGV4AkCjSb4jnkBHPXe3J005; Language=ZH; Username=admin;''' 14 | header = { 15 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Geck 16 | o) Chrome/53.0.2785.143 Safari/537.36', 17 | 'Connection': 'keep-alive', 18 | 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 19 | 'Cookie': cookies 20 | } 21 | r = requests.get(url,headers=header) 22 | print(r.text) 23 | 24 | #===================================# 25 | # 方式2 # 26 | #===================================# 27 | import requests 28 | url = 'http://200.200.0.7/Java/jviewer.jnlp?EXTRNIP=200.200.0.7&JNLPSTR=JViewer' 29 | cookie = '''SessionCookie=shfLO755DJlWGV4AkCjSb4jnkBHPXe3J005; Language=ZH; Username=admin;''' 30 | r = requests.get(url,cookies=cookie) 31 | print(r.text) 32 | 33 | -------------------------------------------------------------------------------- /requests/headers.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :headers.py 7 | # Author :yangbk 8 | # Create Time :2019-05-23 11:43 9 | # Description : 10 | # =============================================================================== 11 | import requests 12 | r = requests.head('http://httpbin.org/get') 13 | print(r.headers) 14 | -------------------------------------------------------------------------------- /requests/multipart.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # Filename :multipart.py 6 | # Author :yangbk 7 | # Description : 8 | # =============================================================================== 9 | import requests,json 10 | 11 | url_mul = 'http://httpbin.org/post' 12 | files = {'file':open('/tmp/test.txt','rb')} 13 | r = requests.post(url_mul,files=files) 14 | print(r) 15 | print(r.text) 16 | print(r.content) 17 | -------------------------------------------------------------------------------- /requests/postjson.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # Filename :json.py 6 | # Author :yangbk 7 | # Description : 8 | # =============================================================================== 9 | import requests,json 10 | 11 | url_json = 'http://httpbin.org/post' 12 | 13 | #dumps:将python对象解码为json数据 14 | data_json = json.dumps({'key1':'value1','key2':'value2'}) 15 | r_json = requests.post(url_json,data_json) 16 | print(r_json) 17 | print(r_json.text) 18 | print(r_json.content) 19 | -------------------------------------------------------------------------------- /requests/urlencoded.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # Filename :urlencoded.py 6 | # Author :yangbk 7 | # Description : 8 | # =============================================================================== 9 | 10 | import requests,json 11 | 12 | url = 'http://httpbin.org/post' 13 | data = {'key1':'value1','key2':'value2'} 14 | r =requests.post(url,data) 15 | print(r) 16 | print('==========================') 17 | print(r.text) 18 | print('==========================') 19 | print(r.content) 20 | -------------------------------------------------------------------------------- /selenium/front.py: -------------------------------------------------------------------------------- 1 | import win32gui 2 | 3 | def windowEnumerationHandler(hwnd, top_windows): 4 | top_windows.append((hwnd, win32gui.GetWindowText(hwnd))) 5 | 6 | if __name__ == "__main__": 7 | results = [] 8 | top_windows = [] 9 | win32gui.EnumWindows(windowEnumerationHandler, top_windows) 10 | for i in top_windows: 11 | if "notepad" in i[1].lower(): 12 | print i 13 | #win32gui.ShowWindow(i[0],1) 14 | win32gui.SetForegroundWindow(i[0]) 15 | break 16 | -------------------------------------------------------------------------------- /selenium/open_switch.py: -------------------------------------------------------------------------------- 1 | import win32gui,win32com.client 2 | #import win32con 3 | from selenium import webdriver 4 | import time 5 | 6 | def Broswer(url,x,y): 7 | browser = webdriver.Firefox() 8 | #browser.set_window_size(x, y) 9 | browser.set_window_size(480, 800) 10 | browser.get(url) 11 | browser.set_window_position(x, y) 12 | #browser.get('https://www.163.com') 13 | #browser.close() 14 | #browser.quit() 15 | 16 | 17 | def windowEnumerationHandler(hwnd, top_windows): 18 | if u"firefox" in win32gui.GetWindowText(hwnd).lower(): 19 | top_windows.append((hwnd, win32gui.GetWindowText(hwnd))) 20 | 21 | 22 | 23 | Broswer('http://www.so.com',0,0) 24 | Broswer('http://www.baidu.com',400,0) 25 | Broswer('http://www.163.com',800,0) 26 | 27 | if __name__ == "__main__": 28 | 29 | results = [] 30 | top_windows = [] 31 | win32gui.EnumWindows(windowEnumerationHandler, top_windows) 32 | '''for i in top_windows: 33 | if u"so" in i[1].lower(): 34 | print i 35 | #w3hd=win32gui.FindWindow('MozillaWindowClass',None) 36 | #w3hd=win32gui.FindWindow('MozillaWindowClass',i[1]) 37 | #win32gui.MoveWindow(w3hd, 50, 50, 300, 200, True) 38 | #0:hiddle 1:display 2:min 3:max 39 | #win32gui.ShowWindow(w3hd,0) 40 | #win32gui.ShowWindow(w3hd, win32con.SW_MAXIMIZE) 41 | 42 | 43 | 44 | win32gui.ShowWindow(i[0],1) 45 | win32gui.SetForegroundWindow(i[0]) 46 | 47 | #break''' 48 | 49 | while True: 50 | for i in top_windows: 51 | #if u"firefox" in i[1].lower(): 52 | print i 53 | time.sleep(3) 54 | 55 | win32gui.ShowWindow(i[0],1) 56 | shell = win32com.client.Dispatch("WScript.Shell") 57 | shell.SendKeys('%') 58 | win32gui.SetForegroundWindow(i[0]) 59 | shell.SendKeys("^{F5}", 0) 60 | 61 | #break 62 | -------------------------------------------------------------------------------- /selenium/random/1.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | driver = webdriver.PhantomJS() 3 | driver.set_window_size(1024, 768) 4 | driver.get('https://www.baidu.com/') 5 | driver.save_screenshot('screen.png') 6 | 7 | -------------------------------------------------------------------------------- /selenium/random/fenbianlv.py: -------------------------------------------------------------------------------- 1 | import win32api 2 | dm = win32api.EnumDisplaySettings(None, 0) 3 | dm.PelsHeight = 720 4 | dm.PelsWidth = 1280 5 | dm.BitsPerPel = 32 6 | dm.DisplayFixedOutput = 0 7 | win32api.ChangeDisplaySettings(dm, 0) -------------------------------------------------------------------------------- /selenium/random/flower.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/selenium/random/flower.html -------------------------------------------------------------------------------- /selenium/random/proxy - ����.txt: -------------------------------------------------------------------------------- 1 | 104.154.221.160 80 2 | 104.154.71.65 80 3 | 104.196.13.113 80 4 | 104.198.190.199 80 5 | 104.198.239.22 80 6 | 104.198.245.176 80 7 | 113.121.250.140 808 8 | 123.114.104.83 808 9 | 123.114.97.196 808 10 | 137.59.44.47 8080 11 | 139.224.199.230 80 12 | 167.250.99.13 53281 13 | 218.73.141.229 808 14 | 36.250.156.245 808 -------------------------------------------------------------------------------- /selenium/random/proxy-test.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import random 3 | import os,time 4 | import selenium 5 | from selenium import webdriver 6 | from selenium.webdriver.common.keys import Keys 7 | 8 | 9 | Browser = [ 10 | 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)', 11 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A', 12 | 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 13 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36', 14 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2595.400 QQBrowser/9.6.10872.400' 15 | ] 16 | 17 | 18 | def randomFurl(): 19 | with open('siteurl.txt') as f: 20 | lines = f.read().splitlines() 21 | return random.choice(lines) 22 | 23 | 24 | def install_proxy(PROXY_HOST,PROXY_PORT): 25 | choicebrowser = random.choice(Browser) 26 | fp = webdriver.FirefoxProfile() 27 | print PROXY_PORT 28 | print PROXY_HOST 29 | fp.set_preference("network.proxy.type", 1) 30 | fp.set_preference("network.proxy.https",PROXY_HOST) 31 | fp.set_preference("network.proxy.https_port",int(PROXY_PORT)) 32 | fp.set_preference("network.proxy.ssl",PROXY_HOST) 33 | fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT)) 34 | fp.set_preference("general.useragent.override",choicebrowser) 35 | fp.update_preferences() 36 | return webdriver.Firefox(firefox_profile=fp) 37 | 38 | #def startFirefox(fromurl,totext): 39 | def startFirefox(fromurl): 40 | with open('proxy.txt') as f: 41 | lines = f.read().splitlines() 42 | for ipport in lines: 43 | ip = ipport.split()[0] 44 | port = ipport.split()[1] 45 | driver = install_proxy(ip,port) 46 | #driver = webdriver.Firefox(firefox_binary="D:\\Program Files\\firefox\\firefox.exe") 47 | driver.delete_all_cookies() 48 | #driver.execute_script('localStorage.clear();') 49 | driver.get(fromurl) 50 | time.sleep(random.randint(5, 20)) 51 | link = driver.find_element_by_link_text('数安时代') 52 | link.click() 53 | time.sleep(random.randint(5, 40)) 54 | 55 | driver.switch_to_window(driver.window_handles[-1]) 56 | link = driver.find_element_by_link_text('test2') 57 | link.click() 58 | time.sleep(random.randint(5, 40)) 59 | 60 | driver.close() 61 | driver.quit() 62 | driver = None 63 | 64 | url = 'http://www.361way.com/test.html' 65 | startFirefox(url) 66 | -------------------------------------------------------------------------------- /selenium/random/proxy.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | import os,time 3 | import selenium 4 | from selenium import webdriver 5 | from selenium.webdriver.common.keys import Keys 6 | 7 | 8 | def startFirefox(): 9 | 10 | profile = webdriver.FirefoxProfile() 11 | ''' 12 | profile.set_preference("network.proxy.type", 1) 13 | profile.set_preference("network.proxy.http", "175.155.24.32") 14 | profile.set_preference("network.proxy.http_port", "808") 15 | ''' 16 | profile.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A") 17 | profile.update_preferences() 18 | driver = webdriver.Firefox(firefox_profile=profile) 19 | 20 | """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页""" 21 | #driver = webdriver.Firefox(firefox_binary="D:\\Program Files\\firefox\\firefox.exe") 22 | driver.delete_all_cookies() 23 | #driver.execute_script('localStorage.clear();') 24 | driver.get("http://www.361way.com") 25 | time.sleep(10) 26 | #driver.findElement(By.cssSelector("a[href*='trustauth']")).click() 27 | link = driver.find_element_by_link_text('数安时代') 28 | link.click() 29 | time.sleep(10) 30 | driver.close() 31 | driver.quit() 32 | driver = None 33 | 34 | def my_proxy(PROXY_HOST,PROXY_PORT): 35 | fp = webdriver.FirefoxProfile() 36 | # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 37 | print PROXY_PORT 38 | print PROXY_HOST 39 | fp.set_preference("network.proxy.type", 1) 40 | fp.set_preference("network.proxy.http",PROXY_HOST) 41 | fp.set_preference("network.proxy.http_port",int(PROXY_PORT)) 42 | fp.set_preference("general.useragent.override","whater_useragent") 43 | fp.update_preferences() 44 | return webdriver.Firefox(firefox_profile=fp) 45 | 46 | # Then call in your code: my_proxy(PROXY_HOST,PROXY_PORT) 47 | def install_proxy(PROXY_HOST,PROXY_PORT): 48 | fp = webdriver.FirefoxProfile() 49 | print PROXY_PORT 50 | print PROXY_HOST 51 | fp.set_preference("network.proxy.type", 1) 52 | fp.set_preference("network.proxy.http",PROXY_HOST) 53 | fp.set_preference("network.proxy.http_port",int(PROXY_PORT)) 54 | fp.set_preference("network.proxy.https",PROXY_HOST) 55 | fp.set_preference("network.proxy.https_port",int(PROXY_PORT)) 56 | fp.set_preference("network.proxy.ssl",PROXY_HOST) 57 | fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT)) 58 | fp.set_preference("network.proxy.ftp",PROXY_HOST) 59 | fp.set_preference("network.proxy.ftp_port",int(PROXY_PORT)) 60 | fp.set_preference("network.proxy.socks",PROXY_HOST) 61 | fp.set_preference("network.proxy.socks_port",int(PROXY_PORT)) 62 | fp.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A") 63 | fp.update_preferences() 64 | return webdriver.Firefox(firefox_profile=fp) 65 | 66 | startFirefox() 67 | -------------------------------------------------------------------------------- /selenium/random/test.py: -------------------------------------------------------------------------------- 1 | # -*- coding:utf-8 -*- 2 | 3 | import random 4 | import os,time 5 | import selenium 6 | from selenium import webdriver 7 | from selenium.webdriver.common.keys import Keys 8 | 9 | pages = ['SSL证书代理','联系我们','登录','注册','SSL证书','公司介绍','证书问题','解决方案','产品价格'] 10 | 11 | Browsers = [ 12 | 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)', 13 | 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A', 14 | 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36', 15 | 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' 16 | ] 17 | 18 | def randomurl(): 19 | with open('siteurl.txt') as f: 20 | lines = f.read().splitlines() 21 | return random.choice(lines) 22 | 23 | 24 | def callpage(page): 25 | link = driver.find_element_by_link_text('page') 26 | link.click() 27 | time.sleep(random.randint(5, 40)) 28 | 29 | 30 | def startFirefox(fromurl): 31 | 32 | 33 | choicebrowser = random.choice(Browsers) 34 | print choicebrowser 35 | 36 | profile = webdriver.FirefoxProfile() 37 | profile.set_preference("general.useragent.override",choicebrowser) 38 | profile.update_preferences() 39 | driver = webdriver.Firefox(firefox_profile=profile) 40 | 41 | """启动安装在默认位置的Firefox浏览器,并自动转到 百度 首页""" 42 | #driver = webdriver.Firefox(firefox_binary="D:\\Program Files\\firefox\\firefox.exe") 43 | driver.delete_all_cookies() 44 | #driver.execute_script('localStorage.clear();') 45 | driver.get(fromurl) 46 | time.sleep(random.randint(5, 20)) 47 | #driver.findElement(By.cssSelector("a[href*='trustauth']")).click() 48 | link = driver.find_element_by_link_text('数安时代') 49 | link.click() 50 | time.sleep(random.randint(5, 40)) 51 | driver.switch_to_window(driver.window_handles[-1]) 52 | link = driver.find_element_by_link_text('首页') 53 | link.click() 54 | time.sleep(random.randint(5, 40)) 55 | 56 | driver.close() 57 | driver.quit() 58 | driver = None 59 | 60 | 61 | url = randomurl() 62 | print url 63 | startFirefox(url) 64 | -------------------------------------------------------------------------------- /selenium/selenium_tab.py: -------------------------------------------------------------------------------- 1 | from selenium.webdriver.common.keys import Keys 2 | from selenium import webdriver 3 | import time 4 | 5 | driver = webdriver.Firefox() 6 | driver.set_page_load_timeout(60) 7 | driver.implicitly_wait(15) 8 | 9 | # First Tab 10 | driver.get("https://www.baidu.com") 11 | oldtab = driver.current_window_handle 12 | print driver.title 13 | time.sleep(3) 14 | 15 | # Second Tab 16 | driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + "t") 17 | driver.get("http://mirrors.aliyun.com/") 18 | newtab = driver.current_window_handle 19 | print driver.title 20 | time.sleep(3) 21 | 22 | # Go back to First Tab 23 | driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP) 24 | driver.switch_to_window(oldtab) 25 | print driver.title 26 | driver.refresh() 27 | time.sleep(3) 28 | 29 | # Go to Second Tab again 30 | driver.find_element_by_tag_name("body").send_keys(Keys.CONTROL + Keys.PAGE_UP) 31 | driver.switch_to_window(newtab) 32 | print driver.title 33 | time.sleep(3) 34 | 35 | driver.close() 36 | -------------------------------------------------------------------------------- /selenium/selenium_test.py: -------------------------------------------------------------------------------- 1 | from selenium import webdriver 2 | from selenium.webdriver.common.keys import Keys 3 | 4 | 5 | def Broswer(url,x,y): 6 | browser = webdriver.Firefox() 7 | #browser.set_window_size(x, y) 8 | browser.set_window_size(480, 800) 9 | browser.get(url) 10 | browser.set_window_position(x, y) 11 | #browser.get('https://www.163.com') 12 | #browser.close() 13 | #browser.quit() 14 | 15 | Broswer('http://www.baidu.com',0,0) 16 | Broswer('http://www.so.com',400,0) 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /selenium/sendkeys.py: -------------------------------------------------------------------------------- 1 | import win32com.client 2 | shell = win32com.client.Dispatch("WScript.Shell") 3 | shell.Run("notepad") 4 | shell.AppActivate("notepad") 5 | shell.SendKeys("^o", 0) 6 | shell.SendKeys("^a", 0) 7 | shell.SendKeys("^c", 0) 8 | -------------------------------------------------------------------------------- /selenium/win32_test.py: -------------------------------------------------------------------------------- 1 | import win32gui 2 | import win32con 3 | 4 | def windowEnumerationHandler(hwnd, top_windows): 5 | top_windows.append((hwnd, win32gui.GetWindowText(hwnd))) 6 | 7 | if __name__ == "__main__": 8 | results = [] 9 | top_windows = [] 10 | win32gui.EnumWindows(windowEnumerationHandler, top_windows) 11 | for i in top_windows: 12 | if "notepad" in i[1].lower(): 13 | print i[1] 14 | w3hd=win32gui.FindWindow(None,i[1]) 15 | #win32gui.MoveWindow(w3hd, 50, 50, 300, 200, True) 16 | #0:hiddle 1:display 2:min 3:max 17 | win32gui.ShowWindow(w3hd,1) 18 | #win32gui.ShowWindow(w3hd, win32con.SW_MAXIMIZE) 19 | 20 | ''' 21 | win32gui.ShowWindow(i[0],5) 22 | win32gui.SetForegroundWindow(i[0]) 23 | ''' 24 | break 25 | -------------------------------------------------------------------------------- /stock/all.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | import urllib2,re 5 | 6 | 7 | 8 | #get gg money go to 9 | zjgg = [] 10 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C._AB&sty=DCFFMBFMS&st=(BalFlowMain)&sr=-1&p=1&ps=160&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f') 11 | for line in re.split('","',html.read()): 12 | line = line.strip('"') 13 | zjgg.append(line.split(",")[1]) 14 | 15 | #print zjgg 16 | 17 | 18 | # get 概念 19 | 20 | gngg = [] 21 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C._BKGN&sty=DCFFMBFMS&st=(ChangePercent)&sr=-1&p=1&ps=5&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f') 22 | for line in re.split('","',html.read()): 23 | line = line.strip('"') 24 | #print line 25 | #print [line.split(",")[i] for i in (1, 2)] 26 | #print line.split(",")[1] 27 | value = line.split(",") 28 | #print value[1],value[2] 29 | gnurl = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C.' + value[1] + '1&sty=DCFFMBFMS&st=(BalFlowMain)&sr=-1&p=1&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f' 30 | for line in re.split('","',urllib2.urlopen(gnurl).read()): 31 | line = line.strip('"') 32 | ggvalue = line.split(",") 33 | 34 | #print ggvalue[1],ggvalue[2],value[2] 35 | #gngg.append([ggvalue[1],ggvalue[2],value[2]]) 36 | 37 | if ggvalue[1] in zjgg: 38 | gngg.append(ggvalue[1]) 39 | 40 | #print gngg 41 | 42 | # get 行业 43 | hygg = [] 44 | 45 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C._BKHY&sty=DCFFMBFMS&st=(ChangePercent)&sr=-1&p=1&ps=5&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f') 46 | for line in re.split('","',html.read()): 47 | line = line.strip('"') 48 | #print line 49 | #print [line.split(",")[i] for i in (1, 2)] 50 | #print line.split(",")[1] 51 | value = line.split(",") 52 | #print value[1],value[2] 53 | gnurl = 'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=C.' + value[1] + '1&sty=DCFFMBFMS&st=(BalFlowMain)&sr=-1&p=1&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f' 54 | for line in re.split('","',urllib2.urlopen(gnurl).read()): 55 | line = line.strip('"') 56 | ggvalue = line.split(",") 57 | 58 | if ggvalue[1] in zjgg: 59 | #print ggvalue[1],ggvalue[2],value[2] 60 | if ggvalue[1] in gngg: 61 | print ggvalue[1],ggvalue[2],value[2] 62 | 63 | -------------------------------------------------------------------------------- /stock/calc/prettytable.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/stock/calc/prettytable.pyc -------------------------------------------------------------------------------- /stock/calc/rate.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | from prettytable import * 4 | 5 | def rate(value): 6 | narrow = ["%.2f" % (i*0.01*value) for i in range(99,89,-1)] 7 | narrow.insert(0,'-') 8 | 9 | add = ["%.2f" % (i*0.01*value) for i in range(101,111)] 10 | add.insert(0,'+') 11 | 12 | field = [i for i in range(1,11)] 13 | field.insert(0,'+/-') 14 | 15 | x = PrettyTable() 16 | x.field_names = field 17 | x.add_row(narrow) 18 | x.add_row(add) 19 | result = x.get_string(hrules=ALL) 20 | print(result) 21 | 22 | ''' 23 | print 'hhkg 辉煌科技' 24 | rate(32.27) 25 | print 'sdby 山东玻药' 26 | rate(22.49) 27 | 28 | print 'jbfz 津滨发展' 29 | rate(9.92) 30 | print 'hbyy ' 31 | rate(12.39) 32 | ''' 33 | #print 'tpjt 塔牌集团' 34 | #rate(23.18) 35 | print 'skja ' 36 | rate(4.3) 37 | print 'trt 连云港' 38 | rate(31.5) 39 | -------------------------------------------------------------------------------- /stock/diagstock.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | import sys 4 | reload(sys) 5 | sys.setdefaultencoding('utf8') 6 | 7 | import requests,re,urllib2,xlsxwriter 8 | from multiprocessing.dummy import Pool as ThreadPool 9 | 10 | def Allstockid(): 11 | stockids = [] 12 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 13 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 14 | for line in re.split('","',html.text): 15 | linev = line.split(',')[1] 16 | stockids.append(linev) 17 | return stockids 18 | 19 | 20 | 21 | def stockscore(stockid): 22 | html = requests.get(r'http://vaserviece.10jqka.com.cn/advancediagnosestock/html/' + str(stockid) + '/index.html') 23 | 24 | # regex = '' 25 | # pattern = re.compile(regex,re.I) 26 | # score = re.findall(pattern, html.text) 27 | 28 | # regex2 = '
    \s+

    (.+?)

    \s+

    打败了99%的股票

    \s+

    (.+?)

    \s+
    ' 29 | # pattern2 = re.compile(regex2,re.M) 30 | # data = re.search(pattern2, html.text) 31 | 32 | score = re.findall('',html.text,re.I) 33 | name = re.findall('

    (.+?)\(',html.text,re.I) 34 | mark = re.findall('

    打败了(.+?)%的股票

    ',html.text,re.I) 35 | suggest = re.findall("

    (.+?)

    ",html.text,re.I) 36 | 37 | if len(name) != 0 and len(score) != 0 and len(mark) != 0 and len(suggest) != 0: 38 | return stockid,name[0].decode('utf8'),score[0],mark[0],suggest[0] 39 | # if len(suggest) == 0: 40 | # return stockid,name[0],score[0],mark[0] 41 | # else: 42 | # return stockid,name[0],score[0],mark[0],suggest[0] 43 | # 44 | def toexcel(data): 45 | workbook = xlsxwriter.Workbook('stockscore.xlsx') 46 | worksheet = workbook.add_worksheet('Sheet1') 47 | bold = workbook.add_format({'bold': 1}) 48 | headings = [u'股票代码', u'名称', u'综合诊断分', u'打败了%的股票', u'建议'] 49 | worksheet.write_row('A1', headings, bold) 50 | row = 1 51 | col = 0 52 | for linev in data: 53 | if linev is not None: 54 | worksheet.write_row(row,col,linev) 55 | row += 1 56 | workbook.close() 57 | #stockscore('000961') 58 | 59 | def main(): 60 | codes = Allstockid() 61 | print codes 62 | 63 | pool = ThreadPool(30) 64 | results = pool.map(stockscore, codes) 65 | print results 66 | toexcel(results) 67 | pool.close() 68 | pool.join() 69 | print 'get all data' 70 | 71 | 72 | if __name__ =='__main__': 73 | main() 74 | #print stockscore('000961') 75 | -------------------------------------------------------------------------------- /stock/hismoney.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | # re.split 截断获取想要的日期范围内的数据 5 | # re.sub 将有问题的流释行清除掉 6 | # re.findall 查找出所有的td行的值 7 | 8 | import requests,re,xlsxwriter 9 | from multiprocessing.dummy import Pool as ThreadPool 10 | 11 | def Allstockid(): 12 | stockids = [] 13 | for pagenum in range(1,57): 14 | r = requests.get(r'http://data.10jqka.com.cn/funds/ggzjl/board/3/field/code/order/asc/page/' + str(pagenum) + '/ajax/1/',timeout = 200) 15 | codes = re.findall('target="_blank">(.+)?', r.text, re.I) 16 | #print codes 17 | stockids.extend(codes) 18 | return stockids 19 | 20 | def hismoney(codeid): 21 | 22 | r = requests.get(r'http://stockpage.10jqka.com.cn/' + codeid + '/funds/#funds_lszjsj') 23 | datev = re.findall('class="border_l_none">(.+)?', r.text, re.I) 24 | 25 | text = re.sub('', '', r.text) 26 | 27 | if len(datev) >5: 28 | #print date[4] 29 | data = re.split('tr_border|>' + datev[4] +'20161213(\S+)' ) 33 | q = p.findall(data[1]) 34 | 35 | # if len(q) >11: 36 | # print q[:11] 37 | # print q[11:22] 38 | # print q[22:33] 39 | # x=0 40 | # y=11 41 | # while (len(q)+1) >y: 42 | # dmoney = q[x:y] 43 | # dmoney.append(codeid) 44 | # mondata.append(dmoney) 45 | # x=x+11 46 | # y=y+11 47 | 48 | x=0 49 | y=11 50 | while (len(q)+1) >y and len(q)>43: 51 | if float((q[:11])[3])<1000 or float((q[11:22])[3])<800 or float((q[22:33])[3])<500 : 52 | # if float((q[:11])[3])<4800 or float((q[11:22])[3])<4800 : 53 | break 54 | dmoney = q[x:y] 55 | dmoney.append(codeid) 56 | mondata.append(dmoney) 57 | 58 | x=x+11 59 | y=y+11 60 | 61 | def toexcel(data): 62 | workbook = xlsxwriter.Workbook('stock.xlsx') 63 | worksheet = workbook.add_worksheet('hismoney') 64 | bold = workbook.add_format({'bold': 1}) 65 | headings = [u'日期', u'收盘价', u'涨跌幅', u'资金净流入', u'5日主力净流入', u'大单净额', u'大单净占比',u'中单净额', u'中单净占比',u'小单净额', u'小单净占比',u'股票代码'] 66 | worksheet.write_row('A1', headings, bold) 67 | row = 1 68 | col = 0 69 | for linev in data: 70 | worksheet.write_row(row,col,linev) 71 | row += 1 72 | workbook.close() 73 | 74 | def main(): 75 | global mondata 76 | mondata = [] 77 | 78 | codes = Allstockid() 79 | # print len(codes),codes 80 | 81 | for code in codes: 82 | print code 83 | hismoney(code) 84 | # pool = ThreadPool(30) 85 | # pool.map(hismoney, codes) 86 | print mondata 87 | toexcel(mondata) 88 | 89 | # pool.close() 90 | # pool.join() 91 | print 'doing ok' 92 | 93 | 94 | if __name__ =='__main__': 95 | 96 | main() 97 | 98 | -------------------------------------------------------------------------------- /stock/htmltable_xlsx.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=gbk 3 | 4 | from BeautifulSoup import BeautifulSoup 5 | # import urllib 6 | # import xlwt 7 | import urllib2 8 | import xlsxwriter 9 | 10 | # wb = xlwt.Workbook() 11 | # ws = wb.add_sheet('a test sheet') 12 | 13 | 14 | # workbook = xlsxwriter.Workbook('BlastResults.xlsx') 15 | # worksheet = workbook.add_worksheet('Sheet1') 16 | # 17 | # row = 1 18 | # col = 0 19 | 20 | 21 | #f = urllib2.urlopen("http://data.10jqka.com.cn/funds/ggzjl/field/zjjlr/order/desc/ajax/1/") 22 | f = urllib2.urlopen("http://data.10jqka.com.cn/funds/ggzjl/field/zjjlr/order/desc/ajax/1/") 23 | # http://data.10jqka.com.cn/funds/ggzjl/field/zjjlr/order/desc/page/2/ajax/1/ 24 | html = f.read() 25 | 26 | 27 | 28 | # 29 | soup = BeautifulSoup(html) 30 | # print soup.prettify() 31 | # print soup 32 | 33 | #table = soup.find("table",id="alignmentTable") 34 | table = soup.find("table") 35 | 36 | 37 | 38 | for i in table.findAll('th'): 39 | print i.find_all(text) 40 | 41 | # for tag in table.findAll(['tr','th','td']): 42 | # print tag.findAll('th') 43 | 44 | 45 | # rows = table.findAll("tr") 46 | # print rows 47 | # for row in rows: 48 | # print row 49 | # cols = row.find_all('td') 50 | 51 | # x = 0 52 | # for tr in rows: 53 | # cols = tr.findAll("td") 54 | # if not cols: 55 | # # when we hit an empty row, we should not print anything to the workbook 56 | # continue 57 | # y = 0 58 | # for td in cols: 59 | # texte_bu = td.text 60 | # texte_bu = texte_bu.encode('utf-8') 61 | # texte_bu = texte_bu.strip() 62 | # worksheet.write_row(x, y, td.text) 63 | # print(x, y, td.text) 64 | # y = y + 1 65 | # # update the row pointer AFTER a row has been printed 66 | # # this avoids the blank row at the top of your table 67 | # x = x + 1 68 | 69 | #workbook.close() -------------------------------------------------------------------------------- /stock/last.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # tmv = Total market value 4 | # famc = free-float market capitalisation 5 | # mav =market value 6 | import requests,re,json,xlsxwriter 7 | 8 | def Allstockid(): 9 | stockids = [] 10 | for pagenum in range(1,57): 11 | r = requests.get(r'http://data.10jqka.com.cn/funds/ggzjl/board/3/field/code/order/asc/page/' + str(pagenum) + '/ajax/1/',timeout = 200) 12 | codes = re.findall('target="_blank">(.+)?', r.text, re.I) 13 | #print codes 14 | stockids.extend(codes) 15 | return stockids 16 | 17 | def mav(codeid): 18 | # r = requests.get(r'http://d.10jqka.com.cn/v2/realhead/hs_002346/last.js',timeout = 200) 19 | r = requests.get(r'http://d.10jqka.com.cn/v2/realhead/hs_' + str(codeid) + '/last.js',timeout = 200) 20 | codes = re.findall(('\{.*?\}\}' ),r.text, re.I) 21 | # print codes[0] 22 | # s = json.dumps(codes[0],indent=4,encoding='utf-8') 23 | if codes: 24 | hjson = json.loads(codes[0]) 25 | 26 | name = hjson['items']['name'] 27 | famc = hjson['items']['3475914'] 28 | tmv = hjson['items']['3541450'] 29 | timev = hjson['items']['time'] 30 | 31 | return [codeid,name,famc,tmv,timev] 32 | 33 | #print mav('000404') 34 | # edata = [] 35 | # codes = Allstockid() 36 | # for code in codes: 37 | # print code 38 | # ov = mav(code) 39 | # if ov: 40 | # edata.extend(ov) 41 | # print edata 42 | 43 | codes = Allstockid() 44 | workbook = xlsxwriter.Workbook('stock.xlsx') 45 | worksheet = workbook.add_worksheet('tmv') 46 | bold = workbook.add_format({'bold': 1}) 47 | headings = [u'股票代码', u'名称', u'流通值', u'总市值', u'日期'] 48 | worksheet.write_row('A1', headings, bold) 49 | worksheet.freeze_panes(1, 0) 50 | row = 1 51 | col = 0 52 | for code in codes: 53 | print code 54 | ov = mav(code) 55 | if ov: 56 | worksheet.write_row(row,col,ov) 57 | row += 1 58 | workbook.close() 59 | print 'ok now !!!' 60 | -------------------------------------------------------------------------------- /stock/moneyin-multiprocess.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | #import urllib2,re,os,time 5 | import requests,re,os,time 6 | from multiprocessing.dummy import Pool as ThreadPool 7 | 8 | def clear(): 9 | os.system('cls' if os.name == 'nt' else 'clear') 10 | 11 | def formatid(codeid): 12 | if str(codeid)[0] != '6': 13 | return str(codeid) + '2' 14 | else: 15 | return str(codeid) + '1' 16 | 17 | def getValue(codeid): 18 | codeid = formatid(codeid) 19 | 20 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100',timeout = 200) 21 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100',timeout = 200) 22 | stockv = (html.text).strip('"') 23 | return stockv.split(",")[1:] 24 | 25 | 26 | def Allstockid(): 27 | stockids = [] 28 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 29 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 30 | for line in re.split('","',html.text): 31 | linev = line.split(',')[1] 32 | stockids.append(linev) 33 | return stockids 34 | 35 | 36 | 37 | #codes = ['000060','600050'] 38 | 39 | def main(): 40 | codes = Allstockid() 41 | print codes 42 | 43 | pool = ThreadPool(4) 44 | results = pool.map(getValue, codes) 45 | print results 46 | 47 | # results = [pool.apply_async(getValue, codeid) for codeid in codes] 48 | # print 'Ordered results using pool.apply_async():' 49 | # for r in results: 50 | # print '\t', r.get() 51 | 52 | pool.close() 53 | pool.join() 54 | 55 | 56 | 57 | if __name__ =='__main__': 58 | start = time.time() 59 | main() 60 | costtime = time.time() - start 61 | print 'mult costime:',costtime 62 | 63 | 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /stock/moneyin.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | import urllib2,re,os,time 5 | from prettytable import * 6 | 7 | def clear(): 8 | os.system('cls' if os.name == 'nt' else 'clear') 9 | 10 | def formatid(codeid): 11 | if str(codeid)[0] != '6': 12 | return str(codeid) + '2' 13 | else: 14 | return str(codeid) + '1' 15 | 16 | def getValue(codeid): 17 | codeid = formatid(codeid) 18 | 19 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100') 20 | stockv = (html.read()).strip('"') 21 | return stockv.split(",")[1:] 22 | 23 | 24 | codes = ['000060','600050'] 25 | 26 | while True: 27 | row = PrettyTable() 28 | row.field_names = ["codeid","name","当前价","百分比","净流入","涨跌值","date"] 29 | for codeid in codes: 30 | 31 | row.add_row(getValue(codeid)) 32 | 33 | result = row.get_string(hrules=ALL) 34 | print result 35 | time.sleep(2) 36 | clear() 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /stock/moneyin/moneyin.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | import urllib2,re,os,time 5 | from prettytable import * 6 | 7 | def clear(): 8 | os.system('cls' if os.name == 'nt' else 'clear') 9 | 10 | def formatid(codeid): 11 | if str(codeid)[0] != '6': 12 | return str(codeid) + '2' 13 | else: 14 | return str(codeid) + '1' 15 | 16 | def getValue(codeid): 17 | codeid = formatid(codeid) 18 | 19 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100') 20 | stockv = (html.read()).strip('"') 21 | return stockv.split(",")[1:] 22 | 23 | 24 | codes = ['000060','600050'] 25 | 26 | while True: 27 | row = PrettyTable() 28 | row.field_names = ["codeid","name","当前价","百分比","净流入","涨跌值","date"] 29 | for codeid in codes: 30 | 31 | row.add_row(getValue(codeid)) 32 | 33 | result = row.get_string(hrules=ALL) 34 | print result 35 | time.sleep(2) 36 | clear() 37 | 38 | 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /stock/moneyin/prettytable.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/stock/moneyin/prettytable.pyc -------------------------------------------------------------------------------- /stock/mult-in-excel.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | #import urllib2,re,os,time 5 | import requests,re,os,time,xlsxwriter 6 | from multiprocessing.dummy import Pool as ThreadPool 7 | 8 | def clear(): 9 | os.system('cls' if os.name == 'nt' else 'clear') 10 | 11 | def formatid(codeid): 12 | if str(codeid)[0] != '6': 13 | return str(codeid) + '2' 14 | else: 15 | return str(codeid) + '1' 16 | 17 | def getValue(codeid): 18 | codeid = formatid(codeid) 19 | 20 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100',timeout = 200) 21 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=' + str(codeid) + '&sty=DCFFMBFMS&st=&sr=&p=&ps=&cb=&js=(x)&token=0b9469e9fdfd123fcec4532ae1c20f4f&_=1481174762100',timeout = 200) 22 | stockv = (html.text).strip('"') 23 | return stockv.split(",")[1:] 24 | 25 | 26 | def Allstockid(): 27 | stockids = [] 28 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 29 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 30 | for line in re.split('","',html.text): 31 | linev = line.split(',')[1] 32 | stockids.append(linev) 33 | return stockids 34 | 35 | def toexcel(data): 36 | workbook = xlsxwriter.Workbook('stock.xlsx') 37 | worksheet = workbook.add_worksheet('monin_dfcf') 38 | bold = workbook.add_format({'bold': 1}) 39 | headings = [u'股票代码', u'名称', u'当前价', u'涨跌幅', u'净流入', u'涨跌值', u'日期'] 40 | worksheet.write_row('A1', headings, bold) 41 | row = 1 42 | col = 0 43 | for linev in data: 44 | worksheet.write_row(row,col,linev) 45 | row += 1 46 | workbook.close() 47 | 48 | 49 | 50 | #codes = ['000060','600050'] 51 | 52 | def main(): 53 | codes = Allstockid() 54 | print codes 55 | 56 | pool = ThreadPool(30) 57 | results = pool.map(getValue, codes) 58 | toexcel(results) 59 | 60 | 61 | pool.close() 62 | pool.join() 63 | 64 | 65 | 66 | if __name__ =='__main__': 67 | start = time.time() 68 | main() 69 | costtime = time.time() - start 70 | print 'mult costime:',costtime 71 | 72 | 73 | 74 | 75 | 76 | -------------------------------------------------------------------------------- /stock/pe/get_pe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | # 1、pe在 0~20 之间的企业 4 | # 2、流通股本小于50亿的企业 5 | 6 | 7 | import urllib2 8 | import time 9 | import json 10 | 11 | 12 | 13 | def get_pe(stockid): 14 | try: 15 | url = 'http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js' % stockid 16 | send_headers = { 17 | 'Host':'d.10jqka.com.cn', 18 | 'Referer':'http://stock.10jqka.com.cn/', 19 | 'Accept':'application/json, text/javascript, */*; q=0.01', 20 | 'Connection':'keep-alive', 21 | 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36', 22 | 'X-Forwarded-For':'124.160.148.178', 23 | 'X-Requested-With':'XMLHttpRequest' 24 | } 25 | req = urllib2.Request(url,headers=send_headers) 26 | f = urllib2.urlopen(req) 27 | 28 | data = f.read().split('items":',1)[1] 29 | data = data.split('})',1)[0] 30 | 31 | 32 | J_data = json.loads(data) 33 | #J_data = json.dumps(data,indent=4,encoding='utf-8') 34 | stockpe = J_data['2034120'] 35 | stockname = J_data['name'] 36 | sumvalue = J_data['3475914'] 37 | currentprice = J_data['10'] 38 | #print stockid,stockname,stockpe 39 | return stockname,stockpe,sumvalue,currentprice 40 | except urllib2.HTTPError, e: 41 | #return stockid ,'get happed httperror' 42 | return e.code 43 | 44 | if __name__ == '__main__': 45 | print 'stockid stockname currentprice stockpe Billvalue' 46 | stockids = [line.strip() for line in open("stock_exp.txt", 'r')] 47 | for stockid in stockids: 48 | try: 49 | stockname,stockpe,sumvalue,currentprice = get_pe(stockid) 50 | if sumvalue: 51 | Billvalue = round(float(sumvalue)/1000/1000/100) 52 | else: 53 | Billvalue = 0 54 | 55 | if stockpe: 56 | if float(stockpe) > 0 and float(stockpe) < 22 and Billvalue < 200 : 57 | print stockid,stockname,currentprice,stockpe,Billvalue 58 | #else: 59 | # print stockid 60 | except TypeError ,e: 61 | print stockid ,'get is error' 62 | -------------------------------------------------------------------------------- /stock/pe/pe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import urllib2 4 | import time 5 | import json 6 | 7 | codeid = '600030' 8 | 9 | f = urllib2.urlopen('http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js'%codeid) 10 | 11 | data = f.read().split('items":',1)[1] 12 | data = data.split('})',1)[0] 13 | 14 | 15 | J_data = json.loads(data) 16 | #J_data = json.dumps(data,indent=4,encoding='utf-8') 17 | stockpe = J_data['2034120'] 18 | stockname = J_data['name'] 19 | 20 | 21 | print codeid,stockname,stockpe 22 | 23 | -------------------------------------------------------------------------------- /stock/pe/thread_pe.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | # 1、pe在 0~20 之间的企业 4 | # 2、流通股本小于50亿的企业 5 | 6 | import threading 7 | import urllib2 8 | import time 9 | import json 10 | 11 | lock = threading.Lock() 12 | 13 | 14 | def get_pe(stockid): 15 | try: 16 | url = 'http://d.10jqka.com.cn/v2/realhead/hs_%s/last.js' % stockid 17 | send_headers = { 18 | 'Host':'d.10jqka.com.cn', 19 | 'Referer':'http://stock.10jqka.com.cn/', 20 | 'Accept':'application/json, text/javascript, */*; q=0.01', 21 | 'Connection':'keep-alive', 22 | 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36', 23 | 'X-Forwarded-For':'124.160.148.178', 24 | 'X-Requested-With':'XMLHttpRequest' 25 | } 26 | req = urllib2.Request(url,headers=send_headers) 27 | f = urllib2.urlopen(req) 28 | 29 | data = f.read().split('items":',1)[1] 30 | data = data.split('})',1)[0] 31 | 32 | 33 | J_data = json.loads(data) 34 | #J_data = json.dumps(data,indent=4,encoding='utf-8') 35 | stockpe = J_data['2034120'] 36 | stockname = J_data['name'] 37 | sumvalue = J_data['3475914'] 38 | currentprice = J_data['10'] 39 | #print stockid,stockname,stockpe 40 | return stockname,stockpe,sumvalue,currentprice 41 | except urllib2.HTTPError, e: 42 | #return stockid ,'get happed httperror' 43 | return e.code 44 | 45 | 46 | def cond(stockid,pe,asset): 47 | pe = int(pe) 48 | asset = int(asset) 49 | try: 50 | stockname,stockpe,sumvalue,currentprice = get_pe(stockid) 51 | if sumvalue: 52 | Billvalue = round(float(sumvalue)/1000/1000/100) 53 | else: 54 | Billvalue = 0 55 | 56 | if stockpe: 57 | if float(stockpe) > 0 and float(stockpe) < pe and Billvalue < asset : 58 | lock.acquire() 59 | print stockid,stockname,currentprice,stockpe,Billvalue 60 | lock.release() 61 | #else: 62 | # print stockid 63 | except TypeError ,e: 64 | print stockid ,'get is error' 65 | 66 | 67 | if __name__ == '__main__': 68 | threads = [] 69 | print 'stockid stockname currentprice stockpe Billvalue' 70 | stockids = [line.strip() for line in open("stock_exp.txt", 'r')] 71 | for stockid in stockids: 72 | t = threading.Thread(target=cond, args=(stockid,28,80)) 73 | threads.append(t) 74 | t.start() 75 | -------------------------------------------------------------------------------- /stock/readme/163.txt: -------------------------------------------------------------------------------- 1 | #总股本 2 | http://quotes.money.163.com/old/#query=EQA&DataType=HS_RANK&sort=PERCENT&order=desc&count=24&page=0 3 | 4 | 5 | -------------------------------------------------------------------------------- /stock/readme/dfcf.txt: -------------------------------------------------------------------------------- 1 | 东方财富 2 | 3 | #之前已实现的功能 4 | 1、stoct百分比计算器 5 | 2、实时股价和资金流入情况 6 | 3、总股本 7 | 4、根据热门行业和净流入排行情况选股 8 | 9 | 5、多进程和多线程行情数据抓取(正在实现) 10 | 11 | #20161208 12 | 13 | #列所有股票(查询codeid 可以取第二个字段 + 第一个字估) 14 | http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571 15 | 16 | 17 | #实时资金排行: 18 | #计算净额比 = 净流入/流通股 (取最大值的前几名,或者返过来取最小值) 19 | http://data.eastmoney.com/zjlx/detail.html 20 | 21 | 22 | #主力资金排行(前两百个 + 净流入超过5000万的) 23 | #同时可以考虑将净流入值也做采集写入excel ---便于在excel中做晒选 24 | #大于7%都除外 25 | http://data.eastmoney.com/zjlx/list.html 26 | 27 | 28 | 29 | #公司信息相关(市值等) 30 | http://softf9.eastmoney.com/v2/?fc=00096102&platform=ios&color=b 31 | 32 | #股本结构 33 | http://softf9.eastmoney.com/v2/F10_gbjg.aspx?fc=00096102&color=b&isShow= 34 | 35 | 36 | 37 | #资金情况 38 | http://ff.eastmoney.com/EM_CapitalFlowInterface/api/js?id=0009612&type=hff&rtntype=2&js=(x)&check=TMLBMS&cb=var%20ph_data=&QueryStyle=1&QuerySpan=2016-11-3&_=1481809483587 39 | http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx?type=CT&cmd=0009612&sty=CTBF&st=z&sr=&p=&ps=&cb=var%20pie_data=&js=(x)&token=28758b27a75f62dc3065b81f7facb365&_=1481809483586 40 | 41 | 42 | #财务信息 43 | http://data.eastmoney.com/bbsj/201706/yjbb.html 44 | -------------------------------------------------------------------------------- /stock/readme/qq.txt: -------------------------------------------------------------------------------- 1 | #财务报告 2 | http://finance.qq.com/data/#yjpm 3 | -------------------------------------------------------------------------------- /stock/readme/readme.txt: -------------------------------------------------------------------------------- 1 | 排序计算(EXCEL自定义排序) 2 | 1、净流入(净流入/总股本) 3 | 2、大单净量2、大单净量 4 | -------------------------------------------------------------------------------- /stock/readme/sina.txt: -------------------------------------------------------------------------------- 1 | 资金净流入 2 | http://vip.stock.finance.sina.com.cn/moneyflow/#jlrlpm 3 | http://vip.stock.finance.sina.com.cn/quotes_service/api/json_v2.php/MoneyFlow.ssl_bkzj_ssggzj?page=1&num=20&sort=netamount&asc=0&bankuai=&shichang= 4 | 5 | 6 | 持续流入情况; 7 | http://vip.stock.finance.sina.com.cn/moneyflow/#lxjlrg 8 | 9 | bigdata 10 | http://finance.sina.com.cn/data/#visual-agrt 11 | 12 | http://vip.stock.finance.sina.com.cn/q/go.php/vIR_Burstout/index.phtml 13 | 14 | -------------------------------------------------------------------------------- /stock/readme/ths.txt: -------------------------------------------------------------------------------- 1 | #同花顺 2 | http://qd.10jqka.com.cn/quote.php?cate=real&type=stock&return=json&callback=showStockData&code=600048,601766,000961,300315,601989,600568,601985,601899,601988,300033 3 | 4 | #若外盘数量大于内盘,则表现买方力量较强 5 | #6昨收 6 | #7开盘价 7 | #8最高价 8 | #9最低价 9 | #10当前价 10 | #13成交量 11 | #14外盘 12 | #15内盘 13 | #19成交额 14 | #69涨停 15 | #70跌停 16 | #526792振幅 17 | #3475914流通值 18 | #264648净涨值(元) 19 | #199112涨幅 20 | #1968584换手 21 | #2034120市盈率 22 | #1378761均价 23 | #1771976量比 24 | #461256委比 25 | #3475914净流入 26 | #395720委差 27 | 28 | 29 | http://stockpage.10jqka.com.cn/spService/000961/Funds/realFunds 30 | #json flash title 返回资金情况和总金额情况 31 | 32 | http://stockpage.10jqka.com.cn/spService/000961/Funds/lineFunds 33 | #json格式 净额占百分 34 | 35 | http://stockpage.10jqka.com.cn/spService/000961/Header/realHeader 36 | #实时头数据,比之前的容易理解 37 | 38 | 39 | 40 | http://d.10jqka.com.cn/v2/fiverange/hs_600048/last.js 41 | http://d.10jqka.com.cn/v2/moneyflow/hs_600048/last.js 42 | http://d.10jqka.com.cn/v2/time/hs_600048/last.js 43 | http://d.10jqka.com.cn/v2/realhead/hs_600048/last.js 44 | 45 | http://d.10jqka.com.cn/v2/realhead/hs_000961/last.js 46 | 47 | 48 | #公司简介 49 | http://basic.10jqka.com.cn/mobile/000961/companyn.html 50 | 51 | #高级诊股 52 | http://vaserviece.10jqka.com.cn/advancediagnosestock/html/000961/index.html 53 | 54 | 55 | 56 | 57 | #财务 58 | 59 | 60 | #历史数据 61 | http://stockpage.10jqka.com.cn/000961/funds/#funds_lszjsj 62 | 63 | #stock name 64 | http://data.10jqka.com.cn/funds/ggzjl/board/3/field/code/order/asc/page/2/ajax/1/ 65 | 66 | 财务业绩 67 | http://data.10jqka.com.cn/financial/yjyg/### 68 | http://data.10jqka.com.cn/financial/yjgg/#refCountId=data_55ebdbc3_648 69 | -------------------------------------------------------------------------------- /stock/readme/zqzx.txt: -------------------------------------------------------------------------------- 1 | 证券之星 2 | 3 | 4 | #沪深A股涨跌股,成交量等信息 5 | http://quote.stockstar.com/stock/ranklist_a.shtml 6 | 7 | #资金流向 8 | http://quote.stockstar.com/cashflow/1_3_1_1.html 9 | 10 | #净流入 11 | http://quote.stockstar.com/cashflow/continue/in_4_1_1.html 12 | -------------------------------------------------------------------------------- /stock/she_codes.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # shelve all stock codes 4 | import requests,re,shelve 5 | 6 | def dfcfall(): 7 | stockids = [] 8 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 9 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 10 | for line in re.split('","',html.text): 11 | linev = line.split(',')[1] 12 | stockids.append(linev) 13 | return stockids 14 | 15 | 16 | def thsall(): 17 | stockids = [] 18 | for pagenum in range(1,57): 19 | r = requests.get(r'http://data.10jqka.com.cn/funds/ggzjl/board/3/field/code/order/asc/page/' + str(pagenum) + '/ajax/1/',timeout = 200) 20 | codes = re.findall('target="_blank">(.+)?', r.text, re.I) 21 | #print codes 22 | stockids.extend(codes) 23 | return stockids 24 | 25 | def shewrite(key,value): 26 | d = shelve.open('shelve.db', flag='c', protocol=2, writeback=False) 27 | assert isinstance(d, shelve.Shelf) 28 | d[key] = value 29 | d.sync() 30 | d.close() 31 | 32 | def sheread(key): 33 | d = shelve.open('shelve.db') 34 | assert isinstance(d, shelve.Shelf) 35 | return d[key] 36 | d.close() 37 | 38 | if __name__ =='__main__': 39 | 40 | shewrite('dfcfall',dfcfall()) 41 | shewrite('thsall',thsall()) 42 | 43 | print sheread('dfcfall') 44 | print 'thsall' , sheread('thsall') 45 | -------------------------------------------------------------------------------- /stock/stock-wave.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # itybku@139.com 4 | # desc: 计算波动率 5 | import sys,requests,re 6 | reload(sys) 7 | sys.setdefaultencoding('utf8') 8 | 9 | import tushare as ts 10 | from multiprocessing.dummy import Pool as ThreadPool 11 | 12 | def per(code): 13 | #print code,type(code) 14 | try: 15 | x = ts.get_hist_data(code,start='2017-01-01',end='2017-12-05') 16 | # for i in x.p_change: 17 | # if abs(i)>3: 18 | # print i 19 | 20 | y = len([i for i in x.p_change if abs(i)>3]) 21 | if len(x)>1 and y >1: 22 | print "%s %.2f%%" %(code ,(float(y)/float(len(x))*100)) 23 | except: 24 | print "code error" 25 | 26 | def Allstockid(): 27 | stockids = [] 28 | #html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 29 | html = requests.get(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=4000&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49373571',timeout = 200) 30 | for line in re.split('","',html.text): 31 | linev = line.split(',')[1] 32 | linev = str(linev) 33 | stockids.append(linev) 34 | return stockids 35 | 36 | ''' 37 | def main(): 38 | codes = Allstockid() 39 | #print codes 40 | 41 | pool = ThreadPool(30) 42 | pool.map(per, codes) 43 | pool.close() 44 | pool.join() 45 | 46 | if __name__ =='__main__': 47 | main() 48 | ''' 49 | for code in Allstockid(): 50 | per(code) 51 | 52 | -------------------------------------------------------------------------------- /stock/talib/01-kdj.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :kdj.py 7 | # Author :yangbk 8 | # Create Time :2019-07-17 19:25 9 | # Description : 10 | # =============================================================================== 11 | import talib as ta 12 | import tushare as ts 13 | import pandas as pd 14 | 15 | 16 | 17 | dw = ts.get_k_data("603236") 18 | print(len(dw)) 19 | dw = dw[300:] 20 | dw.index = range(len(dw)) 21 | dw['slowk'], dw['slowd'] = ta.STOCH(dw['high'].values, 22 | dw['low'].values, 23 | dw['close'].values, 24 | fastk_period=9, 25 | slowk_period=3, 26 | slowk_matype=0, 27 | slowd_period=3, 28 | slowd_matype=0) 29 | 30 | df = pd.DataFrame(data=dw) 31 | row = df.iloc[-10:].values 32 | print(row) 33 | 34 | 35 | 36 | """slowkMA5 = ta.MA(slowk, timeperiod=5, matype=0) 37 | slowkMA10 = ta.MA(slowk, timeperiod=10, matype=0) 38 | slowkMA20 = ta.MA(slowk, timeperiod=20, matype=0) 39 | slowdMA5 = ta.MA(slowd, timeperiod=5, matype=0) 40 | slowdMA10 = ta.MA(slowd, timeperiod=10, matype=0) 41 | slow 42 | """ 43 | -------------------------------------------------------------------------------- /stock/talib/02-kdjall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :02-kdjall.py 7 | # Author :yangbk 8 | # Create Time :2019-07-17 19:26 9 | # Description : 10 | # =============================================================================== 11 | import talib as ta 12 | import tushare as ts 13 | import pandas as pd 14 | 15 | def Get_Stock_List(): 16 | df = ts.get_stock_basics() 17 | return df 18 | 19 | def Get_KDJ(code): 20 | dw = ts.get_k_data(code) 21 | # 排除新股 22 | if len(dw) > 60: 23 | dw = dw[-56:] 24 | dw.index = range(len(dw)) 25 | dw['slowk'], dw['slowd'] = ta.STOCH(dw['high'].values, 26 | dw['low'].values, 27 | dw['close'].values, 28 | fastk_period=9, 29 | slowk_period=3, 30 | slowk_matype=0, 31 | slowd_period=3, 32 | slowd_matype=0) 33 | 34 | df = pd.DataFrame(data=dw) 35 | #row = df.iloc[-10:].values 36 | v1 = df.iloc[-1].values 37 | v2 = df.iloc[-2].values 38 | k1,d1 = v1[-2:] 39 | k2,d2 = v2[-2:] 40 | if (k2 < 22) and (d2 < 22) and (k1 > d1) and (k1 > k2) and (d1 > d2) and (k1 < 35): 41 | return(code) 42 | 43 | codes = [] 44 | df = Get_Stock_List() 45 | for code in df.index: 46 | print('check ' + code + ' kdj now:') 47 | kcode = Get_KDJ(code) 48 | if kcode != None: 49 | codes.append(kcode) 50 | print(codes) 51 | -------------------------------------------------------------------------------- /stock/talib/03-macd.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :03-macd.py 7 | # Author :yangbk 8 | # Create Time :2019-07-17 19:47 9 | # Description : 10 | # =============================================================================== 11 | # macd = 12 天 EMA - 26 天 EMA 12 | # signal = 9 天 MACD的EMA 13 | # hist = MACD - MACD signal 14 | 15 | import talib as ta 16 | import tushare as ts 17 | import pandas as pd 18 | 19 | 20 | 21 | dw = ts.get_k_data("600600") 22 | dw = dw[-50:] 23 | print(dw) 24 | close = dw.close.values 25 | print(close) 26 | print(len(close)) 27 | dw['macd'], dw['macdsignal'], dw['macdhist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) 28 | 29 | df = pd.DataFrame(data=dw) 30 | row = df.iloc[-10:].values 31 | print(row) 32 | -------------------------------------------------------------------------------- /stock/talib/04-macdall.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf8 3 | # =============================================================================== 4 | # Copyright (C) 2019 www.361way.com site All rights reserved. 5 | # 6 | # Filename :04-macdall.py 7 | # Author :yangbk 8 | # Create Time :2019-07-18 17:28 9 | # Description : 10 | # =============================================================================== 11 | import talib as ta 12 | import tushare as ts 13 | import pandas as pd 14 | 15 | def Get_Stock_List(): 16 | df = ts.get_stock_basics() 17 | return df 18 | 19 | def Get_MACD(code): 20 | dw = ts.get_k_data(code) 21 | if len(dw) > 60: 22 | dw = dw[-56:] 23 | close = dw.close.values 24 | dw['macd'], dw['macdsignal'], dw['macdhist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) 25 | 26 | df = pd.DataFrame(data=dw) 27 | row3,row2,row1 = df.iloc[-3:].values 28 | md1,mds1,mdh1 = row1[-3:] 29 | md2,mds2,mdh2 = row2[-3:] 30 | md3,mds3,mdh3 = row3[-3:] 31 | if (md3< md2 8 | # Create Time :2019-07-18 17:30 9 | # Description : 10 | # =============================================================================== 11 | import talib as ta 12 | import tushare as ts 13 | import pandas as pd 14 | 15 | def Get_Stock_List(): 16 | df = ts.get_stock_basics() 17 | return df 18 | 19 | def K_Data(code): 20 | dw = ts.get_k_data(code) 21 | # 排除新股 22 | if len(dw) > 51: 23 | dw = dw[-50:] 24 | return dw 25 | 26 | def Get_KDJ(dw): 27 | dw['slowk'], dw['slowd'] = ta.STOCH(dw['high'].values, 28 | dw['low'].values, 29 | dw['close'].values, 30 | fastk_period=9, 31 | slowk_period=3, 32 | slowk_matype=0, 33 | slowd_period=3, 34 | slowd_matype=0) 35 | 36 | df = pd.DataFrame(data=dw) 37 | #row = df.iloc[-10:].values 38 | v1 = df.iloc[-1].values 39 | v2 = df.iloc[-2].values 40 | k1,d1 = v1[-2:] 41 | k2,d2 = v2[-2:] 42 | if (k2 < 22) and (d2 < 22) and (k1 > d1) and (k1 > k2) and (d1 > d2) and (k1 < 35): 43 | return(code) 44 | 45 | def Get_MACD(dw): 46 | close = dw.close.values 47 | dw['macd'], dw['macdsignal'], dw['macdhist'] = ta.MACD(close, fastperiod=12, slowperiod=26, signalperiod=9) 48 | 49 | df = pd.DataFrame(data=dw) 50 | row3,row2,row1 = df.iloc[-3:].values 51 | md1,mds1,mdh1 = row1[-3:] 52 | md2,mds2,mdh2 = row2[-3:] 53 | md3,mds3,mdh3 = row3[-3:] 54 | if (md3< md2 60: 64 | dw = dw[-56:] 65 | kcode = Get_KDJ(dw) 66 | mcode = Get_MACD(dw) 67 | if (kcode != None) and (mcode != None): 68 | codes.append(code) 69 | print(codes) 70 | print(len(codes)) 71 | 72 | df = ts.get_realtime_quotes(codes) 73 | #print(df) 74 | #print(df[['code','name','price','bid','ask','volume','amount','time']]) 75 | print(df[['code','name','price','low','high','time']]) 76 | -------------------------------------------------------------------------------- /stock/toexcel.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # 4 | import sys 5 | reload(sys) 6 | sys.setdefaultencoding('utf-8') 7 | 8 | import urllib2,re 9 | import xlrd 10 | import xlsxwriter 11 | 12 | workbook = xlsxwriter.Workbook('date_report.xlsx') 13 | worksheet = workbook.add_worksheet('Sheet1') 14 | bold = workbook.add_format({'bold': 1}) 15 | headings = [u'时间', u'问题描述', u'责任人'] 16 | worksheet.write_row('A1', headings, bold) 17 | worksheet.freeze_panes(1, 0) 18 | 19 | row = 1 20 | col = 0 21 | 22 | html = urllib2.urlopen(r'http://nufm.dfcfw.com/EM_Finance2014NumericApplication/JS.aspx/JS.aspx?type=ct&st=(Code)&sr=1&p=1&ps=3500&js=(x)&token=894050c76af8597a853f5b408b759f5d&cmd=C._A&sty=DCFFITA&rt=49311621') 23 | for line in re.split('","',html.read()): 24 | line = line.strip('"') 25 | linev = line.split(',')[:11] 26 | 27 | worksheet.write_row(row,col,linev) 28 | row += 1 29 | workbook.close() 30 | 31 | 32 | -------------------------------------------------------------------------------- /stock/tosql.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | # Create Time:2017-03-10 10:16:55 4 | # Author:itybku < www.361way.com > 5 | # 遍历一段时期的数据并写入数据库 6 | 7 | from sqlalchemy import create_engine 8 | import tushare as ts 9 | import requests,re 10 | from multiprocessing.dummy import Pool as ThreadPool 11 | 12 | 13 | def tosql(id): 14 | #df = ts.get_k_data('600000',ktype='D',index=True,start='2016-03-01', end='2017-03-08') 15 | df = ts.get_k_data(id,ktype='D',start='2016-03-01', end='2017-03-08') 16 | engine = create_engine('mysql://root:password@127.0.0.1/stock?charset=utf8') 17 | 18 | # tick_data 为插入时的表名 19 | df.to_sql('tick_data',engine,if_exists='append') 20 | 21 | def Allstockid(): 22 | stockids = [] 23 | for pagenum in range(1,57): 24 | r = requests.get(r'http://data.10jqka.com.cn/funds/ggzjl/board/3/field/code/order/asc/page/' + str(pagenum) + '/ajax/1/',timeout = 200) 25 | codes = re.findall('target="_blank">(.+)?', r.text, re.I) 26 | #print codes 27 | stockids.extend(codes) 28 | return stockids 29 | 30 | 31 | def writedata(): 32 | codes = Allstockid() 33 | pool = ThreadPool(30) 34 | pool.map(tosql, codes) 35 | 36 | pool.close() 37 | pool.join() 38 | print 'write to mysql is ok!' 39 | 40 | writedata() 41 | -------------------------------------------------------------------------------- /thread/Thread-Pool.py: -------------------------------------------------------------------------------- 1 | 2 | from Queue import Queue 3 | from threading import Thread 4 | 5 | class Worker(Thread): 6 | """Thread executing tasks from a given tasks queue""" 7 | def __init__(self, tasks): 8 | Thread.__init__(self) 9 | self.tasks = tasks 10 | self.daemon = True 11 | self.start() 12 | 13 | def run(self): 14 | while True: 15 | func, args, kargs = self.tasks.get() 16 | try: func(*args, **kargs) 17 | except Exception, e: print e 18 | self.tasks.task_done() 19 | 20 | class ThreadPool: 21 | """Pool of threads consuming tasks from a queue""" 22 | def __init__(self, num_threads): 23 | self.tasks = Queue(num_threads) 24 | for _ in range(num_threads): Worker(self.tasks) 25 | 26 | def add_task(self, func, *args, **kargs): 27 | """Add a task to the queue""" 28 | self.tasks.put((func, args, kargs)) 29 | 30 | def wait_completion(self): 31 | """Wait for completion of all the tasks in the queue""" 32 | self.tasks.join() 33 | 34 | if __name__ == '__main__': 35 | from random import randrange 36 | delays = [randrange(1, 10) for i in range(100)] 37 | 38 | from time import sleep 39 | def wait_delay(d): 40 | print 'sleeping for (%d)sec' % d 41 | sleep(d) 42 | 43 | # 1) Init a Thread pool with the desired number of threads 44 | pool = ThreadPool(20) 45 | 46 | for i, d in enumerate(delays): 47 | # print the percentage of tasks placed in the queue 48 | print '%.2f%c' % ((float(i)/float(len(delays)))*100.0,'%') 49 | 50 | # 2) Add the task to the queue 51 | pool.add_task(wait_delay, d) 52 | 53 | # 3) Wait for completion 54 | pool.wait_completion() 55 | 56 | -------------------------------------------------------------------------------- /thread/condition/con1.py: -------------------------------------------------------------------------------- 1 | import threading 2 | import time 3 | 4 | class Producer(threading.Thread): 5 | def run(self): 6 | global count 7 | while True: 8 | if con.acquire(): 9 | if count > 1000: 10 | con.wait() 11 | else: 12 | count = count+100 13 | msg = self.name+' produce 100, count=' + str(count) 14 | print msg 15 | con.notify() 16 | con.release() 17 | time.sleep(1) 18 | 19 | class Consumer(threading.Thread): 20 | def run(self): 21 | global count 22 | while True: 23 | if con.acquire(): 24 | if count < 100: 25 | con.wait() 26 | else: 27 | count = count-3 28 | msg = self.name+' consume 3, count='+str(count) 29 | print msg 30 | con.notify() 31 | con.release() 32 | time.sleep(1) 33 | 34 | count = 500 35 | con = threading.Condition() 36 | 37 | def test(): 38 | for i in range(2): 39 | p = Producer() 40 | p.start() 41 | for i in range(5): 42 | c = Consumer() 43 | c.start() 44 | if __name__ == '__main__': 45 | test() 46 | -------------------------------------------------------------------------------- /thread/condition/con2.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import threading 3 | import time 4 | cond = threading.Condition() 5 | class kongbaige(threading.Thread): 6 | def __init__(self, cond, diaosiname): 7 | threading.Thread.__init__(self, name = diaosiname) 8 | self.cond = cond 9 | 10 | def run(self): 11 | self.cond.acquire() #获取锁 12 | 13 | print self.getName() + ':一支穿云箭' #空白哥说的第一句话 14 | self.cond.notify() #唤醒其他wait状态的线程(通知西米哥 让他说话) 15 | #然后进入wait线程挂起状态等待notify通知(等西米哥的回复,接下来俩人就开始扯蛋) 16 | self.cond.wait() 17 | 18 | print self.getName() + ':山无棱,天地合,乃敢与君绝!' 19 | self.cond.notify() 20 | self.cond.wait() 21 | 22 | print self.getName() + ':紫薇!!!!(此处图片省略)' 23 | self.cond.notify() 24 | self.cond.wait() 25 | 26 | print self.getName() + ':是你' 27 | self.cond.notify() 28 | self.cond.wait() 29 | 30 | #这里是空白哥说的最后一段话,接下来就没有对白了 31 | print self.getName() + ':有钱吗 借点' 32 | self.cond.notify() #通知西米哥 33 | self.cond.release() #释放锁 34 | 35 | 36 | 37 | class ximige(threading.Thread): 38 | def __init__(self, cond, diaosiname): 39 | threading.Thread.__init__(self, name = diaosiname) 40 | self.cond = cond 41 | 42 | def run(self): 43 | self.cond.acquire() 44 | self.cond.wait() #线程挂起(等西米哥的notify通知) 45 | 46 | print self.getName() +':千军万马来相见' 47 | self.cond.notify() #说完话了notify空白哥wait的线程 48 | self.cond.wait() #线程挂起等待空白哥的notify通知 49 | 50 | print self.getName() + ':海可枯,石可烂,激情永不散!' 51 | self.cond.notify() 52 | self.cond.wait() 53 | 54 | print self.getName() + ':尔康!!!(此处图片省略)' 55 | self.cond.notify() 56 | self.cond.wait() 57 | 58 | print self.getName() + ':是我' 59 | self.cond.notify() 60 | self.cond.wait() 61 | 62 | #这里是最后一段话,后面空白哥没接话了 所以说完就释放锁 结束线程 63 | print self.getName() + ':滚' 64 | self.cond.release() 65 | 66 | 67 | kongbai = kongbaige(cond, ' ') 68 | ximi = ximige(cond, '西米') 69 | #尼玛下面这2个启动标志是关键,虽然是空白哥先开的口,但是不能让他先启动, 70 | #因为他先启动的可能直到发完notify通知了,西米哥才开始启动, 71 | #西米哥启动后会一直处于44行的wait状态,因为空白哥已经发完notify通知了进入wait状态了, 72 | #而西米哥没收到 73 | #造成的结果就是2根线程就一直在那挂起,什么都不干,也不扯蛋了 74 | ximi.start() 75 | kongbai.start() 76 | -------------------------------------------------------------------------------- /thread/condition/con3.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | #---- Condition 3 | #---- 捉迷藏的游戏 4 | import threading, time 5 | class Hider(threading.Thread): 6 | def __init__(self, cond, name): 7 | super(Hider, self).__init__() 8 | self.cond = cond 9 | self.name = name 10 | 11 | def run(self): 12 | time.sleep(1) #确保先运行Seeker中的方法 13 | 14 | self.cond.acquire() #b 15 | print self.name + ': 我已经把眼睛蒙上了' 16 | self.cond.notify() 17 | self.cond.wait() #c 18 | #f 19 | print self.name + ': 我找到你了 ~_~' 20 | self.cond.notify() 21 | self.cond.release() 22 | #g 23 | print self.name + ': 我赢了' #h 24 | 25 | class Seeker(threading.Thread): 26 | def __init__(self, cond, name): 27 | super(Seeker, self).__init__() 28 | self.cond = cond 29 | self.name = name 30 | def run(self): 31 | self.cond.acquire() 32 | self.cond.wait() #a #释放对琐的占用,同时线程挂起在这里,直到被notify并重新占有琐。 33 | #d 34 | print self.name + ': 我已经藏好了,你快来找我吧' 35 | self.cond.notify() 36 | self.cond.wait() #e 37 | #h 38 | self.cond.release() 39 | print self.name + ': 被你找到了,哎~~~' 40 | 41 | cond = threading.Condition() 42 | seeker = Seeker(cond, 'seeker') 43 | hider = Hider(cond, 'hider') 44 | seeker.start() 45 | hider.start() 46 | -------------------------------------------------------------------------------- /thread/lock/dead-lock1.py: -------------------------------------------------------------------------------- 1 | import threading 2 | import time 3 | 4 | class MyThread(threading.Thread): 5 | def run(self): 6 | global num 7 | time.sleep(1) 8 | 9 | if mutex.acquire(1): 10 | num = num+1 11 | msg = self.name+' set num to '+str(num) 12 | print msg 13 | mutex.acquire() 14 | mutex.release() 15 | mutex.release() 16 | num = 0 17 | mutex = threading.Lock() 18 | def test(): 19 | for i in range(5): 20 | t = MyThread() 21 | t.start() 22 | if __name__ == '__main__': 23 | test() 24 | -------------------------------------------------------------------------------- /thread/lock/dead-lock2.py: -------------------------------------------------------------------------------- 1 | import threading 2 | import time 3 | 4 | class MyThread(threading.Thread): 5 | def do1(self): 6 | global resA, resB 7 | if mutexA.acquire(): 8 | msg = self.name+' got resA' 9 | print msg 10 | 11 | if mutexB.acquire(1): 12 | msg = self.name+' got resB' 13 | print msg 14 | mutexB.release() 15 | mutexA.release() 16 | def do2(self): 17 | global resA, resB 18 | if mutexB.acquire(): 19 | msg = self.name+' got resB' 20 | print msg 21 | 22 | if mutexA.acquire(1): 23 | msg = self.name+' got resA' 24 | print msg 25 | mutexA.release() 26 | mutexB.release() 27 | 28 | 29 | def run(self): 30 | self.do1() 31 | self.do2() 32 | resA = 0 33 | resB = 0 34 | 35 | mutexA = threading.Lock() 36 | mutexB = threading.Lock() 37 | 38 | def test(): 39 | for i in range(5): 40 | t = MyThread() 41 | t.start() 42 | if __name__ == '__main__': 43 | test() 44 | -------------------------------------------------------------------------------- /thread/lock/lock1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import threading # 导入threading模块 5 | import time # 导入time模块 6 | class mythread(threading.Thread): # 通过继承创建类 7 | def __init__(self,threadname): # 初始化方法 8 | # 调用父类的初始化方法 9 | threading.Thread.__init__(self,name = threadname) 10 | def run(self): # 重载run方法 11 | global x # 使用global表明x为全局变量 12 | for i in range(3): 13 | x = x + 1 14 | time.sleep(5) # 调用sleep函数,让线程休眠5秒 15 | print x 16 | 17 | tl = [] # 定义列表 18 | for i in range(10): 19 | t = mythread(str(i)) # 类实例化 20 | tl.append(t) # 将类对象添加到列表中 21 | 22 | x=0 # 将x赋值为0 23 | for i in tl: 24 | i.start() 25 | -------------------------------------------------------------------------------- /thread/lock/lock2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | 4 | import threading # 导入threading模块 5 | import time # 导入time模块 6 | class mythread(threading.Thread): # 通过继承创建类 7 | def __init__(self,threadname): # 初始化方法 8 | threading.Thread.__init__(self,name = threadname) 9 | def run(self): # 重载run方法 10 | global x # 使用global表明x为全局变量 11 | lock.acquire() # 调用lock的acquire方法 12 | for i in range(3): 13 | x = x + 1 14 | time.sleep(2) # 调用sleep函数,让线程休眠5秒 15 | print x 16 | lock.release() # 调用lock的release方法 17 | lock = threading.Lock() # 类实例化 18 | tl = [] # 定义列表 19 | for i in range(10): 20 | t = mythread(str(i)) # 类实例化 21 | tl.append(t) # 将类对象添加到列表中 22 | 23 | x=0 # 将x赋值为0 24 | for i in tl: 25 | i.start() # 依次运行线程 26 | -------------------------------------------------------------------------------- /tomcat_deamon/cfg/config.ini: -------------------------------------------------------------------------------- 1 | [MonitorProgram] 2 | StartPath: C:/tomcat/bin/startup.bat 3 | CATALINA_HOME: C:\\tomcat\\ 4 | JAVA_HOME: C:\\Program Files\\Java\\jdk1.8.0_31 5 | 6 | [MonitorProcessName] 7 | ProcessName: tomcat_8080 8 | 9 | [MonitorUrl] 10 | Url: http://127.0.0.1:8080 -------------------------------------------------------------------------------- /tomcat_deamon/logs/output.log: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/361way/python/9bf372f03138112222e64ac2f981f7070cece523/tomcat_deamon/logs/output.log -------------------------------------------------------------------------------- /tomcat_deamon/monitor.py: -------------------------------------------------------------------------------- 1 | #!-*- encoding: utf-8 -*- 2 | import urllib2 3 | import logging 4 | import os 5 | import time 6 | from ConfigParser import ConfigParser 7 | from logging.handlers import TimedRotatingFileHandler 8 | 9 | LOG_FILE = "./logs/output.log" 10 | 11 | logger = logging.getLogger() 12 | logger.setLevel(logging.INFO) 13 | fh = TimedRotatingFileHandler(LOG_FILE,when='midnight',interval=1,backupCount=30) 14 | datefmt = '%Y-%m-%d %H:%M:%S' 15 | format_str = '%(asctime)s %(levelname)s %(message)s ' 16 | formatter = logging.Formatter(format_str, datefmt) 17 | fh.setFormatter(formatter) 18 | fh.suffix = "%Y%m%d%H%M" 19 | logger.addHandler(fh) 20 | 21 | def getUrlcode(url): 22 | try: 23 | start = time.time() 24 | response = urllib2.urlopen(url,timeout=10) 25 | msg = 'httpcode is ' + str(response.getcode()) + ' - open url use time ' + str((time.time()-start)*1000) + 'ms' 26 | logging.info(msg) 27 | return response.getcode() 28 | except urllib2.URLError as e: 29 | msg = 'open url error ,reason is:' + str(e.reason) 30 | logging.info(msg) 31 | 32 | def get(field, key): 33 | result = "" 34 | try: 35 | result = cf.get(field, key) 36 | except: 37 | result = "" 38 | return result 39 | 40 | def read_config(config_file_path, field, key): 41 | cf = ConfigParser() 42 | try: 43 | cf.read(config_file_path) 44 | result = cf.get(field, key) 45 | except: 46 | sys.exit(1) 47 | return result 48 | 49 | CONFIGFILE='./cfg/config.ini' 50 | 51 | os.environ["JAVA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','JAVA_HOME') 52 | os.environ["CATALINA_HOME"] = read_config(CONFIGFILE,'MonitorProgram','CATALINA_HOME') 53 | 54 | ProgramPath = read_config(CONFIGFILE,'MonitorProgram','StartPath') 55 | ProcessName = read_config(CONFIGFILE,'MonitorProcessName','ProcessName') 56 | url = read_config(CONFIGFILE,'MonitorUrl','Url') 57 | #url = "http://dh.361way.com/" 58 | 59 | 60 | while True: 61 | HttpCode = getUrlcode(url) 62 | 63 | if HttpCode is not 200: 64 | command = 'taskkill /F /FI "WINDOWSTITLE eq ' + ProcessName + '"' 65 | os.system(command) 66 | os.system(ProgramPath) 67 | 68 | time.sleep(30) 69 | 70 | ''' 71 | import os 72 | import socket 73 | def IsOpen(ip,port): 74 | s = socket.socket(socket.AF_INET,socket.SOCK_STREAM) 75 | try: 76 | s.connect((ip,int(port))) 77 | s.shutdown(2) 78 | print '%d is open' % port 79 | return True 80 | except: 81 | print '%d is down' % port 82 | return False 83 | if __name__ == '__main__': 84 | IsOpen('127.0.0.1',800) ''' 85 | -------------------------------------------------------------------------------- /tree.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # tree.py 4 | # 5 | # Prints the tree structure for the path specified on the command line 6 | 7 | from os import listdir, sep 8 | from os.path import abspath, basename, isdir 9 | from sys import argv 10 | 11 | def tree(dir, padding, print_files=False): 12 | print padding[:-1] + '+-' + basename(abspath(dir)) + '/' 13 | padding = padding + ' ' 14 | files = [] 15 | if print_files: 16 | files = listdir(dir) 17 | else: 18 | files = [x for x in listdir(dir) if isdir(dir + sep + x)] 19 | count = 0 20 | for file in files: 21 | count += 1 22 | print padding + '|' 23 | path = dir + sep + file 24 | if isdir(path): 25 | if count == len(files): 26 | tree(path, padding + ' ', print_files) 27 | else: 28 | tree(path, padding + '|', print_files) 29 | else: 30 | print padding + '+-' + file 31 | 32 | def usage(): 33 | return '''Usage: %s [-f] 34 | Print tree structure of path specified. 35 | Options: 36 | -f Print files as well as directories 37 | PATH Path to process''' % basename(argv[0]) 38 | 39 | def main(): 40 | if len(argv) == 1: 41 | print usage() 42 | elif len(argv) == 2: 43 | # print just directories 44 | path = argv[1] 45 | if isdir(path): 46 | tree(path, ' ') 47 | else: 48 | print 'ERROR: \'' + path + '\' is not a directory' 49 | elif len(argv) == 3 and argv[1] == '-f': 50 | # print directories and files 51 | path = argv[2] 52 | if isdir(path): 53 | tree(path, ' ', True) 54 | else: 55 | print 'ERROR: \'' + path + '\' is not a directory' 56 | else: 57 | print usage() 58 | 59 | if __name__ == '__main__': 60 | main() 61 | --------------------------------------------------------------------------------