Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Python 3 support #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ def communicate(self, command):
''' Send a single command to the socket and return a single response (raw string) '''
s = self.connect()
if not command.endswith('\n'): command += '\n'
s.send(command)
s.send(command.encode())
result = ''
buf = ''
buf = s.recv(RECV_SIZE)
while buf:
result += buf
result += buf.decode()
Copy link

@graingert graingert Nov 30, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to use an incremental decoder, or decode the whole value at once in return result.decode()

buf = s.recv(RECV_SIZE)
s.close()
return result
Expand All @@ -77,7 +77,7 @@ def get_server_info(self):
for line in output.splitlines():
try:
key,val = line.split(':')
except ValueError, e:
except ValueError as e:
continue
result[key.strip()] = val.strip()
return result
Expand All @@ -98,15 +98,15 @@ def get_stats():
try:
server_info = haproxy.get_server_info()
server_stats = haproxy.get_server_stats()
except socket.error, e:
except socket.error as e:
logger('warn', "status err Unable to connect to HAProxy socket at %s" % HAPROXY_SOCKET)
return stats

if 'server' in PROXY_MONITORS:
for key,val in server_info.items():
try:
stats[key] = int(val)
except (TypeError, ValueError), e:
except (TypeError, ValueError) as e:
pass

for statdict in server_stats:
Expand All @@ -118,7 +118,7 @@ def get_stats():
metricname = METRIC_DELIM.join([ statdict['svname'].lower(), statdict['pxname'].lower(), key ])
try:
stats[metricname] = int(val)
except (TypeError, ValueError), e:
except (TypeError, ValueError) as e:
pass
return stats

Expand Down Expand Up @@ -159,7 +159,7 @@ def read_callback():
if not value in METRIC_TYPES:
try:
key_prefix, key_root = key.rsplit(METRIC_DELIM,1)
except ValueError, e:
except ValueError as e:
pass
if not key_root in METRIC_TYPES:
continue
Expand Down