Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log all http requests in python [duplicate]

from dodgygithubproject import *
import requests

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

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

like image 266
Brian Avatar asked Jan 01 '26 03:01

Brian


2 Answers

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.

like image 170
silver Avatar answered Jan 02 '26 19:01

silver


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.

like image 26
chelmertz Avatar answered Jan 02 '26 17:01

chelmertz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!