I am writing a program which can read json of ipsw.me in python3:
import urrlib.request as ur
url = "https://api.ipsw.me/v4/device/" + device
json_file = ur.urlopen(url)
with open("signedipsw.json",'wb') as output:
output.write(json_file.read())
data = json.load(open("signedipsw.json"))
i = 0
with open("signedipsw.json"):
print("signed firmwares for %s:" % device)
for i in range(0, len(data["firmwares"])):
if data["firmwares"][i]["signed"] == True :
print("%s - %s" % (data["firmwares"][i]["version"], data["firmwares"][i]["buildid"]))
i+=1
I used to write this in Python2 but I used urrlib2 module and it worked perfectly. Now, on Python3 I use urrlib.request but I received an error:
json_file = ur.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 214, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 517, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 534, in _open
result = self._call_chain(self.handle_open, protocol, protocol +
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 494, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1385, in https_open
return self.do_open(http.client.HTTPSConnection, req,
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/urllib/request.py", line 1345, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)>
How can I fix this? Thanks
It looks like urllib (not urrlib) is not able to verify you're talking to the real ipsw.me. They're using Cloudflare's certificates.
You'll have a much better time using the Requests module. It comes with a more recent certificate bundle, which will likely fix your problem.
import requests
device = "..."
resp = requests.get(f"https://api.ipsw.me/v4/device/{device}")
resp.raise_for_status()
data = resp.json()
print(f"signed firmwares for {device}:")
for fw in data["firmwares"]:
if fw["signed"]:
print(f"{fw['version']} - {fw['buildid']}")
Related
I am trying to send SMS following Python suds procedure but facing some problems related to urllib2
The API URL, username & are provided by mobile service company when i try to execute the code i receive some errors relate to ###urllib2.URLError###
from suds.client import Client
client = Client(url='https://cbs.zong.com.pk/reachcwsv2 /corporatesms.svc?wsdl',
username='9231089894269149',
password='Zo98989ng#123', cache=None)
obj_QuickSMS = client.factory.create('obj_QuickSMS')
obj_QuickSMS.loginId = '91313212304269149'
obj_QuickSMS.loginPassword = 'zqong#435'
obj_QuickSMS.Destination = '923459332997'
obj_QuickSMS.Mask = 'Edex'
obj_QuickSMS.Message = 'HELLO'
obj_QuickSMS.UniCode = '0'
obj_QuickSMS.ShortCodePrefered = 'n'
result = client.service.QuickSMS(obj_QuickSMS)
i am receiving the following error:
File "/usr/lib/python2.7/urllib2.py", line 407, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1228, in http_open
return self.do_open(httplib.HTTPConnection, req)>
File "/usr/lib/python2.7/urllib2.py", line 1198, in do_open
raise URLError(err)
urllib2.URLError:
You have white space in your url, it should be like this:
client = Client(url='https://cbs.zong.com.pk/reachcwsv2/corporatesms.svc?wsdl',
username='9231089894269149',
password='Zo98989ng#123', cache=None)
I am new to Python, and have no coding background at all.
I am trying to use my Raspberry Pi to send data to Google Analytics. The problem is that Google Analytics Measurement Protocol usses HTTPS - Which my code doesn't work with.
So my current code looks like this
import urllib2
import time
import RPi.GPIO as io
io.setmode(io.BCM)
door_sensor = 18
sensorTrigger = True
io.setup(door_sensor, io.IN, pull_up_down=io.PUD_UP)
# function for the door opening
def door_open():
print("Door Open")
urllib2.urlopen("https://www.google-analytics.com/collect?v=1&tid=UA- 3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office").close
# function for the door closing
def door_close():
print("Door Close")
while True:
if io.input(door_sensor): # if door is opened
if (sensorTrigger):
door_open() # fire GA code
sensorTrigger = False # make sure it doesn't fire again
if not io.input(door_sensor): # if door is closed
if not (sensorTrigger):
door_close() # fire GA code
sensorTrigger = True # make sure it doesn't fire again
and the error that I keep getting is...
Traceback (most recent call last):
File "/home/pi/Desktop/GADoorSensor.py", line 23, in <module>
door_open() # fire GA code
File "/home/pi/Desktop/GADoorSensor.py", line 14, in door_open
urllib2.urlopen("https://www.google-analytics.com/collect?v=1&tid=UA-3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office").close
File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 431, in open
response = self._open(req, data)
File "/usr/lib/python2.7/urllib2.py", line 449, in _open
'_open', req)
File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open
context=self._context)
File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:581)>
I followed THIS guide if that helps.
I have read a far few articles online about how to get around this, but because I am new to all this it is beyond confusing.
If someone could give me a hand or direct me towards a beginners guide on how to work this out I would be extremely grateful!
I've had better luck with http.client, thusly...
import http.client
myDestination = "https://www.google-analytics.com/collect?v=1&tid=UA- 3458xxxx-1&cid=555&t=event&ec=doors&ea=open&el=office"
conn = http.client.HTTPSConnection(myDestination)
then use...
conn.request()
and
conn.response()
don't forget...
conn.close()
I learned how to download a picture from a certain URL with python as:
import urllib
imgurl="http://www.digimouth.com/news/media/2011/09/google-logo.jpg"
resource = urllib.urlopen(imgurl)
output = open("test.jpg","wb")
output.write(resource.read())
output.close()
and it worked well, but when i changed the URL to
imgurl="http://farm1.static.flickr.com/96/242125326_607a826afe_o.jpg"
it did not work, and gave the information
File "face_down.py", line 3, in <module>
resource = urllib2.urlopen(imgurl)
File "D:\Python27\another\Lib\urllib2.py", line 154, in urlopen
return opener.open(url, data, timeout)
File "D:\Python27\another\Lib\urllib2.py", line 431, in open
response = self._open(req, data)
File "D:\Python27\another\Lib\urllib2.py", line 449, in _open
'_open', req)
File "D:\Python27\another\Lib\urllib2.py", line 409, in _call_chain
result = func(*args)
File "D:\Python27\another\Lib\urllib2.py", line 1227, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "D:\Python27\another\Lib\urllib2.py", line 1197, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error [Errno 10060] >
and I tried to open the latter image URL, and it could be shown as the former, I have no idea to solve it~~ help~~~~
You can try using requests module. The response will be some bytes. So, you can iterate over those byte chunks and write to the file.
import requests
url = "http://farm1.static.flickr.com/96/242125326_607a826afe_o.jpg"
r = requests.get(url)
path = "filename.jpg"
with open(path, 'wb') as f:
for chunk in r:
f.write(chunk)
I looked up both of the addresses and the second one does not lead anywhere. That is probably the problem.
import urllib
imgurl="webpage url"
openimg = urllib.urlopen(imgurl) #opens image (prepares it)
img = open("test.jpg","wb") #opens the img to read it
img.write(openimg.read()) #prints it to the console
img.close() #closes img
Try the link again in your webpage and if it turns up with "webpage not available" that is probably the problem.
I'm trying to access a journal article hosted by an academic service provider (SP), using a Python script.
The server authenticates using a Shibboleth login. I read Logging into SAML/Shibboleth authenticated server using python and tried to implement a login with Python Requests.
The script starts by querying the SP for the link leading to my IDP institution, and is supposed then to authenticate automatically with the IDP. The first part works, but when following the link to the IDP, it chokes on an SSL error.
Here is what I used:
import requests
import lxml.html
LOGINLINK = 'https://www.jsave.org/action/showLogin?redirectUri=%2F'
USERAGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:28.0) Gecko/20100101 Firefox/28.0'
s = requests.session()
s.headers.update({'User-Agent' : USERAGENT})
# getting the page where you can search for your IDP
# need to get the cookies so we can continue
response = s.get(LOGINLINK)
rtext = response.text
print('Don\'t see your school?' in rtext) # prints True
# POSTing the name of my institution
data = {
'institutionName' : 'tubingen',
'submitForm' : 'Search',
'currUrl' : '%2Faction%2FshowBasicSearch',
'redirectUri' : '%2F',
'activity' : 'isearch'
}
response = s.post(BASEURL + '/action/showLogin', data=data)
rtext = response.text
print('university of tubingen' in rtext) # prints True
# get the link that leads to the IDP
tree = lxml.html.fromstring(rtext)
loginlinks = tree.cssselect('a.extLogin')
if (loginlinks):
loginlink = loginlinks[0].get('href')
else:
exit(1)
print('continuing to IDP')
response = s.get(loginlink)
rtext = response.text
print('zentrale Anmeldeseite' in rtext)
This yields:
continuing to IDP...
2014-04-04 10:04:06,010 - INFO - Starting new HTTPS connection (1): idp.uni-tuebingen.de
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 480, in urlopen
body=body, headers=headers)
File "/usr/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 285, in _make_request
conn.request(method, url, **httplib_request_kw)
File "/usr/lib/python3.4/http/client.py", line 1066, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1104, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1062, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 907, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 842, in send
self.connect()
File "/usr/lib/python3.4/site-packages/requests/packages/urllib3/connection.py", line 164, in connect
ssl_version=resolved_ssl_version)
File "/usr/lib/python3.4/site-packages/requests/packages/urllib3/util.py", line 639, in ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=server_hostname)
File "/usr/lib/python3.4/ssl.py", line 344, in wrap_socket
_context=self)
File "/usr/lib/python3.4/ssl.py", line 540, in __init__
self.do_handshake()
File "/usr/lib/python3.4/ssl.py", line 767, in do_handshake
self._sslobj.do_handshake()
ssl.SSLError: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:598)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.4/site-packages/requests/adapters.py", line 330, in send
timeout=timeout
File "/usr/lib/python3.4/site-packages/requests/packages/urllib3/connectionpool.py", line 504, in urlopen
raise SSLError(e)
requests.packages.urllib3.exceptions.SSLError: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:598)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./try.py", line 154, in <module>
response = s.get(loginlink)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 395, in get
return self.request('GET', url, **kwargs)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 383, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python3.4/site-packages/requests/sessions.py", line 486, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python3.4/site-packages/requests/adapters.py", line 385, in send
raise SSLError(e)
requests.exceptions.SSLError: [SSL: TLSV1_ALERT_INTERNAL_ERROR] tlsv1 alert internal error (_ssl.c:598)
Using s.get(loginlink, verify=False) yields exactly the same error. Simply using urllib.request.urlopen(loginlink) does so, too.
Printing and pasting the link into Firefox, on the other hand, works fine.
After trying with openssl s_client it looks like the destination idp.uni-tuebingen.de:443 is only support SSLv3 and misbehaving on anything newer. With forcing SSLv3 one gets:
$ openssl s_client -connect idp.uni-tuebingen.de:443 -ssl3
CONNECTED(00000003)
depth=3 C = DE, O = Deutsche Telekom AG, OU = T-TeleSec Trust Center, CN = Deutsche Telekom Root CA 2
...
But with default setup or forcing TLv1 (-tls1) it only returns an alert:
openssl s_client -connect idp.uni-tuebingen.de:443
CONNECTED(00000003)
140493591938752:error:14077438:SSL routines:SSL23_GET_SERVER_HELLO:tlsv1 alert internal error:s23_clnt.c:741:
So you need to find a way to force SSLv3 for this connection. I'm not familiar with the python at this point but maybe http://docs.python-requests.org/en/latest/user/advanced/ chapter "Example: Specific SSL Version" helps.
And why it works with firefox: the browsers usually retry with a downgraded SSL version if the connects with the safer versions fail. E.g. everybody is trying to work around broken stuff so that the owner of the broken stuff has no intention to fix it :(
I'm getting an error when trying to open a website url with Python 3.1, urllib & json
urllib.error.URLError:
Here's the code. The first website loads fine. The second one
import json
import urllib.request
import urllib.parse
import util
# This one works fine
response = urllib.request.urlopen('http://python.org/')
html = response.read()
print(html)
# parms - CSV filename, company, ....
p_filename = "c:\\temp\\test.csv"
jg_token = "zzzzzzzzzzzzzzzzzzzzzzzzz"
jg_proto = "https://"
jg_webst = "www.jigsaw.com/rest/"
jg_cmd_searchContact = "searchContact.json"
jg_key_companyName = "companyName"
jg_key_levels = "levels"
jg_key_departments = "departments"
jg_args = {
"token":jg_token,
jg_key_companyName: "Technical Innovations",
jg_key_departments: "HR"
}
jg_url = jg_proto + jg_webst + jg_cmd_searchContact + "?" + urllib.parse.urlencode(jg_args)
# This one generates teh error
result = json.load(urllib.request.urlopen(jg_url))
urllib.error.URLError:
File "c:\dev\xdev\PyJigsaw\searchContact.py", line 46, in
result = json.load(urllib.request.urlopen(jg_url))
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 121, in urlopen
return _opener.open(url, data, timeout)
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 349, in open
response = self._open(req, data)
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 367, in _open
'_open', req)
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 327, in _call_chain
result = func(*args)
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 1098, in https_open
return self.do_open(http.client.HTTPSConnection, req)
File "c:\dev\tdev\Python31\Lib\urllib\request.py", line 1075, in do_open
raise URLError(err)
Please edit the title and tags and maybe even the question body:
This has nothing to do with JSON and everything to do with Windows. It's also at a lower level than urllib. (Probably in the SSL code.) Distilled:
Both of the following approaches fail on Python 3.1.2 for Vista, but work fine on Linux (Python 3.1.3)
print( HTTPSConnection(hostname).request('GET',url).getresponse().read() )
print( urllib.request.urlopen('https://'+hostname+url).read() )
Change them to not use SSL, and then they work fine on Windows:
print( HTTPConnection(hostname).request('GET',url).getresponse().read() )
print( urllib.request.urlopen('http://'+hostname+url).read() )
On Vista, I just upgraded from Python 3.1.2 to Python 3.2 and this is no longer a problem.
The following now works just fine:
print( urllib.request.urlopen('https://'+hostname+url).read() )