python-script based HTTP clients and servers#

A Simple HTTP client#

Retrieving the home page of a website can be as simple as passing a GET request string to pync’s stdin stream:

import pync
pync.run('host.example.com 80', input=b'GET / HTTP/1.1\r\n\r\n')

Note

The response will contain HTTP headers that would need filtering out using another tool.

A Simple HTTP Server#

  1. Create a file that contains the HTTP response (index.http):

 1HTTP/1.1 200 OK
 2Server: pync
 3Content-Type: text/html; charset=UTF-8
 4
 5<!doctype html>
 6<html>
 7  <body>
 8    <h1>Hello, World!</h1>
 9  </body>
10</html>
  1. Listen for connections on port 8000 and serve the index.http file:

import platform
import pync

cat = 'cat'
if platform.system() == 'Windows':
    cat = 'type'

pync.run('-vlkc "{cat} index.http" 8000'.format(cat=cat))