Форум сайта python.su
Допустим надо скачать фильм с этого сайта http://kinohouse.ucoz.ru/news/android_smotret_onlajn_film_fantastika_2013_android_app/2013-12-20-4579
нахожу ссылку на фильм
http://cdn22.video.mail.ru/v/45402413.mp4?sign=e01ce7002f674493828b4e17e954a792b81f153c&slave=s%3Ahttp%3A%2F%2F127.0.0.1%3A5010%2F45402413-v.mp4&p=0&expire_at=1387696551&touch=1387474767
такого вида
если вставляю ссылку в браузер - скачивает файл , если программой
def download(url): u = urllib2.urlopen(url) output = open('out.mp4','wb') meta = u.info() file_size = int(meta.getheaders("Content-Length")[0]) print "Downloading: %s Bytes: %s" % ('out.mp4', file_size) file_size_dl = 0 block_sz = 8192 while True: buffer = u.read(block_sz) if not buffer: break file_size_dl += len(buffer) output.write(buffer) status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size) #status = status + chr(8)*(len(status)+1) print status output.close()
Отредактировано sanodin (Дек. 21, 2013 21:23:52)
Офлайн
Прочитайте файл через .read() и запишите его в файл бинарным кодом:
from urllib.request import urlopen logo = urlopen("http://habr.habrastorage.org/post_images/8" "fa/6b4/f63/8fa6b4f632b01dd0cb5d8f5cd137ea5a.png").read() with open("logo3.png", "wb") as f: f.write(logo)
import urllib.request urllib.request.urlretrieve ("http://habr.habrastorage.org/post_images/" "31c/b70/92c/31cb7092cf09ccf23bf3f301e3b16cc4.jpg", "logo.jpg")
Отредактировано Razor (Дек. 21, 2013 13:41:25)
Офлайн
так уже делал, только под второй питон
urllib.urlretrieve(url, "code.zip")
Отредактировано sanodin (Дек. 21, 2013 14:42:37)
Офлайн
я фильм на сайте тоже не могу посмотреть
Офлайн
Singularityв смысле?
я фильм на сайте тоже не могу посмотреть
# -*- coding: cp1251 -*- #убрал выбор сетевой карты dev = raw_input("Enter device name to sniff : ") import socket from struct import * import datetime import pcapy import sys import urllib2 def main(argv): #list all devices devices = pcapy.findalldevs() print devices #ask user to enter device name to sniff print "Available devices are :" for d in devices : print d dev = d#raw_input("Enter device name to sniff : ") print "Sniffing device " + dev ''' open device # Arguments here are: # device # snaplen (maximum number of bytes to capture _per_packet_) # promiscious mode (1 for true) # timeout (in milliseconds) ''' cap = pcapy.open_live(dev , 65536 , 1 , 0) #start sniffing packets while(1) : (header, packet) = cap.next() #print ('%s: captured %d bytes, truncated to %d bytes' %(datetime.datetime.now(), header.getlen(), header.getcaplen())) parse_packet(packet) #Convert a string of 6 characters of ethernet address into a dash separated hex string def eth_addr (a) : b = "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x" % (ord(a[0]) , ord(a[1]) , ord(a[2]), ord(a[3]), ord(a[4]) , ord(a[5])) return b #function to parse a packet def parse_packet(packet) : global flvideo #parse ethernet header eth_length = 14 eth_header = packet[:eth_length] eth = unpack('!6s6sH' , eth_header) eth_protocol = socket.ntohs(eth[2]) #print 'Destination MAC : ' + eth_addr(packet[0:6]) + ' Source MAC : ' + eth_addr(packet[6:12]) + ' Protocol : ' + str(eth_protocol) #Parse IP packets, IP Protocol number = 8 if eth_protocol == 8 : #Parse IP header #take first 20 characters for the ip header ip_header = packet[eth_length:20+eth_length] #now unpack them :) iph = unpack('!BBHHHBBH4s4s' , ip_header) version_ihl = iph[0] version = version_ihl >> 4 ihl = version_ihl & 0xF iph_length = ihl * 4 ttl = iph[5] protocol = iph[6] s_addr = socket.inet_ntoa(iph[8]); d_addr = socket.inet_ntoa(iph[9]); #print 'Version : ' + str(version) + ' IP Header Length : ' + str(ihl) + ' TTL : ' + str(ttl) + ' Protocol : ' + str(protocol) + ' Source Address : ' + str(s_addr) + ' Destination Address : ' + str(d_addr) #TCP protocol if protocol == 6 : t = iph_length + eth_length tcp_header = packet[t:t+20] #now unpack them :) tcph = unpack('!HHLLBBHHH' , tcp_header) source_port = tcph[0] dest_port = tcph[1] sequence = tcph[2] acknowledgement = tcph[3] doff_reserved = tcph[4] tcph_length = doff_reserved >> 4 #print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Sequence Number : ' + str(sequence) + ' Acknowledgement : ' + str(acknowledgement) + ' TCP header length : ' + str(tcph_length) h_size = eth_length + iph_length + tcph_length * 4 data_size = len(packet) - h_size #get data from the packet data = packet[h_size:] #print data #ICMP Packets elif protocol == 1 : u = iph_length + eth_length icmph_length = 4 icmp_header = packet[u:u+4] #now unpack them :) icmph = unpack('!BBH' , icmp_header) icmp_type = icmph[0] code = icmph[1] checksum = icmph[2] #print 'Type : ' + str(icmp_type) + ' Code : ' + str(code) + ' Checksum : ' + str(checksum) h_size = eth_length + iph_length + icmph_length data_size = len(packet) - h_size #get data from the packet data = packet[h_size:] #print data #UDP packets elif protocol == 17 : u = iph_length + eth_length udph_length = 8 udp_header = packet[u:u+8] #now unpack them :) udph = unpack('!HHHH' , udp_header) source_port = udph[0] dest_port = udph[1] length = udph[2] checksum = udph[3] #print 'Source Port : ' + str(source_port) + ' Dest Port : ' + str(dest_port) + ' Length : ' + str(length) + ' Checksum : ' + str(checksum) h_size = eth_length + iph_length + udph_length data_size = len(packet) - h_size #get data from the packet data = packet[h_size:] #print data #some other IP packet like IGMP else : print 'Protocol other than TCP/UDP/ICMP' print try: flv = data.replace('\r\n',' ') flv = flv.split(' ') b = flv[1] a = flv[4] flvideo = a + b print flvideo except Exception as e: print e.message main(sys.argv)
Отредактировано sanodin (Дек. 21, 2013 15:04:57)
Офлайн