Internet communication

Download all scripts: internet_communication.zip

Set up a virtual environment

https://docs.python.org/3/tutorial/venv.html

# Create a new virtual environment
$ python3 -m venv venv-COURSE

# Activate the virtual environment
# macOS or Unix
$ source venv-COURSE/bin/activate
# Windows
$ venv-COURSE\Scripts\activate.bat

open_weather_w_urllib.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
This example gets the current weather information for a city from the OpenWeather website:

	https://openweathermap.org

Steps:
	- Preparing a request
	- Requesting information over the internet
	- Parsing the response
"""

import json, ssl, urllib.parse, urllib.request  # Required for internet requests

# Preparing the request
# =====================

# The request is a URL consisting of a fixed part and a variable part.
# Both parts are described in the API documentation at https://openweathermap.org/current

# The fixed part of the URL
base_url = 'https://api.openweathermap.org/data/2.5/weather?{}'  

# The variable part of the URL is used to
#   - supply the API key
#   - choose the city
#   - choose the temperature units (°C or °F) 
#   - choose the format of the response (JSON or XML)

# The api key is required to access the API.
# It's free for up to 60 calls/minutes and can be obtained by signing up at
# https://home.openweathermap.org/users/sign_up
api_key = '1d7d325e423b2840004f4581c03639ff'

# OpenWeather uses city id's to identify locations
city_id = 2761369  # 2761369 is the id for Vienna

# The desired units of the response (Kelvin, °C or °F)
units = 'metric'  # None = Kelvin & meter/sec (default), 'metric' = celsius & meter/sec, 'imperial' = fahrenheit & miles/hour

# The desired format of the response
mode = 'json'  # 'json' = JavaScript Object Notation (= default), 'xml' = Extensible Markup Language, 'html' = Hypertext Markup Language

# Prepare the query part of the URL as dictionary
query = {
	'appid': api_key,
	'id': city_id,
	'units': units,
	'mode': mode }


# Requesting the information over the internet
# ============================================
context = ssl._create_unverified_context()  # Prepare an SSL context a 'https' requests.
urlencoded_query = urllib.parse.urlencode(query)  # Convert the dict to URL compatible text
request = urllib.request.Request(base_url.format(urlencoded_query)) # Prepare the request
response = urllib.request.urlopen(request, context=context)  # Send the request
json_data = json.loads(response.read())  # Parse the JSON (text-)data into a dict


# Parse the JSON formatted data from the response
# ===============================================
city = json_data['name']
temperature = json_data['main']['temp']
country = json_data['sys']['country'] 


# Print the result
# ----------------
print('\nThe temperature in {}, {} is {} °C.'.format(city, country, temperature))
print('\nThe URL for this request was:\n{}\n'.format(request.full_url))

"""
urllib
	A library to communicate over the internet. It's a competitor to another library: requests.
	https://docs.python.org/3/library/urllib.html


OpenWeather API for current weather conditions
	A web service provided by OpenWeather to get up-to-date weather information for any chosen location.

	https://openweathermap.org/current


City ID
	A unique number assigned to each city by OpenWeather.
	Additional IDs can be found at http://bulk.openweathermap.org/sample/
	or by searching for a city at https://openweathermap.org an getting the ID from the URL

	City           |   ID
	---------------+--------
	New York City  | 5128581
	Rio de Janeiro | 3451190
	Tokyo          | 1850147
	Vienna         | 2761369


API - Application programming interface
	A defined and documented way created by software developers so that multiple applications can communicate with each other.

URL - Uniform Resource Locator
	A standardized way to reference to a web resource (collquially also called 'web address').
	Any URL conforms to the syntax of a generic scheme:

	   scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

	https://en.wikipedia.org/wiki/URL


JSON - JavaScript Object Notation
	An open standard for human- and machine-readable documents

	https://en.wikipedia.org/wiki/JSON


XML - Extensible Markup Language
	An open standard for human- and machine-readable documents
	
	https://en.wikipedia.org/wiki/XML

HTML - Hypertext Markup Language
	An open standard for documents to be displayed using a web browser

	https://en.wikipedia.org/wiki/HTML
"""

open_weather_w_urllib3.py

Since the urrlib3 and certifi libraries are not part of the standard library they have to be installed manually e.g. from the Python Package Index (PyPI) using the pip command.

pip install urllib3
pip install certifi
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
This example gets the current weather information for a city from the OpenWeather website:

	https://openweathermap.org

Steps:
	- Preparing a request
	- Requesting information over the internet
	- Parsing the response
"""

import json, urllib3, certifi  # Required for internet requests

# Preparing the request
# =====================

# The request is a URL consisting of a fixed part and a variable part.
# Both parts are described in the API documentation at https://openweathermap.org/current

# The fixed part of the URL
base_url = 'https://api.openweathermap.org/data/2.5/weather?{}'  

# The variable part of the URL is used to
#   - supply the API key
#   - choose the city
#   - choose the temperature units (°C or °F) 
#   - choose the format of the response (JSON or XML)

# The api key is required to access the API.
# It's free for up to 60 calls/minutes and can be obtained by signing up at
# https://home.openweathermap.org/users/sign_up
api_key = '1d7d325e423b2840004f4581c03639ff'

# OpenWeather uses city id's to identify locations
city_id = 2761369  # 2761369 is the id for Vienna

# The desired units of the response (Kelvin, °C or °F)
units = 'metric'  # None = Kelvin & meter/sec (default), 'metric' = celsius & meter/sec, 'imperial' = fahrenheit & miles/hour

# The desired format of the response
mode = 'json'  # 'json' = JavaScript Object Notation (= default), 'xml' = Extensible Markup Language, 'html' = Hypertext Markup Language

# Prepare the query part of the URL as dictionary
query = {
	'appid': api_key,
	'id': city_id,
	'units': units,
	'mode': mode }


# Requesting the information over the internet
# ============================================
http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()) # Prepare the request
response = http.request('GET', base_url, fields = query)  # Send the request
json_data = json.loads(response.data.decode('utf-8'))  # Parse the JSON (text-)data into a dict


# Parse the JSON formatted data from the response
# # ===============================================
# city = json_data['name']
# temperature = json_data['main']['temp']
# country = json_data['sys']['country'] 
print(json_data)

# # Print the result
# # ----------------
# print('\nThe temperature in {}, {} is {} °C.'.format(city, country, temperature))
# # print('\nThe URL for this request was:\n{}\n'.format(request.full_url))

"""
urllib
	A library to communicate over the internet. It's a competitor to another library: requests.
	https://docs.python.org/3/library/urllib.html


OpenWeather API for current weather conditions
	A web service provided by OpenWeather to get up-to-date weather information for any chosen location.

	https://openweathermap.org/current


City ID
	A unique number assigned to each city by OpenWeather.
	Additional IDs can be found at http://bulk.openweathermap.org/sample/
	or by searching for a city at https://openweathermap.org an getting the ID from the URL

	City           |   ID
	---------------+--------
	New York City  | 5128581
	Rio de Janeiro | 3451190
	Tokyo          | 1850147
	Vienna         | 2761369


API - Application programming interface
	A defined and documented way created by software developers so that multiple applications can communicate with each other.

URL - Uniform Resource Locator
	A standardized way to reference to a web resource (collquially also called 'web address').
	Any URL conforms to the syntax of a generic scheme:

	   scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

	https://en.wikipedia.org/wiki/URL


JSON - JavaScript Object Notation
	An open standard for human- and machine-readable documents

	https://en.wikipedia.org/wiki/JSON


XML - Extensible Markup Language
	An open standard for human- and machine-readable documents
	
	https://en.wikipedia.org/wiki/XML

HTML - Hypertext Markup Language
	An open standard for documents to be displayed using a web browser

	https://en.wikipedia.org/wiki/HTML
"""

open_weather_w_requests.py

Since the requests library is not part of the standard library it has to be installed manually e.g. from the Python Package Index (PyPI) using the pip command.

pip install requests
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
This example gets the current weather information for a city from the OpenWeather website:

	https://openweathermap.org

Steps:
	- Preparing a request
	- Requesting information over the internet
	- Parsing the response
"""

import requests  # Required for internet requests

# Preparing the request
# =====================

# The request is a URL consisting of a fixed part and a variable part.
# Both parts are described in the API documentation at https://openweathermap.org/current

# The fixed part of the URL
base_url = 'https://api.openweathermap.org/data/2.5/weather?{}'  

# The variable part of the URL is used to
#   - supply the API key
#   - choose the city
#   - choose the temperature units (°C or °F) 
#   - choose the format of the response (JSON or XML)

# The api key is required to access the API.
# It's free for up to 60 calls/minutes and can be obtained by signing up at
# https://home.openweathermap.org/users/sign_up
api_key = '1d7d325e423b2840004f4581c03639ff'

# OpenWeather uses city id's to identify locations
city_id = 2761369  # 2761369 is the id for Vienna

# The desired units of the response (Kelvin, °C or °F)
units = 'metric'  # None = Kelvin & meter/sec (default), 'metric' = celsius & meter/sec, 'imperial' = fahrenheit & miles/hour

# The desired format of the response
mode = 'json'  # 'json' = JavaScript Object Notation (= default), 'xml' = Extensible Markup Language, 'html' = Hypertext Markup Language

# Prepare the query part of the URL as dictionary
query = {
	'appid': api_key,
	'id': city_id,
	'units': units,
	'mode': mode }


# Requesting the information over the internet
# ============================================
response = requests.get(base_url, params = query)  # Send the request
json_data = response.json()  # Convert the response to a dict

# Parse the JSON formatted data from the response
# ===============================================
city = json_data['name']
temperature = json_data['main']['temp']
country = json_data['sys']['country'] 


# Print the result
# ----------------
print('\nThe temperature in {}, {} is {} °C.'.format(city, country, temperature))
print('\nThe URL for this request was:\n{}\n'.format(response.url))

"""
requests
	An external library to communicate over the internet. It's a competitor to the built-in library 'urllib'.

	https://pypi.org/project/requests/
	
	Since it's an external library it needs to be installed first using the 'pip' command (short for 'pip installs packages').

	If You just want to try out an external library, it's recommended to set up a virtual environment first and use that.

	https://docs.python.org/3/tutorial/venv.html
	

OpenWeather API for current weather conditions
	A web service provided by OpenWeather to get up-to-date weather information for any chosen location.

	https://openweathermap.org/current


City ID
	A unique number assigned to each city by OpenWeather.
	Additional IDs can be found at http://bulk.openweathermap.org/sample/
	or by searching for a city at https://openweathermap.org an getting the ID from the URL

	City           |   ID
	---------------+--------
	New York City  | 5128581
	Rio de Janeiro | 3451190
	Tokyo          | 1850147
	Vienna         | 2761369


API - Application programming interface
	A defined and documented way created by software developers so that multiple applications can communicate with each other.

URL - Uniform Resource Locator
	A standardized way to reference to a web resource (collquially also called 'web address').
	Any URL conforms to the syntax of a generic scheme:

	   scheme:[//[user:password@]host[:port]][/]path[?query][#fragment]

	https://en.wikipedia.org/wiki/URL


JSON - JavaScript Object Notation
	An open standard for human- and machine-readable documents

	https://en.wikipedia.org/wiki/JSON


XML - Extensible Markup Language
	An open standard for human- and machine-readable documents
	
	https://en.wikipedia.org/wiki/XML

HTML - Hypertext Markup Language
	An open standard for documents to be displayed using a web browser

	https://en.wikipedia.org/wiki/HTML
"""