Talking to Servers#

Sometimes it can be useful to interact with servers by hand for troubleshooting or to verify a servers response to certain commands.

Talking to a web server#

You can send a GET request to a web server to receive the home page.

printf "GET / HTTP/1.0\r\n\r\n" | pync host.example.com 80

Create a text file (http_get.txt) containing the following:

1GET / HTTP/1.0
2
That’s a GET request line followed by a blank line.
The blank line tells the web server that you’re done sending the request and are now ready to receive a response.

Once you’ve created the http_get.txt file, you can then pipe it into pync’s stdin stream to receive the web page:

py -m pync -C host.example.com 80 < http_get.txt

The -C flag tells pync to replace all LF (\n) characters with a CRLF sequence (\r\n).

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

Talking to a mail server#

You could also submit emails to Simple Mail Transfer Protocol (SMTP) servers.
Suppose you have a text file (email_template.txt):
 1HELO host.example.com
 2MAIL FROM: <user@host.example.com>
 3RCPT TO: <user2@host.example.com>
 4DATA
 5From: A tester <user@host.example.com>
 6To: <user2@host.example.com>
 7Date: date
 8Subject: a test message
 9
10Body of email.
11.
12QUIT

You could then send this template to the server like so:

pync -C smtp.example.com 25 < email_template.txt
py -m pync -C smtp.example.com 25 < email_template.txt
# smtp.py
import pync
with open('email_template.txt', 'rb') as f:
    pync.run('-C smtp.example.com 25', stdin=f)
SMTP typically requires lines to be terminated with a carriage return (CR) line feed (LF) sequence (\r\n).
The -C flag tells pync to replace all LF characters (\n) with CRLF characters instead (\r\n).