urllib2

python urllib2 basic authentication

≡放荡痞女 提交于 2020-01-21 07:14:48
问题 Hi i'm trying to use python to access an API URL using urllib2: import urllib2 url = 'https://XXXXXXXXXX.com/' username = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' password = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener) pagehandle = urllib2.urlopen(url) I don't know what the

Make an http POST request to upload a file using python urllib/urllib2

非 Y 不嫁゛ 提交于 2020-01-20 00:59:05
问题 I would like to make a POST request to upload a file to a web service (and get response) using python. For example, I can do the following POST request with curl : curl -F "file=@style.css" -F output=json http://jigsaw.w3.org/css-validator/validator How can I make the same request with python urllib/urllib2? The closest I got so far is the following: with open("style.css", 'r') as f: content = f.read() post_data = {"file": content, "output": "json"} request = urllib2.Request("http://jigsaw.w3

Sublime Text 解决中文乱码

感情迁移 提交于 2020-01-16 01:16:01
sublime text 是一款非常优秀的跨平台文本及源代码编辑器,本人非常喜欢,但是不支持GB2312和GBK编码在某些时候比较麻烦。可以通过向sublime text 中添加编码类型转换包(比如“ConvertToUTF8")来解决这一问题。具体方法如下: 首先声明:这一方法前提是sublime text 2的安装路径没有中文字符,且系统路径的%username%中不包含中文字符 安装Sublime Package Control: 1.在Sublime Text 用Ctrl+~打开控制台并在里面输入以下代码,Sublime Text就会自动安装 Package Control ; import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen(' http://sublime.wbond.net/'

using urllib2 to execute URL and return rendered HTML output, not the HTML itself [duplicate]

和自甴很熟 提交于 2020-01-16 01:01:21
问题 This question already has answers here : Python library for rendering HTML and javascript [closed] (2 answers) Closed 6 years ago . urllib2.urlopen("http://www.someURL.com/pageTracker.html").read(); The code above will return the source HTML at http://www.google.com. What do I need to do to actually return the rendered HTML that you see when you visit google.com? I essentially trying to 'execute' a URL to trigger a view, not retrieve the HTML. To clarify a few things: I'm not actually

Python urllib2 URLError exception?

两盒软妹~` 提交于 2020-01-15 12:12:14
问题 I installed Python 2.6.2 earlier on a Windows XP machine and run the following code: import urllib2 import urllib page = urllib2.Request('http://www.python.org/fish.html') urllib2.urlopen( page ) I get the following error. Traceback (most recent call last):<br> File "C:\Python26\test3.py", line 6, in <module><br> urllib2.urlopen( page )<br> File "C:\Python26\lib\urllib2.py", line 124, in urlopen<br> return _opener.open(url, data, timeout)<br> File "C:\Python26\lib\urllib2.py", line 383, in

I get HTTP 500 error when use python urllib2.open

╄→гoц情女王★ 提交于 2020-01-15 11:05:55
问题 The code is like. The url is my blog and I'd like to fetch and backup my blog posts. Things goes well with my other blog posts but return 500 error with this one. usock = urlopen("http://xiaoshuaistudio.ycool.com/post.4606754.html") htmlSource = usock.read() usock.close() Can you help me figure out how to debug a HTTP 500 error? 回答1: A page content is loaded, and a 500 error is returned. But I myself can't read whether the page content is relevant. To avoid error on this specific page or

I get HTTP 500 error when use python urllib2.open

こ雲淡風輕ζ 提交于 2020-01-15 11:04:35
问题 The code is like. The url is my blog and I'd like to fetch and backup my blog posts. Things goes well with my other blog posts but return 500 error with this one. usock = urlopen("http://xiaoshuaistudio.ycool.com/post.4606754.html") htmlSource = usock.read() usock.close() Can you help me figure out how to debug a HTTP 500 error? 回答1: A page content is loaded, and a 500 error is returned. But I myself can't read whether the page content is relevant. To avoid error on this specific page or

HTTP Authentication in URL with backslash in username

假装没事ソ 提交于 2020-01-14 05:43:07
问题 I need to HTTP Basic Auth for a REST call. In the username I have to provide a domain (which has a hyphen) and then a backslash to separate it from the username, like this: DOM-AIN\user_name . Then the password is pretty benign. This works fine with curl: curl 'https://DOM-AIN\user_name:password@myurl.com' I need to put this into Python now, but I've tried with requests and urllib/2/3...they don't like the \ : or the @ . Even when I URL encode to %40, etc., those get interpreted as an actual

multithreaded file download in python and updating in shell with download progress

允我心安 提交于 2020-01-14 04:21:17
问题 in an attempt to learn multithreaded file download I wrote this piece of cake: import urllib2 import os import sys import time import threading urls = ["http://broadcast.lds.org/churchmusic/MP3/1/2/nowords/271.mp3", "http://s1.fans.ge/mp3/201109/08/John_Legend_So_High_Remix(fans_ge).mp3", "http://megaboon.com/common/preview/track/786203.mp3"] url = urls[1] def downloadFile(url, saveTo=None): file_name = url.split('/')[-1] if not saveTo: saveTo = '/Users/userName/Desktop' try: u = urllib2

multithreaded file download in python and updating in shell with download progress

给你一囗甜甜゛ 提交于 2020-01-14 04:21:08
问题 in an attempt to learn multithreaded file download I wrote this piece of cake: import urllib2 import os import sys import time import threading urls = ["http://broadcast.lds.org/churchmusic/MP3/1/2/nowords/271.mp3", "http://s1.fans.ge/mp3/201109/08/John_Legend_So_High_Remix(fans_ge).mp3", "http://megaboon.com/common/preview/track/786203.mp3"] url = urls[1] def downloadFile(url, saveTo=None): file_name = url.split('/')[-1] if not saveTo: saveTo = '/Users/userName/Desktop' try: u = urllib2