Skip to content

Instantly share code, notes, and snippets.

@blackandred
Created December 18, 2019 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackandred/5db62d90e57f0978d351e77b370ebc28 to your computer and use it in GitHub Desktop.
Save blackandred/5db62d90e57f0978d351e77b370ebc28 to your computer and use it in GitHub Desktop.
Experiments with HTTP requests aborting in Python & PHP
php -S 0.0.0.0:8001 ./Serve.php
sudo python ./client.py
#!/usr/bin/env python3
import pycurl
import certifi
import subprocess
from io import BytesIO
class ProcessReader:
process: subprocess.Popen
def __init__(self, process: subprocess.Popen):
self.process = process
def read_callback(self, size):
try:
self.process.wait(0)
except subprocess.TimeoutExpired:
pass
print('DOING WRITE')
if self.process.returncode is not None and self.process.returncode > 0:
raise Exception('Interrupting! The process exited early')
return self.process.stdout.read(size)
process = subprocess.Popen('/bin/bash -c "echo hehe; dmesg; sleep 1; echo duuupa; dmesg; sleep 1; exit 1"', shell=True, stdout=subprocess.PIPE)
body = BytesIO()
curl = pycurl.Curl()
curl.setopt(curl.URL, 'http://localhost:8001')
curl.setopt(curl.CAINFO, certifi.where())
curl.setopt(curl.CUSTOMREQUEST, 'POST')
curl.setopt(curl.UPLOAD, 1)
curl.setopt(curl.READFUNCTION, ProcessReader(process).read_callback)
curl.setopt(curl.WRITEFUNCTION, body.write)
curl.setopt(curl.VERBOSE, True)
curl.perform()
curl.close()
print(body.getvalue().decode('utf-8'))
<?php declare(strict_types=1);
ignore_user_abort(true);
var_dump(file_get_contents('php://input'));
sleep(10);
$f = fopen('test.log', 'wb');
fwrite($f, 'connection_aborted() = ' . var_export(connection_aborted(), true));
fclose($f);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment