urllib2

Using client certificates with urllib2

回眸只為那壹抹淺笑 提交于 2019-11-28 19:26:13
I need to create a secure channel between my server and a remote web service. I'll be using HTTPS with a client certificate. I'll also need to validate the certificate presented by the remote service. How can I use my own client certificate with urllib2? What will I need to do in my code to ensure that the remote certificate is correct? Here's a bug in the official Python bugtracker that looks relevant, and has a proposed patch. Because alex's answer is a link, and the code on that page is poorly formatted, I'm just going to put this here for posterity: import urllib2, httplib class

Python/Django download Image from URL, modify, and save to ImageField

杀马特。学长 韩版系。学妹 提交于 2019-11-28 19:22:09
问题 I've been looking for a way to download an image from a URL, preform some image manipulations (resize) actions on it, and then save it to a django ImageField. Using the two great posts (linked below), I have been able to download and save an image to an ImageField. However, I've been having some trouble manipulating the file once I have it. Specifically, the model field save() method requires a File() object as the second parameter. So my data has to eventually be a File() object. The blog

How do I add a header to urllib2 opener?

时光怂恿深爱的人放手 提交于 2019-11-28 18:37:32
cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) opener.open('http://abc.com') opener.open('http://google.com') As you can see, I use opener to visit different websites, using a cookie jar. Can I set a header so that each time a website is it, the header is applied? You can add the headers directly to the OpenerDirector object returned by build_opener . From the last example in the urllib2 docs : OpenerDirector automatically adds a User-Agent header to every Request. To change this: import urllib2 opener = urllib2.build_opener() opener.addheaders = [(

urllib2 POST progress monitoring

我怕爱的太早我们不能终老 提交于 2019-11-28 18:27:47
I'm uploading a fairly large file with urllib2 to a server-side script via POST. I want to display a progress indicator that shows the current upload progress. Is there a hook or a callback provided by urllib2 that allows me to monitor upload progress? I know that you can do it with download using successive calls to the connection's read() method, but I don't see a write() method, you just add data to the request. It is possible but you need to do a few things: Fake out the urllib2 subsystem into passing a file handle down to httplib by attaching a __len__ attribute which makes len(data)

How do you get default headers in a urllib2 Request?

心已入冬 提交于 2019-11-28 17:58:31
I have a Python web client that uses urllib2. It is easy enough to add HTTP headers to my outgoing requests. I just create a dictionary of the headers I want to add, and pass it to the Request initializer. However, other "standard" HTTP headers get added to the request as well as the custom ones I explicitly add. When I sniff the request using Wireshark, I see headers besides the ones I add myself. My question is how do a I get access to these headers? I want to log every request (including the full set of HTTP headers), and can't figure out how. any pointers? in a nutshell: How do I get all

How to convert a dictionary to query string in Python?

不羁的心 提交于 2019-11-28 17:41:46
After using cgi.parse_qs() , how to convert the result (dictionary) back to query string? Looking for something similar to urllib.urlencode() . Ignacio Vazquez-Abrams Python 3 urllib.parse. urlencode (query, doseq=False, [...]) Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string. — Python 3 urllib.parse docs A dict is a mapping. Legacy Python urllib.urlencode ( query [, doseq ]) Convert a mapping object or a sequence of two-element tuples to a “percent-encoded” string... a series of key=value pairs

How do I open an image from the internet in PIL?

此生再无相见时 提交于 2019-11-28 17:04:24
I would like to find the dimensions of an image on the internet. I tried using from PIL import Image import urllib2 as urllib fd = urllib.urlopen("http://a/b/c") im = Image.open(fd) im.size as suggested in this answer , but I get the error message addinfourl instance has no attribute 'seek' I checked and objects returned by urllib2.urlopen(url) do not seem to have a seek method according to dir . So, what do I have to do to be able to load an image from the Internet into PIL? Snakes and Coffee You might consider using io.BytesIO for forward compatibility . The StringIO and cStringIO modules do

python3 下载网络图片

南笙酒味 提交于 2019-11-28 16:07:53
说明:这里仅展示在已经获取图片链接后的下载方式,对于爬虫获取链接部分参考前面的文章 1、利用文件读写的方式下载图片 #第一种:用urllib2模块下载 import urllib2 link = ' ' headers = { } request = urllib2.Request(link, headers=headers) image = urllib2.urlopen(request).read() filename = link[-5:]# 注意这里要用wb模式 with open (filename, "wb") as f: f.write(image) #第二种:requests模块下载同理 2、urlretrieve()方法 import os from urllib.request import urlretrieve ....... urlretrieve(link, "路径"+"文件名") 来源: https://www.cnblogs.com/muouran0120/p/11414606.html

BeautifulSoup, where are you putting my HTML?

拟墨画扇 提交于 2019-11-28 14:14:39
I'm using BS4 with python2.7. Here's the start of my code (Thanks root): from bs4 import BeautifulSoup import urllib2 f=urllib2.urlopen('http://yify-torrents.com/browse-movie') html=f.read() soup=BeautifulSoup(html) When I print html, its contents are the same as the source of the page viewed in chrome. When I print soup however, it cuts out all the entire body and leaves me with this (the contents of the head tag): <!DOCTYPE html> <html> <head> <title>Browse Movie - YIFY Torrents</title> <meta charset="utf-8"> <meta content="IE=9" http-equiv="X-UA-Compatible"/> <meta content="YIFY-Torrents

urllib2 opener providing wrong charset

倾然丶 夕夏残阳落幕 提交于 2019-11-28 12:35:23
When I open the url and read it, I can't recognize it. But when I check the content header it says it is encoded as utf-8. So I tried to convert it to unicode and it complained UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 1: ordinal not in range(128) using unicode(). .encode("utf-8") produces UnicodeDecodeError: 'ascii' codec can't decode byte 0x8b in position 1: ordinal not in range(128) .decode("utf-8") produced UnicodeDecodeError: 'utf8' codec can't decode byte 0x8b in position 1: invalid start byte. I have tried everything I can come up with(I'm not that good at