From 7548870a923db820380d352f65ff2e05de2ef65d Mon Sep 17 00:00:00 2001 From: Alvaro Neira Ayuso Date: Fri, 13 Dec 2019 11:47:31 +0100 Subject: Port program to Python 3 Python 2.7 will be deprecated in January of 2020. So, we need to have the program with a supported api. --- main.py | 2 +- src/HTTPParser.py | 6 +++--- src/ogClient.py | 32 +++++++++++++++++--------------- src/ogProcess.py | 2 +- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index 66b451f..c021219 100644 --- a/main.py +++ b/main.py @@ -4,7 +4,7 @@ from src.ogConfig import * def main(): ogconfig = ogConfig() if (not ogconfig.parserFile('cfg/ogagent.cfg')): - print 'Error: Parsing configuration file' + print ('Error: Parsing configuration file') return 0 ip = ogconfig.getValueSection('opengnsys', 'ip') diff --git a/src/HTTPParser.py b/src/HTTPParser.py index bbced43..7c64805 100644 --- a/src/HTTPParser.py +++ b/src/HTTPParser.py @@ -1,5 +1,5 @@ -from mimetools import Message -from StringIO import StringIO +import email +from io import StringIO class HTTPParser: def __init__(self): @@ -14,7 +14,7 @@ class HTTPParser: def parser(self,data): self.requestLine, self.headersAlone = data.split('\n', 1) - self.headers = Message(StringIO(self.headersAlone)) + self.headers = email.message_from_file(StringIO(self.headersAlone)) if 'host' in self.headers.keys(): self.host = self.headers['host'] diff --git a/src/ogClient.py b/src/ogClient.py index e28193b..4fbdfab 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -2,9 +2,11 @@ import errno import select import socket import time +import email +from io import StringIO -from HTTPParser import * -from ogProcess import * +from src.HTTPParser import * +from src.ogProcess import * from enum import Enum class State(Enum): @@ -23,7 +25,7 @@ class ogClient: return self.state def connect(self): - print "connecting" + print ('connecting') self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.sock.setblocking(0) self.state = State.CONNECTING @@ -33,32 +35,32 @@ class ogClient: try: self.sock.connect((self.ip, self.port)) - except socket.error, err: + except socket.error as err: if err.errno == errno.EINPROGRESS: return elif err.errno == errno.ECONNREFUSED: return - print "Error connect " + str(err) + print ('Error connect ' + str(err)) def connect2(self): try: self.sock.connect((self.ip, self.port)) - except socket.error, err: + except socket.error as err: if err.errno == errno.EISCONN: - print "connected" + print ('connected') self.state = State.RECEIVING else: - print "connection refused, retrying..." + print ('connection refused, retrying...') self.state = State.CONNECTING self.sock.close() self.connect() def receive(self): try: - data = self.sock.recv(1024) - except socket.err, err: - print "Error3 " + str(err) + data = self.sock.recv(1024).decode('utf-8') + except socket.err as err: + print ('Error3 ' + str(err)) if len(data) == 0: self.state = State.CONNECTING @@ -73,7 +75,7 @@ class ogClient: if self.data.find("\r\n") > 0: # https://stackoverflow.com/questions/4685217/parse-raw-http-headers request_line, headers_alone = self.data.split('\n', 1) - headers = Message(StringIO(headers_alone)) + headers = email.message_from_file(StringIO(headers_alone)) if 'content-length' in headers.keys(): self.content_len = int(headers['content-length']) @@ -83,9 +85,9 @@ class ogClient: if self.trailer and len(self.data) >= self.content_len: httpparser.parser(self.data) if not ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI()): - self.sock.send("HTTP/1.0 400 Bad request\r\n\r\n") + self.sock.send(bytes('HTTP/1.0 400 Bad request\r\n\r\n', 'utf-8')) else: - self.sock.send("HTTP/1.0 200 OK\r\n\r\n") + self.sock.send(bytes('HTTP/1.0 200 OK\r\n\r\n', 'utf-8')) # Cleanup state information from request self.data = "" @@ -110,4 +112,4 @@ class ogClient: elif state == State.RECEIVING and sock in readable: self.receive() else: - print "bad state" + str(state) + print ('bad state' + str(state)) diff --git a/src/ogProcess.py b/src/ogProcess.py index 143f49a..6601b8c 100644 --- a/src/ogProcess.py +++ b/src/ogProcess.py @@ -3,7 +3,7 @@ import platform import time if platform.system() == 'Linux': - from linux import ogOperations + from src.linux import ogOperations class ogProcess(): def processOperation(self, op, URI): -- cgit v1.2.3-18-g5258