Oneself use socket module built a simple server, run directly server and client code can realize the communication, but the use of pycharm and pyinstaller module will server packaged into exe file, perform the service side exe file, the client has not receive the server sends the message, nor end
# # server code
` ` ` python
The import socket
The from multiprocessing import Process
# multi-process mode
Def handle_client (client_socket: socket. The socket) :
"" "
Handle the client request
: param args: client socket
: return:
"" "
# for the client request data
Request_data=https://bbs.csdn.net/topics/client_socket.recv (1024)
Request_data. Splitlines ()
Print (" request_data: "request_data)
# structure response data
Response_start_line="HTTP/1.1 200 0 k \ r \ n"
Response_header="Server: My Server \ r \ n"
Response_body="hello wangdaye
"The response=response_start_line + response_header + "\ r \ n" + response_body + "\ n"
Print (" response_data: ", the response)
# returned to the client corresponding data
Client_socket. Send (bytes (response, "utf8"))
# close client connection
Client_socket. Close ()
If __name__=="__main__ ':
HTML_ROOT_DIR='
IP="127.0.0.1"
PORT=8111
Server_socket=socket. The socket (socket. AF_INET, socket. SOCK_STREAM) # using IPv4, using TCP protocol
# set the reuse
Server_socket. Setsockopt (socket. SOL_SOCKET, socket. SO_REUSEADDR, 1)
Server_socket. Bind (# (IP, PORT))
Server_socket. Listen (128)
Print (" service has been launched in {}, {} waiting connection... ". The format (IP, PORT))
While True:
Client_socket, client_address=server_socket. The accept ()
Print (" {}, {} connection ". The format (* client_address))
Handle_client_process=Process (target=handle_client, args=(client_socket,))
Handle_client_process. Start ()
Client_socket. Close ()
` ` `
# # the client code
` ` ` python
From the socket import *
# designated IP and port number
IP="127.0.0.1"
SEVER_PORT=8111
# specifies a maximum length of each cache read
BUFLEN=1024
# to instantiate a socket object, AF_INET said the use of IPv4 network layer, the transport layer using the TCP protocol
ClientSocket=socket (AF_INET SOCK_STREAM)
# connect server address
ClientSocket. Connect ((IP, SEVER_PORT))
While True:
# client input information
ToSend="'
{
"Mobile_phone" : "13367899876",
"PWD" : "lemonban
"}
"'
If toSend=="exit" :
Break
# encoded messages sent
ClientSocket. Send (toSend. Encode ())
# wait for the server return messages, here can't pass by keywords and
Recved=clientSocket. Recv (BUFLEN)
If not recved:
Print (" fanhuiweikong ")
Break
# print decoding after return message
Print (recved. Decode ())
Break
ClientSocket. Close ()
` ` `
CodePudding user response:
Is your server address 127.0.0.1, the client can only from the native access, test can, formal version must be wrongCodePudding user response:
Blogger solved excuse me? I also encountered similar problems, python code can run normally, but not after the packaged into exe, checked a day a bug is indeed a socket to receive less than the information on severCodePudding user response: