-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclient.py
executable file
·233 lines (189 loc) · 4.94 KB
/
client.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#! /usr/bin/env python
# Copyright 2012 Johan Astborg
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncore, socket
import os
import datetime
HOST = '192.168.0.13' # The remote host
PORT = 1234 # The same port as used by the server
SOH = '\x01'
TAGS = {
8: 'BeginString',
9: 'BodyLength',
35: 'MsgType',
49: 'SenderCompID',
56: 'TargetCompID',
34: 'MsgSeqNum',
52: 'SendingTime',
10: 'CheckSum',
98: 'EncryptMethod',
108: 'HeartBtInt'
}
TAGSR = {
'BeginString': 8,
'BodyLength': 9,
'MsgType': 35,
'SenderCompID': 49,
'TargetCompID': 56,
'MsgSeqNum': 34,
'SendingTime': 52,
'CheckSum': 10,
'EncryptMethod': 98,
'HeartBtInt': 108
}
MSGTYPES = {
'A': 'Logon',
'0': 'HeartBeat',
'1': 'Test Request',
'2': 'Resend Request',
'4': 'Sequence Reset',
'5': 'Logout',
'8': 'ExecutionReport'
}
MSGTYPESR = {
'Logon': 'A',
'HeartBeat':'0',
'Test Request':'1',
'Resend Request':'2',
'Sequence Reset':'4',
'Logout':'5',
'ExecutionReport':'8'
}
HEADER = [
'SenderCompID',
'TargetCompID',
'MsgSeqNum',
'SendingTime',
]
class FIXClient(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
self.buffer = logon_message() + '\x00'
def handle_connect(self):
self.send(self.buffer)
pass
def handle_close(self):
self.close()
def handle_read(self):
print "----------INBOUND----------"
msg = self.recv(8192)
parse(msg)
def writable(self):
return (len(self.buffer) > 0)
def handl_expt(self):
self.handle_error()
def handle_error(self):
print "Error"
def handle_write(self):
print "----------OUTBOUND----------"
parse(self.buffer)
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
pass
def current_datetime():
return datetime.datetime.utcnow().strftime("%Y%m%d-%H:%M:%S.%f")[:-3]
def make_tag(tag, value):
print "%s=%s" % (TAGSR[tag], value)
def pack(msgs):
# Create body
body = []
if 'SenderCompID' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['SenderCompID'], msgs['SenderCompID']))
if 'TargetCompID' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['TargetCompID'], msgs['TargetCompID']))
if 'MsgSeqNum' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['MsgSeqNum'], msgs['MsgSeqNum']))
if 'SendingTime' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['SendingTime'], msgs['SendingTime']))
if 'EncryptMethod' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['EncryptMethod'], msgs['EncryptMethod']))
if 'HeartBtInt' not in msgs:
print 'ERROR'
return
else:
body.append("%i=%s" % (TAGSR['HeartBtInt'], msgs['HeartBtInt']))
# Enable easy change when debugging
SEP = SOH
body = SEP.join(body) + SEP
# Create header
header = []
header.append("%s=%s" % (TAGSR['BeginString'], 'FIX.4.2'))
header.append("%s=%i" % (TAGSR['BodyLength'], len(body)))
header.append("%s=%s" % (TAGSR['MsgType'], MSGTYPESR[msgs['MsgType']]))
fixmsg = SEP.join(header) + SEP + body
cksum = sum([ord(i) for i in list(fixmsg)]) % 256
fixmsg = fixmsg + "%i=0%s" % (TAGSR['CheckSum'], cksum)
return fixmsg + SEP
def parse(rawmsg):
msg = rawmsg.rstrip(os.linesep).split(SOH)
msg = msg[:-1]
msgs = {}
print "|".join(msg)
for m in msg:
tag, value = m.split('=', 1)
if int(tag) not in TAGS:
print "Not valid: %s" % (tag)
else:
t = TAGS[int(tag)]
if t == 'CheckSum':
cksum = ((sum([ord(i) for i in list(SOH.join(msg[:-1]))]) + 1) % 256)
if cksum == int(value):
print "CheckSum\t%s (OK)" % (int(value))
else:
print "CheckSum\t%s (INVALID)" % (int(value))
elif t == 'MsgType':
msgs[t] = MSGTYPES[value]
print "MsgType\t\t%s" % msgs[t]
else:
msgs[t] = value
def logon_message():
msgs = {}
msgs['SendingTime'] = current_datetime()
msgs['SenderCompID'] = "BANZAI"
msgs['TargetCompID'] = "FIXIMULATOR"
msgs['MsgSeqNum'] = 12
msgs['EncryptMethod'] = 0
msgs['MsgType'] = 'Logon'
msgs['HeartBtInt'] = 30
return pack(msgs)
def main():
msgs = {}
msgs['SendingTime'] = current_datetime()
msgs['SenderCompID'] = "BANZAI"
msgs['TargetCompID'] = "FIXIMULATOR"
msgs['MsgSeqNum'] = 12
msgs['EncryptMethod'] = 0
msgs['MsgType'] = 'Logon'
msgs['HeartBtInt'] = 30
client = FIXClient(HOST, PORT)
asyncore.loop()
if __name__ == "__main__":
main()