2017-02-10 40 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的