简体   繁体   English

在Python上使用套接字进行通信

[英]Communicate using socket on Python

I have made two scripts to practice using sockets in Python but I have trouble communicating after the connexion is established: My scripts below : 我制作了两个脚本来练习在Python中使用套接字,但是在建立连接之后,我无法进行通信:我的以下脚本:

Server.py Server.py

#!/usr/local/bin/python3.5

import socket, sys

HOST = 'myIP'
PORT = 50000
counter = 0

mySocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    mySocket.bind((HOST,PORT))
except socket.error:
    print("Socket connection failed.")
    sys.exit

while 1:
    print("Server ready, waiting for request...")
    mySocket.listen(2)

    connexion, adress = mySocket.accept()
    counter+=1
    print("Client connected, adress IP %s, port %s" % (adress[0], adress[1]))

    msgServeur="Connected to server PytPyt. You can send messages."
    connexion.send(msgServeur.encode("Utf8"))
    msgClient = connexion.recv(1024).decode("Utf8")
    while 1:
        print("C>", msgClient)
        if msgClient.upper() == "END" or msgClient == "":
            break
        msgServeur = input("S> ")
        connexion.send(msgServeur.encode("Utf8"))
        msgClient = connexion.recv(1024).decode("Utf8")

    connexion.send("end".encode("Utf8"))
    print("Connexion finished.")
    connexion.close()

    ch=input("<R>etry <T>erminate?")
    if ch.upper() =='T':
        break

Client.py 客户端

#!/usr/local/bin/python3.5

import socket, sys

HOST = 'myIP'
PORT = 50000

mySocket=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    mySocket.connect((HOST, PORT))
except socket.error:
    print("Connexion failed.")
    sys.exit()
print("Connexion established with server.")

msgServeur = mySocket.recv(1024).decode("Utf8")

while 1:
    if msgServeur.upper == "END"  or msgServeur == "":
        break
    print("S>", msgServeur)
    msgClient=input("C> ")
    mySocket.send(msgClient.encode("Utf8"))
    msgServeur = mySocket.recv(1024).decode("Utf8")

print("Connexion terminated.")
mySocket.close()

When I execute the two scripts I have the result below : 当我执行两个脚本时,结果如下:

Server : 服务器:

myPrompt : ./Server.py &
Server ready, waiting for request...
Client connected, adresse IP myIP, port 53551 

Client : 客户:

myPrompt : ./Client.py &
Connexion established with server.
S> Connected to server PytPyt. You can send messages.
C> hello
-bash: hello: command not found

[1]+  Stopped                 ./Client.py

It seems that my message is executed as a bash command and not a message to send. 看来我的消息是作为bash命令执行的,而不是要发送的消息。 However if I run the job again it will work : 但是,如果我再次运行该作业,它将起作用:

Client : 客户:

myPrompt : %
./Client.py
hello

Server : 服务器:

C> hello
S> 

But it fails again right after. 但此后它再次失败。 I have to run the job again any time I want to send a message. 每当我要发送消息时,我都必须再次运行作业。

Do you know where the mistake is? 你知道错误在哪里吗?

Actually problem lay in script launching. 实际上问题出在脚本启动上。 You using following command 您使用以下命令

./Client.py &

With & the process starts in the background, so you can continue to use the shell. 使用&在后台启动该过程,因此您可以继续使用该Shell。 & detach stdin of your script from terminal. &从终端分离脚本的stdin And all that you print in terminal after script launching will be interpreted as bash commands. 在脚本启动后,您在终端上打印的所有内容都将被解释为bash命令。
Try to use just 尝试只使用

./Client.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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