脚本要实现的功能:输入instance id 1:将所有的volume take snapshot 2: 获取public ip 并登陆机器执行 ps 命令记录patch前进程状态已经端口状态 3:获取机器所在的elb 4: 从elb中移除当前机器 5:检查snapshots是否完成 6:snapshots完成后patching 7:……
分类目录:python
python ‘dict’ object has no attribute ‘has_key’
Python Requests Library
如何通过Python脚本来检查网站的证书过期时间
近期发现有个别网站的证书到期后未及时更新导致的一系列问题,所以需要写个脚本监控一下 网站一搜发现都是需要你提前把证书弄下来的,有了证书,然后再去链接…..感觉有点麻烦(我就是懒)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#!/usr/bin/env python import urllib2 import httplib import ssl import socket import os CERT_FILE = os.path.join(os.path.dirname(__file__), 'cacert.pem') class ValidHTTPSConnection(httplib.HTTPConnection): "This class allows communication via SSL." default_port = httplib.HTTPS_PORT def __init__(self, *args, **kwargs): httplib.HTTPConnection.__init__(self, *args, **kwargs) def connect(self): "Connect to a host on a given (SSL) port." sock = socket.create_connection((self.host, self.port), self.timeout, self.source_address) if self._tunnel_host: self.sock = sock self._tunnel() self.sock = ssl.wrap_socket(sock, ca_certs=CERT_FILE, cert_reqs=ssl.CERT_REQUIRED) class ValidHTTPSHandler(urllib2.HTTPSHandler): def https_open(self, req): return self.do_open(ValidHTTPSConnection, req) opener = urllib2.build_opener(ValidHTTPSHandler) def test_access(url): print "Acessing", url page = opener.open(url) print page.info() data = page.read() print "First 100 bytes:", data[0:100] print "Done accesing", url print "" # This should work test_access("https://www.google.com") # Accessing a page with a self signed certificate should not work # At the time of writing, the following page uses a self signed certificate test_access("https://tidia.ita.br/") |
然后又找到一种不需要提前下载证书的: [crayon-6481ea3cf1cac6……
leetcode406根据身高重建队列(Queue Reconstruction by Height) python
1 2 3 4 5 6 7 8 9 10 11 12 |
假设有打乱顺序的一群人站成一个队列。 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数。 编写一个算法来重建这个队列。 注意: 总人数少于1100人。 示例 输入: [[7,0], [4,4], [7,1], [5,0], [6,1], [5,2]] 输出: [[5,0], [7,0], [5,2], [6,1], [4,4], [7,1]] |
思路:首先,我们把最大的数字按照顺序排好 ,然后再排小的数,就无所谓了,如图: 插入第一个元素后从第二个元素开始: 此时插入发现第二个元素[7,1]刚好放在第二个位置,K的值也符合 然后第三个元素[6,1]: 这个时候我们需要插入元素[6,1],注意,数组中已……
leetcode 338. 比特位计数Counting Bits python
1 2 3 4 5 6 7 8 9 10 |
给定一个非负整数 num。对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回。 示例 1: 输入: 2 输出: [0,1,1] 示例 2: 输入: 5 输出: [0,1,1,2,1,2] |
此题的暴力解法很容易理解,首先我们要先知道如何将10进制转换为2进制 然后我们要知道使用count函数来统计里边的1的个数
1 2 3 4 5 6 7 8 9 10 |
class Solution(object): def countBits(self, num): """ :type num: int :rtype: List[int] """ res = [] for i in xrange(num + 1): res.append(bin(i).count('1')) return res |