Brian
Brian

Reputation: 773

log all http requests in python

from somedodgygithubproject import *
import requests

I wish to log the connection info (user agent/header etc) for every request made by somedodgygithubproject.

Is that a job for contextmanager? How can I be sure that every connection gets logged in the sub-classes etc?

Thanks

Upvotes: 3

Views: 8636

Answers (2)

silver
silver

Reputation: 386

See this answer from elsewhere on stackoverflow, if you're sure that all requests are using the requests package:

https://stackoverflow.com/a/16337639/6709958

Essentially, you just need to activate logging.

Upvotes: 4

chelmertz
chelmertz

Reputation: 20601

You shouldn't expect application code to be called in a certain way (especially not something you think is malicious), instead, you should monitor outgoing HTTP requests externally, for example via Wireshark (related question on Super User).

If you have the code to somedodgygithubproject, you would have to read all of it, and all of its dependencies, in order to see what it does. For example, it would only take a simple import os; os.system('curl https://some-site') to not get noticed by any python code.

Upvotes: 1

Related Questions