python-proxy icon indicating copy to clipboard operation
python-proxy copied to clipboard

Monitor bandwidth usage ?

Open lucydjo opened this issue 2 years ago • 3 comments

Hello, Is it possible to calculate the bandwidth usage? I can't find anything about this in the documentation.

Thanks :)

lucydjo avatar Apr 03 '23 05:04 lucydjo

Add verbose :) pproxy -r socks5://{proxy['host']}:{proxy['port']}#{proxy['username']}:{proxy['password']} -l socks5://:{server_port} -vv

This prints out the current usage to stdout. What I did is sent stdout to a class, and parsed the data. Search for data_usage as such:

	class OutputHandler:
		def __init__(self):
			pass

		def write(self, message):
			self.parse_and_handle_output(message.strip())

		def flush(self):
			pass

		def print_to_output(self, message):
			print(message, file=sys.__stdout__)

		def parse_and_handle_output(self, output):
			websites = re.findall(r'->\s(?:.*)->\s(.*):\d+', output)
			data_usage = re.search(
				"DIRECT: \d+ \(([\d.]+[KM])/s,([\d.]+[KM])/s\)   PROXY: \d+ \(([\d.]+[KM])/s,([\d.]+[KM])/s\)",
				output)
			if websites:  # Parse the websites
				for website in websites:
					self.print_to_output(f"URL: {website}")
			elif data_usage and data_usage != "":  # Parse the data usage
				try:
					direct, direct2, proxy, proxy2 = data_usage.groups()
					direct_combined = self.to_megabytes(direct) + self.to_megabytes(direct2)
					proxy_combined = self.to_megabytes(proxy) + self.to_megabytes(proxy2)
					combined = float(direct_combined + proxy_combined)
					self.print_to_output(round(combined, 4))
			else:
				if output and output != "":
					self.print_to_output(output)

		@staticmethod
		def to_megabytes(value):
			if value[-1] == 'M':
				return float(value[:-1])
			elif value[-1] == 'K':
				return float(value[:-1]) / 1024
			else:
				return 0
sys.stdout = self.OutputHandler()

strangeh21 avatar Apr 17 '23 20:04 strangeh21

@strangeh21 Sorry, can you explain further? Are you running python proxy within a Python script? I'm running via command-line so I'm unclear on how I would adjust your solution.

maxiedaniels avatar May 01 '23 21:05 maxiedaniels

@strangeh21 Sorry, can you explain further? Are you running python proxy within a Python script? I'm running via command-line so I'm unclear on how I would adjust your solution.

In short, verbose level 2 within commandline adds traffic logging. This is done by adding "-vv" When it is running you can press enter at any time to get a complete list of data usage.

strangeh21 avatar May 02 '23 04:05 strangeh21