httplib2

Python httplib2 Handling Exceptions

末鹿安然 提交于 2019-12-20 14:42:23
问题 I have this very simple code to check if a site is up or down. import httplib2 h = httplib2.Http() response, content = h.request("http://www.folksdhhkjd.com") if response.status == 200: print "Site is Up" else: print "Site is down" When I enter a valid URL then it properly prints Site is Up because the status is 200 as expected. But, when I enter an invalid URL, should it not print Site is down? Instead it prints an exception something like this Traceback (most recent call last): File "C:

python, Json and string indices must be integers, not str

孤人 提交于 2019-12-19 19:46:50
问题 I am using Python, and I sent a request to a URL and received a reply using httplib2. The reply I got was in JSon, how do I access a specific parameter. What I have at the moment is: resp, content = parser.request(access_token_uri, method = 'POST', body = params, headers = headers) raise Exception(content['access_token']) and I get the error string indices must be integers, not str How do I do it? Thanks 回答1: Well if the response type is json and it comes in type str. If you are running 2.4

Python create cookies and then load a page with the cookies

ε祈祈猫儿з 提交于 2019-12-18 17:22:06
问题 I would like to access a web page from a python program. I have to set up cookies to load the page. I used the httplib2 library, but I didn't find how add my own cookie resp_headers, content = h.request("http://www.theURL.com", "GET") How can I create cookies with the right name and value, add it to the function and then load the page? Thanks 回答1: http = httplib2.Http() # get cookie_value here headers = {'Cookie':cookie_value} response, content = http.request("http://www.theURL.com", 'GET',

How do I get the IP address from a http request using the requests library?

五迷三道 提交于 2019-12-17 10:42:34
问题 I am making HTTP requests using the requests library in python, but I need the ip address from the server that responded the http request and I'm trying to avoid to make two calls (and possibly having a different ip address from the one that responded the request. Is that possible? Does any python http library allows me to do that? ps: I also need to make HTTPS requests and to use an authenticated proxy. Update 1: Example: import requests proxies = { "http": "http://user:password@10.10.1.10

How do I get the IP address from a http request using the requests library?

狂风中的少年 提交于 2019-12-17 10:42:02
问题 I am making HTTP requests using the requests library in python, but I need the ip address from the server that responded the http request and I'm trying to avoid to make two calls (and possibly having a different ip address from the one that responded the request. Is that possible? Does any python http library allows me to do that? ps: I also need to make HTTPS requests and to use an authenticated proxy. Update 1: Example: import requests proxies = { "http": "http://user:password@10.10.1.10

how to follow meta refreshes in Python

跟風遠走 提交于 2019-12-17 07:37:18
问题 Python's urllib2 follows 3xx redirects to get the final content. Is there a way to make urllib2 (or some other library such as httplib2) also follow meta refreshes? Or do I need to parse the HTML manually for the refresh meta tags? 回答1: Here is a solution using BeautifulSoup and httplib2 (and certificate based authentication): import BeautifulSoup import httplib2 def meta_redirect(content): soup = BeautifulSoup.BeautifulSoup(content) result=soup.find("meta",attrs={"http-equiv":"Refresh"}) if

Python requests PUT method creates a zero byte file

喜欢而已 提交于 2019-12-13 06:17:46
问题 I am trying to upload a file with PUT using the requests module in python. my code is this: with open(file, 'rb') as payload: r = requests.put(url, data=payload, auth=('username', 'password')) The file is created, I am getting a response 200, but it has 0 bytes. If I am not doing something wrong I suspect that I have met the bug here. Is this the case? If yes is there any workaround that I can try? I've tried also the same with the httplib2 library with open(file, 'rb') as payload: h =

google-app-engine : ImportError httplib2 in google api python client hello world

两盒软妹~` 提交于 2019-12-13 01:24:47
问题 I just downloaded the Google App Engine SDK for python (google_appengine_1.6.5.zip) and i try to launch an example from google-api-python-client (appengine) : $ tree . |-- app.yaml |-- client_secrets.json |-- grant.html |-- index.yaml |-- main.py `-- welcome.html I launch dev server : : ./dev_appserver.py /home/yoyo/dev/projets/yoyocontacts/ --backends --clear_datastore --high_replication But when i launched application in my browser, i have the following error : ImportError: No module named

in Python, I want to do rest requests with HTTP and NOT with HTTPS, packages don't seem to agree

守給你的承諾、 提交于 2019-12-11 14:04:23
问题 I have tried to use request: url = 'http://bot' # this is a local service import requests r = requests.get(url) and I get requests.exceptions.SSLError: HTTPSConnectionPool(host='bot', port=443) I specified HTTP, not HTTPS, but it tried to connect with SSL. So, I tried httplib2: url = 'http://bot' # this is a local service response, content = http.request(url, "GET") and I get: ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer

socket.getaddrinfo() block forever when using with gevent

亡梦爱人 提交于 2019-12-11 09:18:29
问题 Using httplib2 , I am sending multiple requests with gevent , after some time http.request() method of httplib2 is getting blocked forever. On checking I found that blocking function is socket.getaddrinfo() in httplib2 link. 回答1: set default timeout using socket.setdefaulttimeout() . since timeout of getaddrinfo is not specified it will use default timeout. 来源: https://stackoverflow.com/questions/24883247/socket-getaddrinfo-block-forever-when-using-with-gevent