Sending Avro file via HTTP

It is possible to send an AVRO file via HTTP. The idea is that one sets up a server process. Once the server process runs, a client call is made.
I found a neat scheme how such process works.

We see on the server side, a socket must be set up. This set-up must also be undertaken with an indication which type of network is utilized. The most common network in the IP4 system: 4 numbers, indicating the position in a network. This is indicated with “AF_INET”. Once the socket is set up, it must be linked to an (internal) IP address. This link is indicated with the port. After that the server can be instructed to start listening to a particular port.
The system will be instructed to receive AVRO files. Therefore the avro modules are imported.

One is then able to see how the server reacts upon the client call. We use Python3 to work this out. The server process runs as;

import avro.datafile
import avro.io
import io
import socket

def handle_client(connection, address):
    data = connection.recv(1024)
    message_buf = io.BytesIO(data)
    reader = avro.datafile.DataFileReader(message_buf, avro.io.DatumReader())
    for thing in reader:
        print(thing)
    reader.close()

def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(('van-maanen.com', 54321))
    sock.listen(10)

    while True:
        conn, addr = sock.accept()
        handle_client(conn, addr)
        conn.close()

if __name__ == '__main__':
    main()

A more extended programme can be seen here:

The client programme then looks like:

import io
import json
import socket
import avro.datafile
import avro.schema
import avro.io
import avro.ipc

SCHEMA = avro.schema.Parse(json.dumps({
 "namespace"    : "example.avro",
 "type"         : "record",
 "name"         : "User",
 "fields"       : [
     {"name": "name"            , "type": "string"},
     {"name": "favorite_number" , "type": ["int", "null"]},
     {"name": "favorite_color"  , "type": ["string", "null"]}
 ]
}))

def send_message(connection, message):
    buf = io.BytesIO()
    writer = avro.datafile.DataFileWriter(buf, avro.io.DatumWriter(), SCHEMA)
    writer.append(message)
    writer.flush()
    buf.seek(0)
    data = buf.read()
    connection.send(data)

def main():
    connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    connection.connect(('192.168.2.25', 54321))
    send_message(connection, {'name': 'Eli', 'favorite_number': 42, 'favorite_color': 'black'})

if __name__ == '__main__':
    main()

A more extended programme can be seen here:

Another elaborate programme can be seen here:

Door tom