客户端套接字无法连接到服务器套接字,[Errno 32]管道断开错误

时间:2019-05-19 22:06:56

标签: python sockets

我已经编写了一个客户端服务器python程序,客户端可以在其中将数据发送到服务器。但是当客户端尝试与服务器连接时,出现以下错误。

[Errno 110] Connection timed out
Sending Key to Server
Traceback (most recent call last):
   File "client.py", line 43, in <module>
       s.send(str(id))
socket.error: [Errno 32] Broken pipe

我尝试了以下解决方案 Broken Pipe errorHow to prevent Broken pipe error,但没有一个解决问题。

这是我的客户端和服务器代码

client.py

import socket
import os
import subprocess
from optparse import OptionParser
from random import randint 

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   
    print "Socket has been successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

# The Manager Address and port 

host_ip='192.168.43.123'
port =10106

# Generates a random number say xxxx then its client id becomes 'clxxxx' and home directory made at the server as '/home/clxxxx' with permissions 700
def random_with_N_digits(n):
    range_start = 10**(n-1)
    range_end = (10**n)-1
    return randint(range_start, range_end)

id=random_with_N_digits(4)
id="cl"+ str(id)

# Looks for a public key in .ssh folder if temp.pub not present. If not found generates a ssh public private key and sends it to manager which then copies it to the server
subprocess.call(["bash","keygen.sh"])


#s = socket.socket()

try:
    s.connect((host_ip,port))
    print "the socket has successfully connected to Backup Server IP == %s" %(host_ip)

except socket.error as err:
    print err

f = open('temp.pub','r')
print "Sending Key to Server"

j = "-"
s.send(str(id))
l=f.read(8192)
while(l):
    print 'Sending...'
    s.send(l)
    l = f.read(8192)

try:
    client_id=s.recv(1024)
    data=s.recv(12)
    ip=s.recv(24)
    print  client_id,
    print  data, ip
except:
    print "An Unknown Error Occurred!"

f.close()

# Writes the parameters of client in the file 'backup_dir.txt'
with open('backup_dir.txt','w') as the_file:
    the_file.write(client_id)
    the_file.write('\n')
    the_file.write(data)
    the_file.write('\n')
    the_file.write(ip)
    the_file.write('\n')
f.close()


s.close()

server.py

import socket
import subprocess
import os

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   
    print "Socket has been successfully created"
except socket.error as err:
    print "socket creation failed with error %s" %(err)

port = 10106

s.bind(('', port))
print("socket binded to %s port" %port)

s.listen(10)
print("socket is listening")

while(True):
    print("waiting for a connection")
    c, addr = s.accept()
    print("Got a connection from", addr,c)
    clientID =(c.recv(8192))
    key =(c.recv(8192))
    print clientID
    print key

    with open("temp.pub", 'w') as fp:
        fp.write(key)
    note=subprocess.check_output("./test_user.sh "+ clientID, shell=True)
    note = str(note)
    print(len(note))
    flag, path, serverIP = note.split(":")
    print(flag)
    print(path)
    print(serverIP)
    if flag:
        c.send(clientID)
        c.send(path)
        c.send(serverIP)
        os.remove("temp.pub")
    else:
        c.send("Unable to add Client.")

如何解决此问题,以便客户端可以将数据发送到服务器而没有任何错误? 预先谢谢你。

1 个答案:

答案 0 :(得分:0)

错误已解决。
这是由于@RaymondNijland提到的防火墙问题,谢谢。
我在防火墙中添加了该规则,以允许以下端口用于套接字连接,并且可以正常工作。

sudo ufw allow 10106