Skip to content

Instantly share code, notes, and snippets.

@datadavev
Created March 1, 2021 03:15
Show Gist options
  • Save datadavev/120414effb6d785f5bfdfa9fc5a82eca to your computer and use it in GitHub Desktop.
Save datadavev/120414effb6d785f5bfdfa9fc5a82eca to your computer and use it in GitHub Desktop.
server caching and ld
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"id": "similar-train",
"metadata": {},
"source": [
"Two requests are sent to the same URL using technically the same Accept header, though it differs literally.\n",
"\n",
"In the first instance, a response of `application/ld+json` is received. In the second a response of `text/html` is received."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "devoted-intent",
"metadata": {},
"outputs": [],
"source": [
"import requests\n",
"\n",
"interest = ['Cache-Control', 'Content-Type', 'Content-Length', 'Location', 'X-Drupal-Cache']\n",
"\n",
"def loghistory(resp):\n",
" print(f\"Req headers: {resp.request.headers}\")\n",
" def ph(_id, _h):\n",
" print(f\"{i}: URL: {_h.url}\")\n",
" print(f\" Status: {_h.status_code}\")\n",
" print(\" headers:\")\n",
" for hk in sorted(_h.headers):\n",
" if hk in interest:\n",
" print(f\" {hk} : {_h.headers[hk]}\")\n",
"\n",
" i = 0\n",
" for h in resp.history:\n",
" ph(i, h)\n",
" i = i+1\n",
" ph(i, resp)\n",
" "
]
},
{
"cell_type": "markdown",
"id": "occupational-dealing",
"metadata": {},
"source": [
"Send a request asking specifically for a JSON-LD response:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "planned-method",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Req headers: {'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/ld+json', 'Connection': 'keep-alive'}\n",
"0: URL: https://www.bco-dmo.org/dataset/3160\n",
" Status: 301\n",
" headers:\n",
" Cache-Control : no-cache, must-revalidate\n",
" Content-Length : 20\n",
" Content-Type : text/html; charset=UTF-8\n",
" Location : https://www.bco-dmo.org/node/3160.jsonld\n",
" X-Drupal-Cache : MISS\n",
"1: URL: https://www.bco-dmo.org/node/3160.jsonld\n",
" Status: 301\n",
" headers:\n",
" Cache-Control : no-cache, must-revalidate\n",
" Content-Length : 20\n",
" Content-Type : text/html; charset=UTF-8\n",
" Location : https://www.bco-dmo.org/node/3160/rdf/jsonld\n",
" X-Drupal-Cache : MISS\n",
"2: URL: https://www.bco-dmo.org/node/3160/rdf/jsonld\n",
" Status: 200\n",
" headers:\n",
" Cache-Control : public, max-age=900\n",
" Content-Length : 2635\n",
" Content-Type : application/ld+json\n",
" X-Drupal-Cache : MISS\n",
"{\"@context\":{\"content\":\"http:\\/\\/purl.org\\/rss\\/1.0\\/modules\\/content\\/\",\"dc\":\"http:\\/\\/purl.org\\/dc\\/terms\\/\",\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"og\":\"http:\\/\\/ogp.me\\/ns#\",\"rdfs\":\"http:\\/\\/www.w3.org\\/2000\\/01\\/rdf-schema#\",\"sioc\":\"http:\\/\\/rdfs.org\\/sioc\\/ns#\",\"sioct\":\"http:\\/\\/rdfs.org\\/sioc\\/types#\",\"skos\":\"http:\\/\\/www.w3.org\\/2004\\/02\\/skos\\/core#\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\",\"owl\":\"http:\\/\\/www.w3.org\\/2002\\/07\\/owl#\",\"rdf\":\"http:\\/\\/www.w3.org\\/1999\\/02\\/22-rdf-syntax-ns#\",\"rss\":\"http:\\/\\/purl.org\\/rss\\/1.0\\/\",\"site\":\"https:\\/\\/www.bco-dmo.org\\/ns#\",\"odo\":\"http:\\/\\/ocean-data.org\\/schema\\/\",\"emo\":\"http:\\/\\/ocean-data.org\\/schema\\/entity-matching#\",\"bibo\":\"http:\\/\\/purl.org\\/ontology\\/bibo\\/\",\"crypto\":\"http:\\/\\/id.loc.gov\\/vocabulary\\/preservation\\/cryptographicHashFunctions\\/\",\"bcodmo\":\"http:\\/\\/lod.bco-dmo.org\\/id\\/\",\"arpfo\":\"http:\\/\\/vocab.ox.ac.uk\\/projectfunding#\",\"tw\":\"http:\\/\\/tw.rpi.edu\\/schema\\/\",\"dcat\":\"http:\\/\\/www.w3.org\\/ns\\/dcat#\",\"time\":\"http:\\/\\/www.w3.org\\/2006\\/time#\",\"geo\":\"http:\\/\\/www.w3.org\\/2003\\/01\\/geo\\/wgs84_pos#\",\"geosparql\":\"http:\\/\\/www.opengis.net\\/ont\\/geosparql#\",\"sf\":\"http:\\/\\/www.opengis.net\\/ont\\/sf#\",\"void\":\"http:\\/\\/rdfs.org\\/ns\\/void#\",\"sd\":\"http:\\/\\/www.w3.org\\/ns\\/sparql-service-description#\",\"dctype\":\"http:\\/\\/purl.org\\/dc\\/dcmitype\\/\",\"prov\":\"http:\\/\\/www.w3.org\\/ns\\/prov#\",\"schema\":\"http:\\/\\/schema.org\\/\",\"geolink\":\"http:\\/\\/schema.geolink.org\\/1.0\\/base\\/main#\"},\"@id\":\"http:\\/\\/lod.bco-dmo.org\\/id\\/dataset\\/3160#graph\",\"@graph\":[{\"http:\\/\\/lod.bco-dmo.org\\/id\\/dataset\\/3160\":{\"@id\":\"http:\\/\\/lod.bco-dmo.org\\/id\\/dataset\\/3160\",\"@type\":[\"http:\\/\\/ocean-data.org\\/schema\\/DeploymentDatasetCollection\",\"http:\\/\\/www.w3.org\\/ns\\/dcat#Dataset\",\"http:\\/\\/www.w3.org\\/ns\\/prov#Entity\",\"http:\\/\\/ocean-data.org\\/schema\\/Dataset\"],\"http:\\/\\/ocean-data.org\\/schema\\/hasAcquisitionDescription\":[{\"@value\":\"<div xmlns=\\\"http:\\/\\/www.w3.org\\/1999\\/xhtml\\\" lang=\\\"en\\\"><p>Generated by BCO-DMO staff from project \n"
]
}
],
"source": [
"url = \"https://www.bco-dmo.org/dataset/3160\"\n",
"headers = {\"Accept\":\"application/ld+json\"}\n",
"response1 = requests.get(url, headers=headers, timeout=30)\n",
"loghistory(response1)\n",
"#print the first 2k chars\n",
"print(response1.text[:2000])"
]
},
{
"cell_type": "markdown",
"id": "concrete-illness",
"metadata": {},
"source": [
"Send another request to the same URL requesting the same, except using prioritization of formats in the Accept header:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "constitutional-heaven",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Req headers: {'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/ld+json;q=1.0,text/html;q=0.8', 'Connection': 'keep-alive'}\n",
"0: URL: https://www.bco-dmo.org/dataset/3160\n",
" Status: 200\n",
" headers:\n",
" Cache-Control : public, max-age=900\n",
" Content-Type : text/html; charset=utf-8\n",
" X-Drupal-Cache : MISS\n",
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML+RDFa 1.0//EN\"\n",
" \"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd\">\n",
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" version=\"XHTML+RDFa 1.0\" dir=\"ltr\"\n",
" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n",
" xmlns:dc=\"http://purl.org/dc/terms/\"\n",
" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\"\n",
" xmlns:og=\"http://ogp.me/ns#\"\n",
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n",
" xmlns:sioc=\"http://rdfs.org/sioc/ns#\"\n",
" xmlns:sioct=\"http://rdfs.org/sioc/types#\"\n",
" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\"\n",
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n",
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n",
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n",
" xmlns:rss=\"http://purl.org/rss/1.0/\"\n",
" xmlns:site=\"https://www.bco-dmo.org/ns#\"\n",
" xmlns:odo=\"http://ocean-data.org/schema/\"\n",
" xmlns:emo=\"http://ocean-data.org/schema/entity-matching#\"\n",
" xmlns:bibo=\"http://purl.org/ontology/bibo/\"\n",
" xmlns:crypto=\"http://id.loc.gov/vocabulary/preservation/cryptographicHashFunctions/\"\n",
" xmlns:bcodmo=\"http://lod.bco-dmo.org/id/\"\n",
" xmlns:arpfo=\"http://vocab.ox.ac.uk/projectfunding#\"\n",
" xmlns:tw=\"http://tw.rpi.edu/schema/\"\n",
" xmlns:dcat=\"http://www.w3.org/ns/dcat#\"\n",
" xmlns:time=\"http://www.w3.org/2006/time#\"\n",
" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"\n",
" xmlns:geosparql=\"http://www.opengis.net/ont/geosparql#\"\n",
" xmlns:sf=\"http://www.opengis.net/ont/sf#\"\n",
" xmlns:void=\"http://rdfs.org/ns/void#\"\n",
" xmlns:sd=\"http://www.w3.org/ns/sparql-service-description#\"\n",
" xmlns:dctype=\"http://purl.org/dc/dcmitype/\"\n",
" xmlns:prov=\"http://www.w3.org/ns/prov#\"\n",
" xmlns:schema=\"http://schema.org/\"\n",
" xmlns:geolink=\"http://schema.geolink.org/1.0/base/main#\">\n",
"\n",
"<head profile=\"http://www.w3.org/1999/xhtml/vocab\">\n",
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n",
"<meta name=\"Generator\" content=\"Drupal 7 (http://drupal.org)\" />\n",
"<link rel=\"canonical\" href=\"https://www.bco-dmo.org/dataset/3160\" />\n",
"<link rel=\"shortlink\" href=\"/node/3160\" />\n",
"<scr\n"
]
}
],
"source": [
"url = \"https://www.bco-dmo.org/dataset/3160\"\n",
"headers = {\"Accept\":\"application/ld+json;q=1.0,text/html;q=0.8\"}\n",
"response2 = requests.get(url, headers=headers, timeout=30)\n",
"loghistory(response2)\n",
"#print the first 2k chars\n",
"print(response2.text[:2000])"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "sensitive-parker",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Req headers: {'User-Agent': 'python-requests/2.25.1', 'Accept-Encoding': 'gzip, deflate', 'Accept': 'application/ld+json', 'Connection': 'keep-alive'}\n",
"0: URL: https://www.bco-dmo.org/dataset/3160\n",
" Status: 200\n",
" headers:\n",
" Cache-Control : public, max-age=900\n",
" Content-Type : text/html; charset=utf-8\n",
" X-Drupal-Cache : HIT\n",
"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML+RDFa 1.0//EN\"\n",
" \"http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd\">\n",
"<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" version=\"XHTML+RDFa 1.0\" dir=\"ltr\"\n",
" xmlns:content=\"http://purl.org/rss/1.0/modules/content/\"\n",
" xmlns:dc=\"http://purl.org/dc/terms/\"\n",
" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\"\n",
" xmlns:og=\"http://ogp.me/ns#\"\n",
" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n",
" xmlns:sioc=\"http://rdfs.org/sioc/ns#\"\n",
" xmlns:sioct=\"http://rdfs.org/sioc/types#\"\n",
" xmlns:skos=\"http://www.w3.org/2004/02/skos/core#\"\n",
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema#\"\n",
" xmlns:owl=\"http://www.w3.org/2002/07/owl#\"\n",
" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n",
" xmlns:rss=\"http://purl.org/rss/1.0/\"\n",
" xmlns:site=\"https://www.bco-dmo.org/ns#\"\n",
" xmlns:odo=\"http://ocean-data.org/schema/\"\n",
" xmlns:emo=\"http://ocean-data.org/schema/entity-matching#\"\n",
" xmlns:bibo=\"http://purl.org/ontology/bibo/\"\n",
" xmlns:crypto=\"http://id.loc.gov/vocabulary/preservation/cryptographicHashFunctions/\"\n",
" xmlns:bcodmo=\"http://lod.bco-dmo.org/id/\"\n",
" xmlns:arpfo=\"http://vocab.ox.ac.uk/projectfunding#\"\n",
" xmlns:tw=\"http://tw.rpi.edu/schema/\"\n",
" xmlns:dcat=\"http://www.w3.org/ns/dcat#\"\n",
" xmlns:time=\"http://www.w3.org/2006/time#\"\n",
" xmlns:geo=\"http://www.w3.org/2003/01/geo/wgs84_pos#\"\n",
" xmlns:geosparql=\"http://www.opengis.net/ont/geosparql#\"\n",
" xmlns:sf=\"http://www.opengis.net/ont/sf#\"\n",
" xmlns:void=\"http://rdfs.org/ns/void#\"\n",
" xmlns:sd=\"http://www.w3.org/ns/sparql-service-description#\"\n",
" xmlns:dctype=\"http://purl.org/dc/dcmitype/\"\n",
" xmlns:prov=\"http://www.w3.org/ns/prov#\"\n",
" xmlns:schema=\"http://schema.org/\"\n",
" xmlns:geolink=\"http://schema.geolink.org/1.0/base/main#\">\n",
"\n",
"<head profile=\"http://www.w3.org/1999/xhtml/vocab\">\n",
" <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n",
"<meta name=\"Generator\" content=\"Drupal 7 (http://drupal.org)\" />\n",
"<link rel=\"canonical\" href=\"https://www.bco-dmo.org/dataset/3160\" />\n",
"<link rel=\"shortlink\" href=\"/node/3160\" />\n",
"<scr\n"
]
}
],
"source": [
"# Now ask for the original again\n",
"url = \"https://www.bco-dmo.org/dataset/3160\"\n",
"headers = {\"Accept\":\"application/ld+json\"}\n",
"response3 = requests.get(url, headers=headers, timeout=30)\n",
"loghistory(response3)\n",
"#print the first 2k chars\n",
"print(response3.text[:2000])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "available-residence",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"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.9.1"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment