简体   繁体   中英

Python 3.4 input?

I'm trying to make a proof of concept tcp server and client with Python 3.4. Everything with network part works nice. I tried to make it interactive and get user input from the client and send it to the server. But when I try to use it as HTTP client (write a custom request in console and send it to the server) it failed to get a response. The problem is that input() function interprets \\r\\n\\ as string. How can I tell to python to interpret them as a .

Example :

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(("www.google.com", 80))

user_input = input("Inser message: ") # GET / HTTP/1.1\r\n\r\n
sock.sendall(user_input.encode("utf-8"))

message = sock.recv(4096)

Thanks in advance for any suggestion.

您可以使用replace:

user_input = user_input.replace('\\r', '\r').replace('\\n', '\n')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM