def mangle(self, word): year = datatime.now().year suffixes = ["", "1", "!", year] mangled = [] for password in (word, word.capitalize()): for suffix in suffixes: mangled.append("%s%s" % (password, suffix)) return mangled
from datatime import datatime print(datatime.now().strftime('今天是%Y年%m月%d日'))
def __init__(self, email, content, created=None): self.email = email self.content = content self.created = created or datatime.now()
from datatime import datatime, timedelta dt1 = datatime(2018, 1, 1) + timedelta(days=1, seconds=1) print(dt1) dt2 = datatime.now() duration = dt2 - dt1 print(duration) print("days", duration.days) print("seconds", duration.seconds) print("total_seconds", duration.total_seconds()) # data time objects, these objects represent a point in time. In datatime module also class called time delta, which represents a duration. # why youre wondering why we don't have month or year here, the reason is that we can have a varying amount of time in a montha or in year,That's why only days, seconds and microseconds.
def _packet_in_handler(self, ev): msg = ev.msg # OpenFlow event message datapath = msg.datapath # Switch class that received the packet ofproto = datapath.ofproto # OpenFlow protocol class # Parse packet pkt = packet.Packet(msg.data) eth = pkt.get_protocol(ethernet.ethernet) # Ignore lldp packet if eth.ethertype == ether_types.ETH_TYPE_LLDP: return dst = eth.dst src = eth.src dpid = datapath.id self.logger.info( "--Packet IN: Switch id[%s], Src MAC[%s], Dst MAC[%s], Port[%s]", dpid, src, dst, msg.in_port) action = "allow" if dst == 'ff:ff:ff:ff:ff:ff': self.logger.info(" Broadcast packet") if eth.ethertype == ether_types.ETH_TYPE_ARP: arp_packet = pkt.get_protocol(arp.arp) if arp_packet.opcode == 1: arp_dst_ip = arp_packet.dst_ip arp_src_ip = arp_packet.src_ip self.logger.info(" Received ARP request for dst IP %s" % arp_dst_ip) if arp_dst_ip in self.switch: switch_mac = self.switch[arp_dst_ip][MAC] self.send_arp_reply(datapath, switch_mac, arp_packet.dst_ip, src, arp_src_ip, msg.in_port) self.logger.info( " Sent gratious ARP reply [%s]-[%s] to %s " % (arp_packet.dst_ip, switch_mac, arp_packet.src_ip)) return 0 actions = [ datapath.ofproto_parser.OFPActionOutput(ofproto.OFPP_FLOOD) ] data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: #packet is not buffered on switch data = msg.data out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port, actions=actions, data=data) self.logger.info(" Flooding packet to all other ports") datapath.send_msg(out) return ip4_pkt = pkt.get_protocol(ipv4.ipv4) if ip4_pkt: global gateway self.logger.info(" --- IP LOOKUP") src_ip = ip4_pkt.src dst_ip = ip4_pkt.dst self.logger.info(" --- src_ip[%s], dst_ip[%s]" % (src_ip, dst_ip)) ipRicercato = fromIPtoBinary(dst_ip) t1 = datatime.now() ricerca(ipRicercato, deep, indice) t2 = datatime.now() self.logger.info('\n\n##########Duration: {}'.format(t2 - t1)) sw = binary2ip[gateway] gateway = None if sw is not None: #if dst_ip in self.lookup: #sw = self.lookup[dst_ip] self.logger.info(" --- Destination present on switch %s" % (self.switch[sw])) dp = get_datapath(self, int(self.switch[sw][DPID])) out_port = self.mac_to_port[self.switch[sw][DPID]][ self.ip_to_mac[dst_ip]] self.logger.info(" --- Output port set to %s" % (out_port)) actions = [dp.ofproto_parser.OFPActionOutput(int(out_port))] data = msg.data pkt = packet.Packet(data) eth = pkt.get_protocol(ethernet.ethernet) #change the mac address of packet eth.dst = self.ip_to_mac[dst_ip] self.logger.info(" --- Changing destination mac to %s" % (eth.dst)) pkt.serialize() out = dp.ofproto_parser.OFPPacketOut( datapath=dp, buffer_id=0xffffffff, in_port=datapath.ofproto.OFPP_CONTROLLER, actions=actions, data=pkt.data) print("---------") dp.send_msg(out) return # Forward the packet if dst in self.mac_to_port[str(dpid)]: out_port = self.mac_to_port[str(dpid)][dst] self.logger.info( " Destination MAC is on port %s. Forwarding the packet", out_port) else: out_port = ofproto.OFPP_FLOOD self.logger.info( " Destination MAC not present in table. Flood the packet") actions = [datapath.ofproto_parser.OFPActionOutput(int(out_port))] data = None if msg.buffer_id == ofproto.OFP_NO_BUFFER: data = msg.data out = datapath.ofproto_parser.OFPPacketOut(datapath=datapath, buffer_id=msg.buffer_id, in_port=msg.in_port, actions=actions, data=data) datapath.send_msg(out)
# Import libraries import time from datatime import datatime as dt # Windows host file path hostsPath=r"C:\Windows\System32\drivers\etc\hosts" redirect="127.0.0.1" # Add the website you want to block, in this list while True: # Duration during which, website blocker will work if dt(dt.now().year,dt.now().month,dt.now().day,9) < dt.now() < dt(dt.now().year,dt.now().month,dt.now().day,18): print ("Sorry Not Allowed...") with open(hostsPath, 'r+') as file: content=file.read() for site in websites: if site in content: pass else: file.write(redirect+" "+site+"\n") else: with open(hostsPath, 'r+') as file: content=file.readlines() file.seek(0) for line in content: if not any(site in line for site in websites): file.write(line) file.truncate() print ("Allowed Access !")
''' Zaznacz prawidłowe odp: - c.tzinfo is None. - a is equal to zero. - d computes the time that has passed since the epoch. - Can replace the call to datetime.utcnow with datetime.now and will get the same result - b is equal to 2018-1-2B ''' from datatime import datatime a = (datatime.now() - datatime.now()).total_seconds() b = datatime(2018, 1, 2, 2, 3).strftime("%Y-%m-%d") c = datatime.now() d = datatime.utcnow() - datatime(1970, 1, 1, 0, 0, 0) d2 = datatime.now() - datatime(1970, 1, 1, 0, 0, 0) print(a) print(b) print(c) print(d) print(d1)
from datatime import datatime print(datatime.now().strftime('Today is %Y-%m-%d'))