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():
|
def main():
|
||||||
ogconfig = ogConfig()
|
ogconfig = ogConfig()
|
||||||
if (not ogconfig.parserFile('cfg/ogagent.cfg')):
|
if (not ogconfig.parserFile('cfg/ogagent.cfg')):
|
||||||
print 'Error: Parsing configuration file'
|
print ('Error: Parsing configuration file')
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
ip = ogconfig.getValueSection('opengnsys', 'ip')
|
ip = ogconfig.getValueSection('opengnsys', 'ip')
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from mimetools import Message
|
import email
|
||||||
from StringIO import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
class HTTPParser:
|
class HTTPParser:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -14,7 +14,7 @@ class HTTPParser:
|
||||||
|
|
||||||
def parser(self,data):
|
def parser(self,data):
|
||||||
self.requestLine, self.headersAlone = data.split('\n', 1)
|
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():
|
if 'host' in self.headers.keys():
|
||||||
self.host = self.headers['host']
|
self.host = self.headers['host']
|
||||||
|
|
|
@ -2,9 +2,11 @@ import errno
|
||||||
import select
|
import select
|
||||||
import socket
|
import socket
|
||||||
import time
|
import time
|
||||||
|
import email
|
||||||
|
from io import StringIO
|
||||||
|
|
||||||
from HTTPParser import *
|
from src.HTTPParser import *
|
||||||
from ogProcess import *
|
from src.ogProcess import *
|
||||||
from enum import Enum
|
from enum import Enum
|
||||||
|
|
||||||
class State(Enum):
|
class State(Enum):
|
||||||
|
@ -23,7 +25,7 @@ class ogClient:
|
||||||
return self.state
|
return self.state
|
||||||
|
|
||||||
def connect(self):
|
def connect(self):
|
||||||
print "connecting"
|
print ('connecting')
|
||||||
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||||
self.sock.setblocking(0)
|
self.sock.setblocking(0)
|
||||||
self.state = State.CONNECTING
|
self.state = State.CONNECTING
|
||||||
|
@ -33,32 +35,32 @@ class ogClient:
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.sock.connect((self.ip, self.port))
|
self.sock.connect((self.ip, self.port))
|
||||||
except socket.error, err:
|
except socket.error as err:
|
||||||
if err.errno == errno.EINPROGRESS:
|
if err.errno == errno.EINPROGRESS:
|
||||||
return
|
return
|
||||||
elif err.errno == errno.ECONNREFUSED:
|
elif err.errno == errno.ECONNREFUSED:
|
||||||
return
|
return
|
||||||
|
|
||||||
print "Error connect " + str(err)
|
print ('Error connect ' + str(err))
|
||||||
|
|
||||||
def connect2(self):
|
def connect2(self):
|
||||||
try:
|
try:
|
||||||
self.sock.connect((self.ip, self.port))
|
self.sock.connect((self.ip, self.port))
|
||||||
except socket.error, err:
|
except socket.error as err:
|
||||||
if err.errno == errno.EISCONN:
|
if err.errno == errno.EISCONN:
|
||||||
print "connected"
|
print ('connected')
|
||||||
self.state = State.RECEIVING
|
self.state = State.RECEIVING
|
||||||
else:
|
else:
|
||||||
print "connection refused, retrying..."
|
print ('connection refused, retrying...')
|
||||||
self.state = State.CONNECTING
|
self.state = State.CONNECTING
|
||||||
self.sock.close()
|
self.sock.close()
|
||||||
self.connect()
|
self.connect()
|
||||||
|
|
||||||
def receive(self):
|
def receive(self):
|
||||||
try:
|
try:
|
||||||
data = self.sock.recv(1024)
|
data = self.sock.recv(1024).decode('utf-8')
|
||||||
except socket.err, err:
|
except socket.err as err:
|
||||||
print "Error3 " + str(err)
|
print ('Error3 ' + str(err))
|
||||||
|
|
||||||
if len(data) == 0:
|
if len(data) == 0:
|
||||||
self.state = State.CONNECTING
|
self.state = State.CONNECTING
|
||||||
|
@ -73,7 +75,7 @@ class ogClient:
|
||||||
if self.data.find("\r\n") > 0:
|
if self.data.find("\r\n") > 0:
|
||||||
# https://stackoverflow.com/questions/4685217/parse-raw-http-headers
|
# https://stackoverflow.com/questions/4685217/parse-raw-http-headers
|
||||||
request_line, headers_alone = self.data.split('\n', 1)
|
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():
|
if 'content-length' in headers.keys():
|
||||||
self.content_len = int(headers['content-length'])
|
self.content_len = int(headers['content-length'])
|
||||||
|
@ -83,9 +85,9 @@ class ogClient:
|
||||||
if self.trailer and len(self.data) >= self.content_len:
|
if self.trailer and len(self.data) >= self.content_len:
|
||||||
httpparser.parser(self.data)
|
httpparser.parser(self.data)
|
||||||
if not ogprocess.processOperation(httpparser.getRequestOP(), httpparser.getURI()):
|
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:
|
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
|
# Cleanup state information from request
|
||||||
self.data = ""
|
self.data = ""
|
||||||
|
@ -110,4 +112,4 @@ class ogClient:
|
||||||
elif state == State.RECEIVING and sock in readable:
|
elif state == State.RECEIVING and sock in readable:
|
||||||
self.receive()
|
self.receive()
|
||||||
else:
|
else:
|
||||||
print "bad state" + str(state)
|
print ('bad state' + str(state))
|
||||||
|
|
|
@ -3,7 +3,7 @@ import platform
|
||||||
import time
|
import time
|
||||||
|
|
||||||
if platform.system() == 'Linux':
|
if platform.system() == 'Linux':
|
||||||
from linux import ogOperations
|
from src.linux import ogOperations
|
||||||
|
|
||||||
class ogProcess():
|
class ogProcess():
|
||||||
def processOperation(self, op, URI):
|
def processOperation(self, op, URI):
|
||||||
|
|
Loading…
Reference in New Issue