Files
Your Name 54ccb1423f added
2021-02-08 11:17:02 +00:00

283 lines
6.2 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "kl29WUo3W4D5"
},
"outputs": [],
"source": [
"import socket\n",
"import threading\n",
"import requests\n",
"import json\n",
"from flask import Flask, jsonify, request"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 855,
"status": "ok",
"timestamp": 1574651075864,
"user": {
"displayName": "Anthony So",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mCYY-iGjUIqBSnlLoszfZTN7rU7FRNg05Rdt9Ii3A=s64",
"userId": "11809607246124237079"
},
"user_tz": -660
},
"id": "jSL6D5Q7yxNq",
"outputId": "e2f2aa1f-42b0-498a-a162-a7ab64e8eb02"
},
"outputs": [],
"source": [
"ip_address = socket.gethostbyname(socket.gethostname()) + ':8080'\n",
"ip_address"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "pvYKOG2NzIEc"
},
"outputs": [],
"source": [
"app = Flask(__name__)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "IU_Q8MYzzPdK"
},
"outputs": [],
"source": [
"@app.route(\"/\")\n",
"def welcome():\n",
" return \"Welcome to my API!\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"executionInfo": {
"elapsed": 846,
"status": "ok",
"timestamp": 1574651075866,
"user": {
"displayName": "Anthony So",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mCYY-iGjUIqBSnlLoszfZTN7rU7FRNg05Rdt9Ii3A=s64",
"userId": "11809607246124237079"
},
"user_tz": -660
},
"id": "6iLQtxEEz6vK",
"outputId": "a78173bf-b091-461d-8393-552b4bf94baf"
},
"outputs": [],
"source": [
"flask_thread = threading.Thread(target=app.run, kwargs={'host':'0.0.0.0','port':8080})\n",
"flask_thread.start()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"executionInfo": {
"elapsed": 11711,
"status": "ok",
"timestamp": 1574651090661,
"user": {
"displayName": "Anthony So",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mCYY-iGjUIqBSnlLoszfZTN7rU7FRNg05Rdt9Ii3A=s64",
"userId": "11809607246124237079"
},
"user_tz": -660
},
"id": "ooPSHPDO0C7f",
"outputId": "8855b8b7-5c44-454f-eba1-6e5230157d57"
},
"outputs": [],
"source": [
"r = requests.get(f\"http://{ip_address}\")\n",
"r.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "qrihpEDn0FRf"
},
"outputs": [],
"source": [
"@app.route('/empty', methods=['POST'])\n",
"def check_empty():\n",
" data = request.get_json()\n",
" return jsonify(not data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "h6DSUEWD3CIL"
},
"outputs": [],
"source": [
"empty_json = json.dumps([])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "DgH1hJMD3m0v"
},
"outputs": [],
"source": [
"headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"executionInfo": {
"elapsed": 821,
"status": "ok",
"timestamp": 1574651099539,
"user": {
"displayName": "Anthony So",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mCYY-iGjUIqBSnlLoszfZTN7rU7FRNg05Rdt9Ii3A=s64",
"userId": "11809607246124237079"
},
"user_tz": -660
},
"id": "TqqJA5Un32sx",
"outputId": "46ee7fe5-31c1-42c7-e8d1-6bedc673edfc"
},
"outputs": [],
"source": [
"r_empty = requests.post(f\"http://{ip_address}/empty\", data=empty_json, headers=headers)\n",
"r_empty.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "M9J-lEDx4mOe"
},
"outputs": [],
"source": [
"not_empty_json = json.dumps(['Data Science', 'is', 'so', 'cool', '!'])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 51
},
"colab_type": "code",
"executionInfo": {
"elapsed": 715,
"status": "ok",
"timestamp": 1574651101516,
"user": {
"displayName": "Anthony So",
"photoUrl": "https://lh3.googleusercontent.com/a-/AAuE7mCYY-iGjUIqBSnlLoszfZTN7rU7FRNg05Rdt9Ii3A=s64",
"userId": "11809607246124237079"
},
"user_tz": -660
},
"id": "5LFjq2ON4k78",
"outputId": "ab8fac96-e0be-4701-dd2a-fc465553c9ae"
},
"outputs": [],
"source": [
"r_not_empty = requests.post(f\"http://{ip_address}/empty\", data=not_empty_json, headers=headers)\n",
"r_not_empty.text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "Exercise18_01.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}