Harry
Harry

Reputation: 13329

Understanding and creating a protocol for Python

I have documentation on how to use a TCP/IP binary stream API. The attached images show the protocol. I have a working example of this in C#. I want to do this using python instead, as I don't know c# or windows.

I am assuming I would use python sockets, then I have to send the API messages, with payloads looking at this docs.

Could you point me in the right direction to get this going with python?

How would I set it to know this is the authentication message, 0x21 and compress the data etc?

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(u"{'username':'username', 'password':'password'}".encode('utf8'))

Protocol doc enter image description here enter image description here enter image description here

Upvotes: 2

Views: 4194

Answers (2)

ralf htp
ralf htp

Reputation: 9422

in OSI model you are above layer 4 (transport, TCP/IP,...) on at least layer 5 (session). there are some examples of implementations of session layer protocols like http, ftp,... i.e. in http://github.com/python-git/python/blob/master/Lib/ftplib.py or http://github.com/python-git/python/blob/master/Lib/httplib.py

as your protocol includes headers maybe http is the better example

to use the TCP/IP API in http protocol see http://www.stackoverflow.com/questions/8315209/sending-http-headers-with-python

import socket

sock = socket.socket()
sock.bind(('', 8080))
sock.listen(5)
client, adress = sock.accept()

print "Incoming:", adress
print client.recv(1024)
print

client.send('HTTP/1.0 200 OK\r\n')
client.send("Content-Type: text/html\r\n\r\n")
client.send('<html><body><h1>Hello World</body></html>')
client.close()

print "Answering ..."
print "Finished."

sock.close()

as far as i can see you skipped the headers (version, sequence, type, encoding, ...) in your code completely you have to add them whenever you send a frame

so try

self.socket.send(...headers...)
self.socket.send(u"{'username':'username', 'password':'password'}".encode('utf8')) // has to be send as JSON ???

see also http://www.stackoverflow.com/questions/22083359/send-tetx-http-over-python-socket

ftp example (no headers...)

    # Internal: send one line to the server, appending CRLF
    def putline(self, line):
    line = line + CRLF
    if self.debugging > 1: print '*put*', self.sanitize(line)
    self.sock.sendall(line)

also see scapy

Upvotes: 2

Tural Muzaffarli
Tural Muzaffarli

Reputation: 36

I try to use tcp protocol once. You can send authorization values(username ,password ) in all requests.And other data with it exp({'username':'username', 'password':'password','data':'value'}).With this there is no any current standart for data. Many of tcp clients send that data like this #A#:username;password;#D:value\n (A -authorization ,D -data), example

Upvotes: 0

Related Questions