<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shivam Mitra on Medium]]></title>
        <description><![CDATA[Stories by Shivam Mitra on Medium]]></description>
        <link>https://medium.com/@shivammitra?source=rss-f9098a5a8bcc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Uza_p3y7B954XIfGdIJ0eA.jpeg</url>
            <title>Stories by Shivam Mitra on Medium</title>
            <link>https://medium.com/@shivammitra?source=rss-f9098a5a8bcc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 23:51:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shivammitra/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Part 1: Exploring Networking APIs: TCP, HTTP and RPC]]></title>
            <link>https://medium.com/@shivammitra/part-1-exploring-network-communication-from-local-functions-to-tcp-http-and-rpc-93ee5c27a2df?source=rss-f9098a5a8bcc------2</link>
            <guid isPermaLink="false">https://medium.com/p/93ee5c27a2df</guid>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[tcp]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Shivam Mitra]]></dc:creator>
            <pubDate>Sun, 18 Feb 2024 18:05:30 GMT</pubDate>
            <atom:updated>2024-02-20T09:26:49.719Z</atom:updated>
            <content:encoded><![CDATA[<p>In the vast realm of computer networking, communication between applications is facilitated through a variety of protocols and interfaces. Among these, Transmission Control Protocol (TCP), Hypertext Transfer Protocol (HTTP), and Remote Procedure Call (RPC) stand as pillars, enabling seamless data exchange over networks. Understanding these networking APIs is crucial for developers striving to build robust, scalable, and efficient distributed systems</p><p>In this blog, we’ll start by talking about local functions. Then, we’ll explore how things change when we need to communicate over network. We’ll learn about TCP, a way for computers to talk to each other reliably, and HTTP, the language of the web and REST APIs which are built on top of HTTP. In the next blog, we will focus on remote procedure calls(RPC) and how it is different than REST.</p><p>All the code examples discussed in this blog are written in Python and are available on GitHub. You can clone the repository to your local system and run the examples yourself. Additionally, feel free to give the repository a star if you found the examples helpful or contribute to the codebase by submitting pull requests with improvements or additional examples.</p><p>Repository Link: <a href="https://github.com/codophobia/rest-vs-rpc">https://github.com/codophobia/rest-vs-rpc</a></p><p>If you prefer video content, we’ve got you covered! We’ve created a YouTube video that complements this blog post, offering visual explanations and demonstrations of the concepts discussed here.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FyLEl_FdvP3E%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DyLEl_FdvP3E&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FyLEl_FdvP3E%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/9dba646961a3a2966685c55496355260/href">https://medium.com/media/9dba646961a3a2966685c55496355260/href</a></iframe><h4>Local Functions</h4><p>Local function calls refer to the invocation of functions within the same program or process without involving any network communication. In simpler terms, when you have a function defined in your code and you call it directly within the same program or process, it’s considered a local function call.</p><p>Here’s a basic example in Python which uses a function to add two numbers:</p><pre>def add(num1, num2):<br>    return num1 + num2<br><br><br>if __name__ == &quot;__main__&quot;:<br>    print(add(5, 7))</pre><p>Local function calls are typically fast and efficient since they don’t incur the overhead associated with network communication. They are commonly used for organizing code into reusable and modular components within a single application.</p><h4>Communication over network using TCP sockets</h4><p>Imagine you’re faced with a simple yet intriguing task: finding the sum of two numbers, but with a twist. The code to perform this calculation resides on a remote server, and you need to connect to it to get the result. How would you tackle this challenge?</p><p>One of the basic ways to implement networking calls for a service that returns the sum of two numbers is by using TCP sockets. Implementing networking calls using TCP sockets is indeed one of the fundamental concepts in computer networking.</p><p>Below is a basic implementation of a TCP server in Python that listens on port number 12345 for client requests, parses the client requests into numbers, and returns the sum of two numbers to the client.</p><pre>import socket<br><br><br>def add_numbers(num1, num2):<br>    return num1 + num2<br><br><br>def start_server():<br>    # Create a socket object<br>    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br><br>    # Define the host and port<br>    host = &#39;localhost&#39;<br>    port = 12345<br><br>    # Bind the socket to the host and port<br>    server_socket.bind((host, port))<br><br>    # Listen for incoming connections<br>    server_socket.listen(1)<br>    print(f&#39;Server listening on {host}:{port}&#39;)<br><br>    while True:<br>        # Accept a client connection<br>        client_socket, addr = server_socket.accept()<br>        print(&#39;Connected to client:&#39;, addr)<br><br>        # Receive the numbers from the client<br>        data = client_socket.recv(1024).decode()<br>        num1, num2 = map(int, data.split(&#39;,&#39;))<br><br>        # Add the numbers<br>        result = add_numbers(num1, num2)<br><br>        # Send the result back to the client<br>        client_socket.send(str(result).encode())<br><br>        # Close the client connection<br>        client_socket.close()<br><br><br>if __name__ == &#39;__main__&#39;:<br>    start_server()</pre><pre>// Start the server<br>$ python socket/server.py<br>Server listening on localhost:12345</pre><p>Similarly, we can write a python client which connects with the server and requests the addition of two numbers.</p><pre>import socket<br><br><br>def add_numbers(a, b):<br>    # Connect to the server<br>    server_address = (&#39;localhost&#39;, 12345)<br>    client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br>    client_socket.connect(server_address)<br><br>    # Send the numbers to the server<br>    message = f&quot;{a},{b}&quot;<br>    client_socket.sendall(message.encode())<br><br>    # Receive the result from the server<br>    result = client_socket.recv(1024).decode()<br><br>    # Close the connection<br>    client_socket.close()<br><br>    return int(result)<br><br><br>if __name__ == &#39;__main__&#39;:<br>    # Test the client<br>    num1 = 5<br>    num2 = 7<br>    print(f&quot;Request: {num1} + {num2}&quot;)<br>    output = add_numbers(5, 7)<br>    print(f&quot;Result: {output}&quot;)</pre><pre>// Run the client<br>(venv) $ python socket/python_client.py<br>Request: 5 + 7<br>Result: 12</pre><p>We can also use nc command in Linux to send the request.</p><pre>(venv) $ nc 127.0.0.1 12345<br>5,7<br>12%</pre><h4>Steps involved in communicating over network</h4><p>Function calls over a network using low-level networking typically involve establishing a connection between client and server, serializing function parameters, sending them over the network, executing the function on the server, and then returning the result to the client. Here’s a simplified overview of how it works:</p><ol><li><strong>Client Side (Request)</strong>: The client prepares the function call by serializing the function name and its parameters into a format suitable for transmission over the network. It establishes a connection with the server using a network protocol such as TCP/IP or UDP. The serialized function call is sent over the network to the server.</li><li><strong>Server Side (Processing)</strong>: The server receives the function call over the network. It deserializes the function name and parameters. The server executes the function locally, using the provided parameters. After execution, the server serializes the result (if any) and sends it back to the client.</li><li><strong>Client Side (Response)</strong>: The client receives the response from the server over the network. It deserializes the response and processes the result. The client can then use the result of the function call as needed.</li><li><strong>Error Handling</strong>: Both client and server need to implement error handling mechanisms to deal with network failures, timeouts, or errors during function execution.</li></ol><pre>     Client                                    Server<br>  +---------+                              +---------+<br>  |         |    Establish Connection      |         |<br>  |         |   ------------------------&gt;  |         |<br>  |         |                              |         |<br>  |         |    Serialize &amp; Send Request  |         |<br>  |         |   ------------------------&gt;  |         |<br>  |         |                              |         |<br>  |         |      Deserialize &amp; Process   |         |<br>  |         |   ------------------------   |         |<br>  |         |                              |         |<br>  |         |    Serialize &amp; Send Response |         |<br>  |         |   &lt;------------------------  |         |<br>  |         |                              |         |<br>  |         |      Deserialize &amp; Process   |         |<br>  |         |   ------------------------   |         |<br>  +---------+                              +---------+</pre><h4>Difference between local function calls and network calls</h4><ul><li><strong>Location of Execution:</strong> Local function calls execute within the same process or application, while remote function calls execute on separate systems or processes.</li><li><strong>Reliability</strong>: Local function calls are reliable, while network requests are unpredictable due to potential issues like network problems or slow remote servers. Because network problems are common, it’s important to be ready for them, like retrying a request if it doesn’t work the first time.</li><li><strong>Serialization/Deserilzation</strong>: When you call a local function, you can easily pass it references (pointers) to objects in local memory. However, when making a network request, all parameters must be encoded into bytes for transmission. The remote server then needs to deserialize the data.</li><li><strong>Datatype translation</strong>: The client and the service may be implemented in different programming languages, so the communication framework must translate datatypes from one language to another. This can become tricky if two languages don’t have the same data type.</li></ul><h4>Communication over network using HTTP protocol</h4><p>Pure TCP sockets offer advantages in communication, including ease of implementation and high-speed transmission for simple data types like numbers and strings but they do suffer from multiple limitations:</p><ol><li><strong>Low-Level Abstraction</strong>: TCP sockets provide a low-level abstraction for communication, requiring developers to handle details such as connection establishment, packetization, and error handling manually. This can result in more complex code and increased development effort.</li><li><strong>Data Serialization</strong>: With TCP sockets, developers are responsible for serializing and deserializing data into a format suitable for transmission over the network. This process can be error-prone and may require additional code to handle different data types and structures</li><li><strong>Lack of standardization</strong>: TCP sockets lack a standardized message format or protocol for communication.</li></ol><p>HTTP, being a higher-level protocol built on top of TCP, can overcome some of the disadvantages of pure TCP communication:</p><ol><li><strong>Abstraction</strong>: HTTP abstracts away low-level details of TCP communication, such as connection management and data serialization, making it easier to work with. Developers can focus on application logic rather than dealing with network-level intricacies.</li><li><strong>Standardization</strong>: HTTP defines a standardized message format and protocol for communication, including methods (GET, POST, PUT, DELETE), headers, and status codes. This standardization ensures interoperability between different implementations and simplifies integration between systems.</li><li><strong>Compatibility</strong>: HTTP’s standardized format and protocol facilitate compatibility among different systems and platforms. Clients and servers can communicate seamlessly regardless of their underlying technology stack or programming language.</li></ol><p>HTTP, or Hypertext Transfer Protocol, is a protocol used for transmitting data over the internet. It’s the foundation of data communication on the World Wide Web. HTTP follows a client-server model where a client (such as a web browser or an application) sends requests to a server (such as a web server), and the server responds with the requested resources (such as web pages or files). You can read more about HTTP protocol <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview">here</a>.</p><h4>Code example of using HTTP protocol</h4><p>Let’s try to understand HTTP protocol with some coding examples in Python. We will be using the Flask web framework for examples.</p><p>Flask is a lightweight and flexible web framework for Python that provides tools and libraries for building web applications. When you use Flask to build a web application, you typically create routes that handle HTTP requests and return responses to clients.</p><p>Let’s implement a simple HTTP server using Flask which accepts a request containing two numbers and returns the sum of them.</p><pre>from flask import Flask, request<br><br>app = Flask(__name__)<br><br>// This is the function that will be called when a GET request is made to the 127.0.0.1:5000/add route<br>@app.route(&#39;/add&#39;, methods=[&#39;GET&#39;])<br>def add_numbers():<br>    num1 = int(request.args.get(&#39;num1&#39;))<br>    num2 = int(request.args.get(&#39;num2&#39;))<br>    result = num1 + num2<br>    return {&#39;result&#39;: result}<br><br><br>if __name__ == &#39;__main__&#39;:<br>    app.run()</pre><pre>// Start the server<br>$ python rest/server.py<br> * Serving Flask app &#39;server&#39;<br> * Debug mode: off<br>WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.<br> * Running on http://127.0.0.1:5000<br>Press CTRL+C to quit</pre><p>Let’s write a python client which will send a request to the HTTP server.</p><pre>import requests<br><br><br>def add_numbers(a, b):<br>    // Url where the request will be made<br>    url = &#39;http://localhost:5000/add&#39;<br>    try:<br>        // Json is the most common message format used in HTTP communication<br>        payload = {&#39;num1&#39;: a, &#39;num2&#39;: b}<br>        print(&quot;Request:&quot;, payload[&#39;num1&#39;], &quot;+&quot;, payload[&#39;num2&#39;])<br>        // requests module abstracts away low-level details of TCP communication <br>        response = requests.get(url, params=payload)<br>        <br>        // Check response code of the response<br>        if response.status_code != 200:<br>            print(&quot;Error: Unexpected response code:&quot;, response.status_code)<br>            return None<br>        <br>        result = response.json()<br>        return result[&#39;result&#39;]<br>    except requests.exceptions.RequestException as e:<br>        print(&quot;Error: &quot;, e)<br>        return None<br><br><br>if __name__ == &#39;__main__&#39;:<br>    result = add_numbers(5, 3)<br>    print(&quot;Result:&quot;, result)</pre><pre>// Run the client.<br>(venv) $ python rest/python_client.py<br>Request: 5 + 3<br>Result: 8</pre><p>You will notice that the above client-server implementation abstracts lot of lower level details and use of standard message formats(json) and protocol(GET method, response codes).</p><p>We can also use <a href="https://blog.hubspot.com/website/curl-command#:~:text=Client%20URL%20(cURL%2C%20pronounced%20%E2%80%9C,send%20to%20that%20server%20URL.">curl</a> and <a href="https://www.tecmint.com/netcat-nc-command-examples/">nc</a> command to make the request to the HTTP server.</p><pre>(venv) $ curl -i -X GET &quot;http://localhost:5000/add?num1=3&amp;num2=5&quot;<br>HTTP/1.1 200 OK<br>Server: Werkzeug/3.0.1 Python/3.8.9<br>Date: Sun, 18 Feb 2024 16:43:40 GMT<br>Content-Type: application/json<br>Content-Length: 13<br>Connection: close</pre><pre>(venv) $ nc 127.0.0.1 5000<br>GET /add?num1=3&amp;num2=5 HTTP/1.1<br><br>HTTP/1.1 200 OK<br>Server: Werkzeug/3.0.1 Python/3.8.9<br>Date: Sun, 18 Feb 2024 16:41:54 GMT<br>Content-Type: application/json<br>Content-Length: 13<br>Connection: close<br><br>{&quot;result&quot;:8}</pre><p>There are many other tools like POSTMAN which can be used as a client and these tools make it easy to debug any issues between client and the server.</p><h4>REST APIs</h4><p>Let’s first understand what API means.</p><p>API stands for Application Programming Interface. It is a set of <strong>rules and protocols</strong> that allows different software applications to communicate with each other. APIs define the <strong>methods</strong> and <strong>data formats</strong> that applications can use to request and exchange information.</p><p>REST (Representational State Transfer) API is an architectural style for designing networked applications built over HTTP protocol. Let’s have a look at the protocols, methods and data formats used in REST APIs.</p><ol><li><strong>Simple Data Formats</strong>: RESTful APIs typically use simple and widely-supported data formats such as JSON (JavaScript Object Notation) or XML (eXtensible Markup Language) for representing resources. These formats are easy to parse and understand, making them ideal for exchanging data between clients and servers.</li><li><strong>URLs for Resource Identification</strong>: Resources in a RESTful API are identified by unique URLs (Uniform Resource Locators) or URIs (Uniform Resource Identifiers). Each resource should have its own URL, which clients can use to interact with it using standard HTTP methods.</li><li><strong>HTTP Methods</strong>: RESTful APIs leverage standard HTTP methods (GET, POST, PUT, DELETE, PATCH, etc.) to perform operations on resources. Each HTTP method corresponds to a specific action on the resource, such as retrieving, creating, updating, or deleting it.</li><li><strong>HTTP Features for Cache Control</strong>: RESTful APIs make use of HTTP caching mechanisms to improve performance and reduce server load. By setting appropriate cache control headers (such as Cache-Control and Expires), servers can instruct clients to cache responses and serve them from the cache instead of making repeated requests.</li></ol><p>The examples which we have used for HTTP protocol uses REST design principles to communicate over the network.</p><h4>Conclusion</h4><p>I hope that you are now good with basics of network communications and how TCP and HTTP protocol are used in network communications. In the next blog, we will focus on remote procedure calls(RPC) which is another popular network communication protocol.</p><h4>References</h4><ul><li><a href="https://www.amazon.co.uk/Designing-Data-Intensive-Applications-Reliable-Maintainable/dp/1449373321">https://www.amazon.co.uk/Designing-Data-Intensive-Applications-Reliable-Maintainable/dp/1449373321</a></li><li><a href="https://en.wikipedia.org/wiki/REST">https://en.wikipedia.org/wiki/REST</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93ee5c27a2df" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>