-
-
Save kennethreitz/973705 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import urllib2 | |
gh_url = 'https://api.github.com' | |
req = urllib2.Request(gh_url) | |
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() | |
password_manager.add_password(None, gh_url, 'user', 'pass') | |
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) | |
opener = urllib2.build_opener(auth_manager) | |
urllib2.install_opener(opener) | |
handler = urllib2.urlopen(req) | |
print handler.getcode() | |
print handler.headers.getheader('content-type') | |
# ------ | |
# 200 | |
# 'application/json' |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import requests | |
r = requests.get('https://api.github.com', auth=('user', 'pass')) | |
print r.status_code | |
print r.headers['content-type'] | |
# ------ | |
# 200 | |
# 'application/json' |
Và có vẻ như bạn có thể đơn giản hóa nó hơn nữa:
nhập urllib2 từ base64 nhập b64encode yêu cầu = urllib2 . Yêu cầu ( 'https://api.github.com/user' ) <meta property="fb:pages" content="112008217311363" /> _header ( 'Ủy quyền' , 'Cơ bản' + b64encode ( 'user' + ':' + 'pass' )) r = urllib2 . urlopen ( yêu cầu ) in r . getcode () print r . headers [ "content-type" ] print r . tiêu đề [ "X-RateLimit-Limit" ]Vì vậy, nó chỉ có 3 dòng và nó tương thích với
urllib2
cuộc gọi thông thường . Vì vậy, mặc dù API củaurllib2
thực sự không tốt, nhưng nó không tệ như ví dụ ban đầu của bạn. Vì vậy, tôi nghĩ rằng bạn nên sử dụng phiên bản 3 dòng này để thay thế cho công bằng.CHỈNH SỬA: Đảm bảo rằng bạn sử dụng
b64encode
chức năng trên. Sau đó, bạn không cần xóa dấu '\ n' theo sau khỏi chuỗi.CHỈNH SỬA 2: Đơn giản hơn một chút. Tôi nghĩ nó đơn giản như bây giờ.
It's look interesting guys keep going.
Já utilizaram request no Plone versão 4.3.18? Com os mesmo códigos sugeridos, tenho no Plone resposta de "Privilegios Insuficientes."
Have you already used request in Plone version 4.3.18? With the same suggested codes, I have "Insufficient Privileges" in Plone.
nice
import requests
response = requests.get("https://en.wikipedia.org/wiki/Python")
print(response)
print(response.status_code)
Really simple
URLLIB (Python3)
from urllib import urlopen
r = urlopen('https://google.com/')
print(r.read().decode())
print(r.code)
Requests (Python3)
from requests import get
r = get('https://google.com/')
print(r.text)
print(r.status_code)
4 lines in each code !
But yes, when (like in the example) you want to login to a website, thats annother thing...
URLLIB is great for little things if you cant download Requests.
Requests is really better because Python code with Requests its a lot more human-readable.
0_urllib2.py
#!/usr/bin/env python
-- coding: utf-8 --
import urllib2
gh_url = 'https://api.github.com'
req = urllib2.Request(gh_url)
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, gh_url, 'user', 'pass')
auth_manager = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(auth_manager)
urllib2.install_opener(opener)
handler = urllib2.urlopen(req)
print handler.getcode()
print handler.headers.getheader('content-type')
------
200
'application/json'``
[[](https://perfectbt. com/crash/php)
@ HGStyle wrote…
4 lines in each code ! But yes, when (like in the example) you want to login to a website, thats annother thing... URLLIB is great for little things if you cant download Requests. Requests is really better because Python code with Requests its a lot more human-readable.
Yes, that's the point of @kennethreitz's examples. Operations that are complicated with urllib
are simple with requests
.
@source-creator wrote...
Unfortunately, code to format an url with requests is longer and more bloated than urllib:
# requests from requests import Request url = Request(None, 'http://example.com/?', params={'Data1': 'data'}).prepare().url # urllib import urllib url = 'http://example.com/?' + urllib.parse.urlencode({'Data1': 'data'})
Just read this: https://stackoverflow.com/a/46783596
You can just use requests.utils
(and other submodules, I've seen annother one for functions included in urllib.parse but I don't remember it) instead of urllib.parse in most cases I think.
(and it does not requires requests.utils to be imported because importing requests also imports requests.utils)
NOTE: The functions are comming right from the URLLIB package, so the actual documentation is in URLLIB for some (not all) of the function of this submodule of requests.
@HeyITGuyFixIt : https://gist.github.com/andrewwatts/2012630