Libraries



A library.  It contains a collection of books and the occasional person and Tux.
Programming libraries are the same.  They contain a collection of functions/methods.

Disclaimers:

I am going to talk a bit about using libraries.  At this point I will skip how to build them.  Perhaps someone else would like to do that ...?

I am also not going to talk about Bash libraries - although they do exist and are mostly wierd due to the limitations of Bash functions.  Craig opinions?
Most languages come with a set of "standard" libraries and then we can add additional libraries that folks have written but are not supplied with the standard language installation.

We will do that for CURL in a bit.  But first some of the

supplied libraries

C  

https://www.tutorialspoint.com/c_standard_library/

Python

BaseHTTPServer      ast                 ihooks              select
Bastion             asynchat            imageop             sets
CDROM               asyncore            imaplib             setuptools
CGIHTTPServer       atexit              imghdr              sgmllib
Canvas              audiodev            imp                 sha
ConfigParser        audioop             importlib           shelve
Cookie              base64              imputil             shlex
DLFCN               bdb                 inspect             shutil
Dialog              binascii            io                  signal
DocXMLRPCServer     binhex              itertools           site
FileDialog          bisect              json                smtpd
FixTk               bsddb               keyword             smtplib
HTMLParser          bz2                 lib2to3             sndhdr
IN                  cPickle             linecache           socket
MimeWriter          cProfile            linuxaudiodev       spwd
Queue               cStringIO           locale              sqlite3
RPi                 calendar            logging             sre
ScrolledText        cgi                 macpath             sre_compile
SimpleDialog        cgitb               macurl2path         sre_constants
SimpleHTTPServer    chunk               mailbox             sre_parse
SimpleXMLRPCServer  cmath               mailcap             ssl
SocketServer        cmd                 markupbase          stat
StringIO            code                marshal             statvfs
TYPES               codecs              math                string
Tix                 codeop              md5                 stringold
Tkconstants         collections         mhlib               stringprep
Tkdnd               colorsys            mimetools           strop
Tkinter             commands            mimetypes           struct
UserDict            compileall          mimify              subprocess
UserList            compiler            mmap                sunau
UserString          contextlib          modulefinder        sunaudio
_LWPCookieJar       cookielib           multifile           symbol
_MozillaCookieJar   copy                multiprocessing     symtable
__builtin__         copy_reg            mutex               sys
__future__          crypt               netrc               sysconfig
_abcoll             csv                 new                 syslog
_ast                ctypes              nntplib             tabnanny
_bisect             curl                ntpath              tarfile
_codecs             curses              nturl2path          telnetlib
_codecs_cn          datetime            numbers             tempfile
_codecs_hk          dbhash              opcode              termios
_codecs_iso2022     dbm                 operator            textwrap
_codecs_jp          decimal             optparse            this
_codecs_kr          difflib             os                  thread
_codecs_tw          dircache            os2emxpath          threading
_collections        dis                 ossaudiodev         time
_csv                distutils           parser              timeit
_ctypes             dl                  pdb                 tkColorChooser
_ctypes_test        doctest             pickle              tkCommonDialog
_curses             dumbdbm             pickletools         tkFileDialog
_curses_panel       dummy_thread        pip                 tkFont
_elementtree        dummy_threading     pipes               tkMessageBox
_functools          easy_install        pkg_resources       tkSimpleDialog
_hashlib            email               pkgutil             toaiff
_heapq              encodings           platform            token
_hotshot            ensurepip           plistlib            tokenize
_io                 errno               popen2              trace
_json               exceptions          poplib              traceback
_locale             fcntl               posix               ttk
_lsprof             filecmp             posixfile           tty
_multibytecodec     fileinput           posixpath           turtle
_multiprocessing    fnmatch             pprint              types
_osx_support        formatter           profile             unicodedata
_pyio               fpformat            pstats              unittest
_random             fractions           pty                 urllib
_socket             ftplib              pwd                 urllib2
_sqlite3            functools           py_compile          urlparse
_sre                future_builtins     pyclbr              user
_ssl                gc                  pycurl              uu
_strptime           genericpath         pydoc               uuid
_struct             getopt              pydoc_data          warnings
_symtable           getpass             pyexpat             wave
_sysconfigdata      gettext             quopri              weakref
_testcapi           glob                random              webbrowser
_threading_local    grp                 re                  whichdb
_warnings           gzip                readline            wsgiref
_weakref            hashlib             repr                xdrlib
_weakrefset         heapq               resource            xml
abc                 hmac                rexec               xmllib
aifc                hotshot             rfc822              xmlrpclib
antigravity         htmlentitydefs      rlcompleter         xxsubtype
anydbm              htmllib             robotparser         zipfile
argparse            httplib             runpy               zipimport
array               idlelib             sched               zlib

php

Add on functionality, but no real system libraries - it's all included?

Bash

Bash - just add an executable and put it in the PATH

Examples - display some random numbers

C

include <stdio.h>
#include <stdlib.h>
//#include <time.h>

int main() {
// Initialize the random number generator.
//  srand(time(NULL));
//  srand (12745);
  for(int i=0; i<5; i++) {
    printf("A pseudo random number is:  %d\n", rand());
  }
}

python

import random

for i in range(5):
  print (random.randint(0,32767))  # Use the default system time for seed.

php 

note no includes or requires ...

<?php
for ($i=0; $i<5; $i++) {
  echo rand() . "\n";
}

Bash

i=0

while [ "$i" -lt 5 ]
do
    echo $RANDOM
    let "i += 1"
done



Adding a new library

We are going to install the curl library so we can learn more from Greg's presentation a while back at the netSig.

Install libcurl

$ sudo apk add libcurl
[sudo] password for deid:
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/armhf/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/armhf/APKINDEX.tar.gz
(1/4) Installing ca-certificates (20191127-r2)
(2/4) Installing nghttp2-libs (1.39.2-r0)
(3/4) Installing libssh2 (1.9.0-r1)
(4/4) Installing libcurl (7.61.1-r3)
Executing busybox-1.28.4-r3.trigger
Executing ca-certificates-20191127-r2.trigger
OK: 368 MiB in 134 packages

Install curl-dev

as we are going to be doing development (little program but still).
$ sudo apk add curl-dev
(1/5) Installing libressl-dev (2.7.5-r0)
(2/5) Installing zlib-dev (1.2.11-r1)
(3/5) Installing libssh2-dev (1.9.0-r1)
(4/5) Installing nghttp2-dev (1.39.2-r0)
(5/5) Installing curl-dev (7.61.1-r3)
Executing busybox-1.28.4-r3.trigger
OK: 385 MiB in 139 packages

C Source Code - get url

Source from: https://curl.haxx.se/libcurl/c/https.html  and modified to remove checks for certs and host name verification

/***************************************************************************
 *                                  _   _ ____  _
 *  Project                     ___| | | |  _ \| |
 *                             / __| | | | |_) | |
 *                            | (__| |_| |  _ <| |___
 *                             \___|\___/|_| \_\_____|
 *
 * Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution. The terms
 * are also available at https://curl.haxx.se/docs/copyright.html.
 *
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 * copies of the Software, and permit persons to whom the Software is
 * furnished to do so, under the terms of the COPYING file.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 * And I did modify it.  Deid
 ***************************************************************************/
/* <DESC>
 * Simple HTTP GET
 * </DESC>
 */

// Include the libraries
#include <stdio.h>
#include <curl/curl.h>

int main(void) {

// Define a couple of variables for CURL to use.
  CURL *curl;    // Pointer to "handle"
  CURLcode res;  // return code of request

// Initialize to defaults
  curl_global_init(CURL_GLOBAL_DEFAULT);

  // Get a handle to reference further operations
  curl = curl_easy_init(); //curl_easy is one of the choices.  the other is multi.

  // If we got a handle, proceed else clean up and exit.
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://drsol.com/~deid/pi/");
    // Perform the request, res will get the return code
    res = curl_easy_perform(curl);
    // If there are any errors display them
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",  curl_easy_strerror(res));

    //  We are done.  Clean up
    curl_easy_cleanup(curl);
  }
  // and a global cleanup
  curl_global_cleanup();
  return 0;
}


C source code - get header

/*  Copyright (C) 1998 - 2019, Daniel Stenberg, <daniel@haxx.se>, et al.
 * See https://curl.haxx.se/docs/copyright.html for information.
 */

// Include the libraries
#include <stdio.h>
#include <curl/curl.h>

int main(void) {

// Define a couple of variables for CURL to use.
  CURL *curl;    // Pointer to "handle"
  CURLcode res;  // return code of request

// Initialize to defaults
  curl_global_init(CURL_GLOBAL_DEFAULT);

  // Get a handle to reference further operations
  curl = curl_easy_init();  //curl_easy is one of the choices.  the other is multi.

  // If we got a handle, proceed else clean up and exit.
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_URL, "http://drsol.com/~deid/pi/");
    curl_easy_setopt(curl, CURLOPT_HEADER, 1);
    curl_easy_setopt(curl, CURLOPT_NOBODY, 1L);
    // Perform the request, res will get the return code
    res = curl_easy_perform(curl);
    // If there are any errors display them
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",  curl_easy_strerror(res));

    //  We are done.  Clean up
    curl_easy_cleanup(curl);
  }
  // and a global cleanup
  curl_global_cleanup();
  return 0;
}

Compile

$./libcurlExample

And the output

<html><head><title>Pi Makers Presentations</title>
 <link rel="stylesheet" type="text/css" href="sysadm.css">
</head>
<body>
<h1>Victoria Pi Makers and <br>Others Presentations</h1>
<ul>
<li><a href="gpio1">Flash a Led</a><br></li>
<li><a href="gpio2">Push a Button</a><br></li>
<li><a href="tempHumidity">Temperature/Humidity</a><br></li>
<li><a href="gpio-html">Control from the Web</a><br></li>
<li><a href="analogPresentation">Analog</a></li>
<li><a href="php//intro/PHP.pdf">PHP</a></li>
<li><a href="esp1">RF switches and ESP8266</a></li>
<li><a href="php/sqlite/phpSqlite.html">PHP/SQLite"</a></li>
<li><a href="Node-RED/index.html">Node-RED and sensors</a></li>
<li><a href="Node-REDII/index.html">Node-RED and sensors part II</a></li>
<li><a href="leds/index.html">PL9823 Web Controlled LEDs </a></li>
<li><a href="camera/index.html">The Pi Camera and Python></a></li>
<li><a href="oled/index.html">ESP8266 and 128x64 OLED LCD Display </a></li>
<li><a href="gitCraig/index.html">Craig's Intro. to git </a></li>
<li><a href="bashT/index.html">Intro to bash programming</a></li>
<li><a href="whatIDid/index.html">What I did last Summer (2019)</a></li>
<li><a href="nexusdrx/index.html">Ham Go Box and NEXUS DR-X)</a></li>
<li><a href="hardening/index.html">Pi Hardening</a></li>
<li><a href="summer2020/index.html">Hat</a></li>
<br>
<li><a href="programmingSessions/index.html">Programming Sessions</a></li>
<br>
<li><a href="picamp">Pi Camp</a></li>
<br>
<li><a href="Beginners">Tuesday Beginners Intro</a></li>

</ul>
</body></html>



Python - Install the library

# pip install pycurl
Collecting pycurl
  Downloading https://files.pythonhosted.org/packages/ef/05/4b773f74f830a90a326b06f9b24e65506302ab049e825a3c0b60b1a6e26a/pycurl-7.43.0.5.tar.gz (216kB)
    100% |████████████████████████████████| 225kB 3.3MB/s
Installing collected packages: pycurl
  Running setup.py install for pycurl ... done
Successfully installed pycurl-7.43.0.5

Python Source Code - get url

# Get the contents of a URL from:
# http://pycurl.io/docs/latest/quickstart.html
# with modifications by Deid Reimer

import pycurl
from StringIO import StringIO

#  Create a StringIO object buffer as a buffer for the returned data.
buffer = StringIO()

# Instantiate a curl object "c"
c = pycurl.Curl()

# set options - URL to retrieve and where to put it.
c.setopt(c.URL, 'http://drsol.com/~deid/pi/')
c.setopt(c.WRITEDATA, buffer)

# Do it and close the object
c.perform()
c.close()

# Display the returned info.
body = buffer.getvalue()
print(body.decode('iso-8859-1'))

Python Source Code - get header

# Get the header information from a URL.  With thanks to
# http://pycurl.io/docs/latest/quickstart.html
# Deid Reimer

import pycurl
from StringIO import StringIO

headers = [""]
def header_function(header_line):
    headers[0] += header_line

buffer = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'http://drsol.com/~deid/pi/')
c.setopt(c.WRITEFUNCTION, buffer.write)
# Set our header function.
c.setopt(c.HEADERFUNCTION, header_function)
c.perform()
c.close()

print (headers[0])

PHP - install the library.

# apk add php7-curl
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/main/armhf/APKINDEX.tar.gz
fetch http://dl-cdn.alpinelinux.org/alpine/v3.8/community/armhf/APKINDEX.tar.gz
(1/1) Installing php7-curl (7.2.26-r0)
OK: 385 MiB in 140 packages

php pear  ???

# apk add  php7-pear
(1/2) Installing php7-xml (7.2.26-r0)
(2/2) Installing php7-pear (7.2.26-r0)
Executing busybox-1.28.4-r3.trigger
OK: 388 MiB in 143 packages

PHP Source

<?php
$ch = curl_init("http://drsol.com/~deid/pi/");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);

if(curl_error($ch)) {
    print (curl_error($ch));
}
curl_close($ch);
?>

https://www.kite.com/python/examples/3254/pycurl-examine-response-headers



BASH - add the command.

# apk add curl
(1/1) Installing curl (7.61.1-r3)
Executing busybox-1.28.4-r3.trigger
OK: 386 MiB in 141 packages

curl http://drsol.com/~deid/pi/