Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Demonstrates Android HTTPS via urllib module #1

Merged
merged 1 commit into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# Simple Kivy App to use any HTTPs request
# author: Sirfanas

Demonstrate Android HTTPS via the `urllib` is achievable thanks to the `SSL_CERT_DIR` environment variable.
See <https://github.com/kivy/python-for-android/issues/1827>.

# Compile:
# After pulling docker Buildozer (see https://github.com/kivy/buildozer)


## Compile:
After pulling docker Buildozer (see https://github.com/kivy/buildozer)
```
docker run --interactive --tty --rm --volume ${pwd}\.buildozer\:/home/user/.buildozer --volume ${pwd}:/home/user/hostcwd --entrypoint /bin/bash kivy/buildozer
```
8 changes: 7 additions & 1 deletion buildozer.spec
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@ version = 0.1

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = certifi,openssl,python3,kivy,android
requirements =
android,
certifi,
kivy,
openssl,
python3,
requests

# (str) Custom source folders for requirements
# Sets custom source for any requirements with recipes
Expand Down
Empty file removed certifi/cacert.pem
Empty file.
60 changes: 38 additions & 22 deletions main.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,25 +1,41 @@
# coding: utf-8

import certifi
#!/usr/bin/env python3
import os

# Here's all the magic !
os.environ['SSL_CERT_FILE'] = certifi.where()

from kivy.app import App
from kivy.uix.image import AsyncImage
import ssl
import unittest
import urllib.request

# Importing an "external" file to check the conf is global to the entire app
import test


class HTTPsApp(App):

def build(self):
contents = urllib.request.urlopen("https://www.google.com/").read()
return AsyncImage(
source='https://media.makeameme.org/created/its-working-oyy433.jpg'
)

HTTPsApp().run()
import certifi
import requests
from kivy.utils import platform


class TestApp(unittest.TestCase):

URL = 'https://media.makeameme.org/created/its-working-oyy433.jpg'
EXPECTED_STATUS = 200

def test_requests(self):
"""
The requests module handles certs seamlessly by default.
"""
response = requests.get(self.URL)
self.assertEqual(response.status_code, self.EXPECTED_STATUS)

@unittest.skipUnless(platform == 'android', 'requires Android')
def test_urllib(self):
"""
On Android urllib doesn't have certs by default and would crash unless
explicitely loaded.
"""
self.assertIsNone(os.environ.get('SSL_CERT_DIR'))
with self.assertRaises(urllib.error.URLError) as e:
urllib.request.urlopen(self.URL)
self.assertEqual(
type(e.exception.args[0]), ssl.SSLCertVerificationError)
os.environ['SSL_CERT_FILE'] = certifi.where()
response = urllib.request.urlopen(self.URL)
self.assertEqual(response.status, self.EXPECTED_STATUS)


if __name__ == '__main__':
unittest.main()
7 changes: 0 additions & 7 deletions test.py

This file was deleted.