diff options
author | Alvaro Neira Ayuso <aneira@soleta.eu> | 2019-12-13 11:47:31 +0100 |
---|---|---|
committer | Alvaro Neira Ayuso <alvaroneay@gmail.com> | 2020-01-19 19:50:44 +0100 |
commit | 7548870a923db820380d352f65ff2e05de2ef65d (patch) | |
tree | 1cf97dfb60f51c1510f7b0591f561f4930b9c162 /src/ogClient.py | |
parent | 4ad2759e55f3038cd4680d213c8b59bb135d98b4 (diff) |
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.
Diffstat (limited to 'src/ogClient.py')
-rw-r--r-- | src/ogClient.py | 32 |
1 files changed, 17 insertions, 15 deletions
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))
|