-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinbedded-SQL-Proxy-Client.py
More file actions
134 lines (100 loc) · 2.64 KB
/
Linbedded-SQL-Proxy-Client.py
File metadata and controls
134 lines (100 loc) · 2.64 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
# (c) 2014-2015 LINBEDDED Pawel Suchanecki
# pawel.suchanecki@gmail.com
import os
import re
import socket
import sys
# rcvd msg
data = '';
# ip
ip = ''
# globals used before START
rdy_file = sys.argv[2] + ".rdy"
def is_valid_ipv4(ip_str):
"""
Check the validity of an IPv4 address
"""
try:
socket.inet_pton(socket.AF_INET, ip_str)
except AttributeError:
try:
socket.inet_aton(ip_str)
except socket.error:
return False
return ip_str.count('.') == 3
except socket.error:
return False
return True
def write_error_msg (pdata):
print sys.argv[2]
f = open(sys.argv[2], "w+")
f.writelines(str(pdata))
f.close()
# rdy file
r = open(rdy_file, 'w')
r.writelines("1")
r.close()
sys.exit(1)
# START
# read config
conf = open ('/tmp/SERVER_IP', 'r')
for line in conf:
ip=line.rstrip('\n')
conf.close
# validate ip
if is_valid_ipv4(ip):
pass
else:
write_error_msg ("DB_ERROR: adres IP jest nieprawidlowy\n");
# remove dest file
if os.path.isfile(sys.argv[2]):
#print "removing file: %s\n" % sys.argv[2]
os.remove(sys.argv[2])
if os.path.isfile(rdy_file):
os.remove(rdy_file)
try:
# create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# set timeout
sock.settimeout(3)
# connect the socket to the port where the server is listening
server_address = (ip, 9999)
# print >>sys.stdout, 'connecting to %s port %s' % server_address
sock.connect(server_address)
# After the connection is established
# data can be sent through the socket with sendall()
# and received with recv(), just as in the server.
# Send data
#message = 'BASE:pomiary#SELECT * from pomiary order by ID_ZAP;'
message = sys.argv[1]
#print >>sys.stderr, 'sending "%s"' % message
sock.sendall(message)
# Look for the response
amount_received = 0
m = re.search ('x', 'y');
while ( m == None ):
data += sock.recv(16)
amount_received += len(data)
m = re.search ('(DB_OK|DB_ERROR)', str(data))
#print >>sys.stdout, '%s' % data
f = open(sys.argv[2], "w+")
f.writelines(str(data))
f.close()
# rdy file
open(rdy_file, 'w').close()
except socket.error as msg:
data = 'DB_ERROR: socket.error: ' + str(msg) + "\n"
write_error_msg(data)
except socket.herror as msg:
data = 'DB_ERROR: socket.herror: ' + str(msg) + "\n"
write_error_msg(data)
except socket.gaierror as msg:
data = 'DB_ERROR: socket.gaierror: ' + str(msg) + "\n"
write_error_msg(data)
except socket.timeout as msg:
data = 'DB_ERROR: socket.timeout: ' + str(msg) + "\n"
write_error_msg(data)
finally:
#print >>sys.stderr, 'closing socket'
sock.close()