1
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

Brian
  • 677
  • 2
  • 10
  • 22

2 Answers2

2

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.

silver
  • 316
  • 1
  • 5
1

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.

chelmertz
  • 19,026
  • 4
  • 40
  • 45
  • 1
    Thanks @chelmertz I have wireshark, however I was hoping to do something in python. Suppose that it's not actually a dodgy project, but one which I trust reasonably to not be obfuscating connections, and I would like to store a list of each request made through requests.get. Is that something we can do without re-writing the source? – Brian Jun 10 '19 at 15:24
  • 1
    Then it seems like @silver's answer does what you want. Needless to say, you should take care with executing all of the code if you need to get a complete listing of the possible requests made. If you only need to passively monitor the requests, then just keeping a growing log that you inspect manually, should do fine. – chelmertz Jun 11 '19 at 07:13
  • Yes indeed, it was a simple problem in the end. Many thanks. – Brian Jun 11 '19 at 10:29