urllib equivalent to httplib2

A forum for general discussion of the Python programming language.

urllib equivalent to httplib2

Postby activecat » Tue Oct 01, 2013 5:46 am

I have successfully implement this (below code), it works fine.

Code: Select all
import httplib2
hhh = httplib2.Http(".cache")
url_index = 'https://icems.abc.com.my/webDiary/index.jsp'   
headers = { 'Content-type': 'application/x-www-form-urlencoded', 'User-agent': 'Mozilla/5.0' }     
resp1, content1 = hhh.request( url_index, "GET", headers=headers )
print( content1.decode('ISO-8859-1') )


Since httplib2 is not a python3 standard library, I would like to use urllib instead.
So I rewrite it as below:

Code: Select all
import urllib
url_index = 'https://icems.abc.com.my/webDiary/index.jsp'   
headers = { 'Content-type': 'application/x-www-form-urlencoded', 'User-agent': 'Mozilla/5.0' }   
req = urllib.request.Request( url_index, headers=headers )
resp = urllib.request.urlopen( req )


But this (using urllib) doesn't work. It fails with following error message:
---------------------------------------------------------------------------
URLError Traceback (most recent call last)
<ipython-input-43-1a9002134eea> in <module>()
----> 1 resp = urllib.request.urlopen( req )

/usr/lib/python3.2/urllib/request.py in urlopen(url, data, timeout, cafile, capath)
137 else:
138 opener = _opener
--> 139 return opener.open(url, data, timeout)
140
141 def install_opener(opener):

/usr/lib/python3.2/urllib/request.py in open(self, fullurl, data, timeout)
368 req = meth(req)
369
--> 370 response = self._open(req, data)
371
372 # post-process response

/usr/lib/python3.2/urllib/request.py in _open(self, req, data)
386 protocol = req.type
387 result = self._call_chain(self.handle_open, protocol, protocol +
--> 388 '_open', req)
389 if result:
390 return result

/usr/lib/python3.2/urllib/request.py in _call_chain(self, chain, kind, meth_name, *args)
346 for handler in handlers:
347 func = getattr(handler, meth_name)
--> 348 result = func(*args)
349 if result is not None:
350 return result

/usr/lib/python3.2/urllib/request.py in https_open(self, req)
1174 def https_open(self, req):
1175 return self.do_open(http.client.HTTPSConnection, req,
-> 1176 context=self._context, check_hostname=self._check_hostname)
1177
1178 https_request = AbstractHTTPHandler.do_request_

/usr/lib/python3.2/urllib/request.py in do_open(self, http_class, req, **http_conn_args)
1141 except socket.error as err: # timeout error
1142 h.close()
-> 1143 raise URLError(err)
1144 else:
1145 r = h.getresponse()

URLError: <urlopen error [Errno 104] Connection reset by peer>


Please advise, why it fails, and how to correct it (using urllib) ..?
Thanks in advance.
Last edited by Yoriz on Tue Oct 01, 2013 10:09 am, edited 1 time in total.
Reason: First post lock
activecat
 
Posts: 1
Joined: Tue Oct 01, 2013 5:32 am

Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests

cron