Problem
In the work often uses the socket transmits the data, for example the client sends the data to the server (both sides agreed the data format), before the test, uses the python to write the server which accepts the data, parses the data format is correct. Writing in Python is a lot simpler than the C language.
PS: In fact, I do not python, working in C/D + + development, using Python is purely for lazy ^_^
To give a specific example: the data format agreed by both parties to the communication is
The data format is binary, and Python needs to use the struct module to process the binary data. The most important three function pack () in the struct module, unpack (), calcsize (). Because structs are equivalent to structs in the C language, unpack () returns a tuple. The format supported by the struct is the following table
Note 1) Q and q are only meaningful when the machine supports 64 bits;
Note 2) Each format can have a number, indicating the number;
Note 3) The S format represents a string of a certain length, 4s represents a string of length 4, and p represents a Pascal string;
Note 4) p is used to convert a pointer, whose length is related to the machine word size;
By default, structs are converted according to the local machine byte order, or you can change the alignment by using the first character in the format. Defined as follows:
Note: Whether the packet is a Python program struct.pack () or a C,c++,java program, just ensure that the client side and server byte order are consistent.
The pack () and unpack () functions are illustrated by an example at the beginning of the article:
Note: The test environment in Chinese is utf-8 encoding (python coding toss a half-day, also do not understand, here is not the focus)
1) Pack (format, v1, v2, ...) Encapsulates the data into a string, as specified by format (format), for example
>>s=struct.pack ("2i13si6s2i", "www.baidu.com", 6, "Winter", 0, 0)
2) Unpack (format, string) parses the byte stream string in the given format (FMT), returning the parsed tuple, for example
>>us=struct.unpack ("2i13si6s2i", s)
Output Result:
>>print US
(www.baidu.com, 6, ' \xe5\x86\xac\xe5\xad\xa3 ', 0, 0)
Note: The Chinese part is binary, which is removed from the tuple and then printed
>> Print Us[4]
Winter
Note: Students interested in Chinese coding under Python can study the Python Environment Code (again, I really don't python! >_<)
To give a simple example:
#!/usr/bin/Pythonimport SocketimportstructImport Osimport timeif__name__ = ="__main__": Server=Socket.socket (socket.af_inet, socket. Sock_stream) Server.bind (("127.0.0.1",51001)) #本机端口号51001 Server.listen (1) while(1): Conn,client=server.accept () conn.settimeout ( the) #设置超时时间 msg= Conn.recv (4) #total Data lengthifLen (msg) <=0: #接收空数据包ContinueData=struct. Unpack ("I", msg) Print"Recv Total length:%d"% (data[0]) Process_len=0msg= Conn.recv (data[0]) forIinchRange0,4): #循环四次, take URL title content author para= Msg[process_len: (Process_len +4)] ifLen (para) <4: #如果某一字段为空, do not processContinueData=struct. Unpack ("I", para) Str_len= data[0] Print"%d"%(STR_LEN) para= msg[(Process_len +4):(Process_len +4+Str_len)] ifLen (para) <Str_len: #如果实际收到的字符串长度小于数据头给的长度, not processedContinueData=struct. Unpack ("%ds"%(Str_len), para) print"%s"% (data[0]) Process_len= Process_len +4+Str_len conn.close ()
Python socket binary