Home > other >  Python network programming problems.
Python network programming problems.

Time:10-18

 
The import socket

Target_host='www.baidu.com'
Target_port=80

Client=socket. The socket (socket. AF_INET, socket. SOCK_STREAM)
Client. The connect ((target_host target_port))
MSG='get/http/1.1\r\nHost:baidu.com \ r \ n \ r \ n'
Client. The send (MSG) encode (' utf-8))
The response=client. Recv (4096)

Print (response)

Client. The close


Why in the contents of the output b 'HTTP/1.1 400 Bad Request \ r \ n \ r \ n' b 'will have?
Write the udp is also will have these two characters,

CodePudding user response:

B '... 'when python2 introduction, then call binary string, Python is called byte string after 3 byte string, similar to C in the char [], the bytes in the Java [] a byte one character at a time, using ASCII encoding,
Server response is usually byte string, compatible with good

Note that the default in the python is unicode. Of course you can specify the code
So in general, judge: 'A'==b 'A' result is False

Python also some famous series, such as in regular, common r '... After 'raw string, Python 3.6 introduced f'... 'the format string of the format string

Want to convert, have to decode () function. The STR () no
 & gt;> S=b 'hello, world' 
> S
B 'hello, world'
> STR (s)
"B 'hello, world"
> Spyware doctor ecode (' ASCII)
'hello, world'



  • Related