Skip to content

Instantly share code, notes, and snippets.

@Sean-Bradley
Created April 19, 2019 14:21
Show Gist options
  • Save Sean-Bradley/e4e57324fa05a6defde3cbd7e70220bb to your computer and use it in GitHub Desktop.
Save Sean-Bradley/e4e57324fa05a6defde3cbd7e70220bb to your computer and use it in GitHub Desktop.
import requests
import psycopg2
with requests.get("http://127.0.0.1:5000/very_large_request/100000000", stream=True) as r:
conn = psycopg2.connect(dbname="stream_test",
user="postgres", password="postgres")
cur = conn.cursor()
sql = "INSERT INTO transactions (txid, uid, amount) VALUES (%s, %s, %s)"
buffer = ""
for chunk in r.iter_content(chunk_size=1):
if chunk.endswith(b'\n'):
t = eval(buffer)
print(t)
cur.execute(sql, (t[0], t[1], t[2]))
conn.commit()
buffer = ""
else:
buffer += chunk.decode()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment