Showing posts with label network-programming. Show all posts
Showing posts with label network-programming. Show all posts

Tuesday, November 4, 2014

PDF in a Net, with Netius - the Python client

By Vasudev Ram

In a recent post, titled PDF in a Net, with Netius, a pure Python network library, I had shown how to use Netius, to serve a PDF to a client. I had shown the server side of that app and said that I would show the client side in a future post. This is that post.

Though the documentation has examples of using netius to create clients as well as servers, I chose not to use it for this example; instead I just used the requests HTTP library for Python. There was nothing in my code that required the extra features of a Netius client.

Here is my client for the Netius PDF server:

# netius_pdf_client.py

import requests

resp = requests.get('http://localhost:8080')
if resp.status_code != 200:
    print "get PDF failed"
else:
    with open('output.pdf', 'wb') as pdf_fil:
        pdf_fil.write(resp.content)
    print "PDF created in output.pdf"
As you can see, it's quite short. That's partly due to the fact that the output PDF name is hard-coded, so no need to pick it from a command-line argument, and also because I kept the code as simple as possible, for clarity.

The client uses the requests library to do an HTTP GET call to the endpoint localhost:8080 (on which the server is running).

The server then returns the PDF to the client, in a response with the Content-Type 'application/pdf' - see the server code in the previous post.

The client then writes the response content to disk, in the file 'output.pdf', with binary file mode - the 'wb' argument to open() - since the PDF is a binary file, not text).

Run the server (see the server code in the previous post linked to near the top of this post):
$ python netius_pdf_server.py
2014-11-04 22:00:45,733 [INFO] Booting netius 1.4.3 (CPython 2.7.8.final win32)
...
2014-11-04 22:00:45,795 [INFO] Serving 'WSGIServer' service on 127.0.0.1:8080 ..
.
2014-11-04 22:00:45,796 [INFO] Starting HTTP server with 'plain' encoding ...
(I renamed the server program from test_netius_wsgi_server.py (as shown in the server post) to netius_pdf_server.py.)

Then run the client in another terminal window:
$ python netius_pdf_client.py
The PDF file fetched from the server gets created on disk. Here's a screenshot of it, as shown in Foxit PDF Reader:


- Vasudev Ram - Dancing Bison Enterprises

Signup for email about new products from me.

Contact Page

Sunday, August 25, 2013

New way of writing socket servers in Linux kernel 3.9 (and in Python too)


By Vasudev Ram

A new way of writing socket servers has been introduced with the Linux kernel 3.9.

It involves the ability to bind multiple listening sockets to the same port on the same host, by unrelated processes or threads. The socket option is called SO_REUSEPORT.

Article about SO_REUSEPORT.

The above article includes a demo of how to use the feature in Python, using the example of a simple echo server and netcat.

Another article about SO_REUSEPORT on LWN.net by Michael Kerrisk.

Hacker News thread about SO_REUSEPORT.

About Michael Kerrisk. He has worked at DEC (Digital Equipment Corporation) and Google in the past.

Coincidentally, I happened to have come across Michael Kerrisk's Unix and Linux work some months earlier and had emailed him with a few questions about it, which he was kind enough to answer.

Kerrisk is the author of the book The Linux Programming Interface, which seems like an interesting and useful book.



From the web site for the The Linux Programming Interface book:

[ The Linux Programming Interface (published in October 2010, No Starch Press, ISBN 978-1-59327-220-3) is a detailed guide and reference for Linux and UNIX system programming.

With 1552 pages, 115 diagrams, 88 tables, nearly 200 example programs, and over 200 exercises, TLPI is the most comprehensive description of Linux and UNIX system programming available. The author, Michael Kerrisk, is the maintainer of the Linux man-pages project, which documents the Linux kernel and glibc APIs. He has long been active in the documentation, testing, and design review of Linux kernel-userspace interfaces. ]

And if you want to write command-line programs in Linux using C (an area closely related to the topic of the TLPI book), you may wish to check out my article on the subject, written for IBM developerWorks:

Developing a Linux command-line utility.

I have not yet tried out the SO_REUSEPORT option, because I need to get Linux kernel 3.9 first, but it seems like a useful technique for increasing performance of socket servers. Note that there are various other issues involved, so you may not get increased performance just by using this option in your code. As always with performance tuning, you have to profile your code, identify hotspots, and then only work on improving certain parts of it that seem to be the bottlenecks. And in this case, even before all that, you may need to evaluate whether this socket option is relevant to your application at all. So, caveat lector :-)


- Vasudev Ram - Dancing Bison Enterprises

Contact me

Friday, September 21, 2012

Trigger, Python networking toolkit

By Vasudev Ram


Trigger, a Python-based network toolkit, originally created by the AOL Network Security team for their own use. Now open source on GitHub.

Excerpt from the Trigger page:

[ Trigger is a robust network engineering toolkit written in Python that was designed for interfacing with network devices and managing network configuration and security policy. It increases the speed and efficiency of managing large-scale networks while reducing the risk of human error.

Started by the AOL Network Security team in 2006, Trigger was originally designed for security policy management on firewalls, routers, and switches. It has since been expanded to be a full-featured network engineering toolkit. ]

- Vasudev Ram - Dancing Bison Enterprises



Tuesday, September 4, 2012

The Spread toolkit for distributed computing

By Vasudev Ram


The Spread toolkit seems interesting. I just saw it. It is a toolkit for distributed computing. It supports C/C++, Java and Python.

Excerpts from the site:

[ Some of the services and benefits provided by Spread:

Reliable and scalable messaging and group communication.

A very powerful but simple API simplifies the construction of distributed architectures.

Easy to use, deploy and maintain.

Highly scalable from one local area network to complex wide area networks.

Supports thousands of groups with different sets of members.

Enables message reliability in the presence of machine failures, process crashes and recoveries, and network partitions and merges.

Provides a range of reliability, ordering and stability guarantees for messages.

Emphasis on robustness and high performance.

Completely distributed algorithms with no central point of failure. ]

Spread Credits (people who worked on Spread.)

The Spread license.

Spread Concepts, the company behind the Spread toolkit.

About Spread Concepts.

The Spread Concepts Team. They seem to have good backgrounds.

- Vasudev Ram - Dancing Bison Enterprises