summaryrefslogtreecommitdiffstats
path: root/src/ogClient.py
diff options
context:
space:
mode:
authorRoberto Hueso gomez <rhueso@soleta.eu>2020-01-16 13:07:15 +0100
committerAlvaro Neira Ayuso <alvaroneay@gmail.com>2020-01-19 19:50:44 +0100
commit0c5cbee301db4be515fd9224f93c026857976488 (patch)
treee5584c44a4ef3cbdc0b37a69a75120450848cb69 /src/ogClient.py
parenta9d81adb1f0e77bfae8a462d509206d43809f7a8 (diff)
Change ogClient.py encoding from dos to unix
Diffstat (limited to 'src/ogClient.py')
-rw-r--r--src/ogClient.py246
1 files changed, 123 insertions, 123 deletions
diff --git a/src/ogClient.py b/src/ogClient.py
index 4f04e56..c380b1d 100644
--- a/src/ogClient.py
+++ b/src/ogClient.py
@@ -1,123 +1,123 @@
-import errno
-import select
-import socket
-import time
-import email
-from io import StringIO
-
-from src.HTTPParser import *
-from src.ogRest import *
-from enum import Enum
-
-class State(Enum):
- CONNECTING = 0
- RECEIVING = 1
- FORCE_DISCONNECTED = 2
-
-class ogClient:
- def __init__(self, ip, port):
- self.ip = ip
- self.port = port
- self.ogrest = ogRest()
-
- def get_socket(self):
- return self.sock
-
- def get_state(self):
- return self.state
-
- def connect(self):
- print ('connecting')
- self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- self.sock.setblocking(0)
- self.state = State.CONNECTING
- self.data = ""
- self.trailer = False
- self.content_len = 0
-
- try:
- self.sock.connect((self.ip, self.port))
- except socket.error as err:
- if err.errno == errno.EINPROGRESS:
- return
- elif err.errno == errno.ECONNREFUSED:
- return
-
- print ('Error connect ' + str(err))
-
- def send(self, msg):
- self.sock.send(bytes(msg, 'utf-8'))
- return len(msg)
-
- def connect2(self):
- try:
- self.sock.connect((self.ip, self.port))
- except socket.error as err:
- if err.errno == errno.EISCONN:
- print ('connected')
- self.state = State.RECEIVING
- else:
- print ('connection refused, retrying...')
- self.state = State.CONNECTING
- self.sock.close()
- self.connect()
-
- def receive(self):
- try:
- 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
- self.sock.close()
- self.connect()
-
- self.data = self.data + data
- httpparser = HTTPParser()
-
- if not self.trailer:
- 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 = email.message_from_file(StringIO(headers_alone))
-
- if 'content-length' in headers.keys():
- self.content_len = int(headers['content-length'])
-
- self.trailer = True
-
- if self.trailer and len(self.data) >= self.content_len:
- httpparser.parser(self.data)
- self.ogrest.processOperation(httpparser, self)
-
- # Cleanup state information from request
- self.data = ""
- self.content_len = 0
- self.trailer = False
-
- def disconnect(self):
- self.state = State.FORCE_DISCONNECTED
- self.sock.close()
-
- def run(self):
- while 1:
- sock = self.get_socket()
- state = self.get_state()
-
- if state == State.CONNECTING:
- readset = [ sock ]
- writeset = [ sock ]
- elif state == State.FORCE_DISCONNECTED:
- return 0
- else:
- readset = [ sock ]
- writeset = [ ]
-
- readable, writable, exception = select.select(readset, writeset, [ ])
- if state == State.CONNECTING and sock in writable:
- self.connect2()
- elif state == State.RECEIVING and sock in readable:
- self.receive()
- else:
- print ('bad state' + str(state))
+import errno
+import select
+import socket
+import time
+import email
+from io import StringIO
+
+from src.HTTPParser import *
+from src.ogRest import *
+from enum import Enum
+
+class State(Enum):
+ CONNECTING = 0
+ RECEIVING = 1
+ FORCE_DISCONNECTED = 2
+
+class ogClient:
+ def __init__(self, ip, port):
+ self.ip = ip
+ self.port = port
+ self.ogrest = ogRest()
+
+ def get_socket(self):
+ return self.sock
+
+ def get_state(self):
+ return self.state
+
+ def connect(self):
+ print ('connecting')
+ self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ self.sock.setblocking(0)
+ self.state = State.CONNECTING
+ self.data = ""
+ self.trailer = False
+ self.content_len = 0
+
+ try:
+ self.sock.connect((self.ip, self.port))
+ except socket.error as err:
+ if err.errno == errno.EINPROGRESS:
+ return
+ elif err.errno == errno.ECONNREFUSED:
+ return
+
+ print ('Error connect ' + str(err))
+
+ def send(self, msg):
+ self.sock.send(bytes(msg, 'utf-8'))
+ return len(msg)
+
+ def connect2(self):
+ try:
+ self.sock.connect((self.ip, self.port))
+ except socket.error as err:
+ if err.errno == errno.EISCONN:
+ print ('connected')
+ self.state = State.RECEIVING
+ else:
+ print ('connection refused, retrying...')
+ self.state = State.CONNECTING
+ self.sock.close()
+ self.connect()
+
+ def receive(self):
+ try:
+ 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
+ self.sock.close()
+ self.connect()
+
+ self.data = self.data + data
+ httpparser = HTTPParser()
+
+ if not self.trailer:
+ 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 = email.message_from_file(StringIO(headers_alone))
+
+ if 'content-length' in headers.keys():
+ self.content_len = int(headers['content-length'])
+
+ self.trailer = True
+
+ if self.trailer and len(self.data) >= self.content_len:
+ httpparser.parser(self.data)
+ self.ogrest.processOperation(httpparser, self)
+
+ # Cleanup state information from request
+ self.data = ""
+ self.content_len = 0
+ self.trailer = False
+
+ def disconnect(self):
+ self.state = State.FORCE_DISCONNECTED
+ self.sock.close()
+
+ def run(self):
+ while 1:
+ sock = self.get_socket()
+ state = self.get_state()
+
+ if state == State.CONNECTING:
+ readset = [ sock ]
+ writeset = [ sock ]
+ elif state == State.FORCE_DISCONNECTED:
+ return 0
+ else:
+ readset = [ sock ]
+ writeset = [ ]
+
+ readable, writable, exception = select.select(readset, writeset, [ ])
+ if state == State.CONNECTING and sock in writable:
+ self.connect2()
+ elif state == State.RECEIVING and sock in readable:
+ self.receive()
+ else:
+ print ('bad state' + str(state))