mirror of https://git.48k.eu/ogclient
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.more_events
parent
4ad2759e55
commit
7548870a92
2
main.py
2
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')
|
||||
|
|
|
@ -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']
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue