2017-02-10 38 views
1

我有关于如何使用TCP/IP二进制流API的文档。附加的图像显示协议。我在C#中有一个这样的工作示例。我想用python来代替,因为我不知道c#或windows。了解和创建用于Python的协议

我假设我会使用python套接字,然后我必须发送API消息,有效载荷看这个文档。

你能指出我在正确的方向来得到这与python?

我该如何设置它来知道这是身份验证消息,0x21和压缩数据等?

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

+1

不确定什么阻止了你?你知道Python,你知道如何在Python中实现TCP通信吗?你有协议文档... – Fildor

+0

我不知道它那么好,我不确定你如何设置编码,压缩等。Im用于http文章 – Harry

+0

关于这两个你的Docu sais:“请注意,服务器将使用与用于发送客户端认证信息相同“(不是确切的引用)。所以我会开始没有压缩和任何适合你最好的(JSON/BSON)。如果可行,继续前进。哦,它也SAIS“UTF-8”:) – Fildor

回答

0

我尝试使用TCP协议一次。您可以在所有请求中发送授权值(用户名,密码)。以及其他数据exp({'username':'username','password':'password','data':'value'})。目前没有任何数据标准。许多tcp客户端发送的数据是这样的#A#:用户名;密码; #D:值\ n(A-授权,D -data)example

2

在OSI模型中,您位于第4层(传输,TCP/IP,...)至少在第5层(会话)上。有会话层协议实现方式的一些实例像httpftp,...在http://github.com/python-git/python/blob/master/Lib/ftplib.pyhttp://github.com/python-git/python/blob/master/Lib/httplib.py

即作为协议包括也许HTTP是更好的例子

使用TCP/IP API中HTTP协议见 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() 

据我可以看到你跳过头(版本,序列,类型,编码,...)在你的代码完全你必须增加他们只要发送帧

所以尽量

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

也看到http://www.stackoverflow.com/questions/22083359/send-tetx-http-over-python-socket

例如FTP(无头...)

# 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) 

也看到Scapy的