如何在python 3.7中修复“ OSError:[Errno 98]地址已在使用中”?

时间:2019-09-07 17:56:49

标签: python sockets

我看过另一篇有关此问题的文章,但找不到解决我问题的好方法...我使用Archlinux和Python 3.7。

我有这两个python脚本:

#!/bin/env python

#--*-- coding:UTF-8 --*--

import socket
connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = input("Ip : ")
port = int(input("Port : "))
connexion.connect((host, port))
print("The connection with {} is done.".format(host, port))
started = True

while started:
    choice = int(input(" [1] Command\n [2] Leave\n > "))
    if choice == 1:
        command = input(" Command : ")
        connexion.send(command.encode())
        retour = connexion.recv(1024).decode('latin1')
        print(retour)

        if choice == 2:
            connexion.send("exit")
            connexion.close()
            break

然后:

#!/bin/env python

#--*-- coding:UTF-8 --*--

import socket
import subprocess

connexion = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 631
connexion.bind((host,port))
connexion.listen(5)
connexion_client, infos_connexion = connexion.accept()
started = True

while started:
    rcv_cmd = connexion_client.recv(1024)
    rcv_cmd = rcv_cmd.decode()
    if rcv_cmd != "exit":
        cmd = subprocess.Popen(rcv_cmd, shell=true, stdout = subprocess.PIPE, stderr =subprocess.PIPE, stdin = subprocess.PIPE )
        out = cmd.stdout.read() + cmd.sdterr.read()
        connexion_client.send(out)
    else:
        started = False
        conexion.close()
        exit

但是,如果我运行的第一个脚本的IP地址为127.0.0.1,IP地址为631,如果我尝试运行两个脚本的第二个脚本,则出现此错误:

Traceback (most recent call last):
  File "client.py", line 11, in <module>
    connexion.bind((host,port))
OSError: [Errno 98] Address already in use

我不知道为什么...你能告诉我问题出在哪里吗?

0 个答案:

没有答案