-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathconnect.py
35 lines (28 loc) · 1.28 KB
/
connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import socket
class connect:
def __init__(self):
self.raw_response = None
self.filetype = None
def request(self, resource, host, itemtype, port=70):
#connects to server and returns list with response type and body
socket_conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_conn.settimeout(15.0)
try:
socket_conn.connect((host, port))
socket_conn.sendall((resource + '\r\n').encode('utf-8'))
if itemtype in ['i','0','1','3']:
response = socket_conn.makefile(mode = 'r', errors = 'ignore')
else:
response = socket_conn.makefile(mode = 'rb', errors = 'ignore')
self.raw_response = response.read()
self.filetype = itemtype
except socket.timeout:
print('Socket timeout')
socket_conn.close()
return {'type': '1', 'body': '3ERROR: Server request timed out\tfalse\tnull.host\t1'}
except Exception as e:
print('Misc socket error: ', e)
socket_conn.close()
return {'type': '1', 'body': '3ERROR: Unable to communicate with remote server\tfalse\tnull.host\t1'}
socket_conn.close()
return {'type': self.filetype, 'body': self.raw_response}