[英]Python socket getting the error “connection refused error 111”
I have a problem connecting with the socket.我在连接套接字时遇到问题。 I wrote a code whose purpose is to get the serial number of the hard disk from a client and send it to the server.我写了一段代码,其目的是从客户端获取硬盘的序列号并将其发送到服务器。 If I run the server and the client code on my local machine, it works fine.如果我在本地机器上运行服务器和客户端代码,它工作正常。 When I try to run the server on the real server and the client on the real client (2 different machines) I'm getting the error: “Connection refused error 111”当我尝试在真实服务器上运行服务器并在真实客户端(2 台不同的机器)上运行客户端时,出现错误:“连接被拒绝错误 111”
This is my client code:这是我的客户代码:
#!/usr/bin/python3
import os, socket
from time import sleep
def serialNumber():
"""Find the product serial number"""
serialtest = "smartctl -a -i /dev/sda2 > /root/Desktop/serialTest.txt"
grepp = "grep 'Serial Number:' /root/Desktop/serialTest.txt > /root/Desktop/NewserialTest.txt"
sedd = "sed -i 's/Serial Number: //g' /root/Desktop/NewserialTest.txt"
os.system(serialtest)
os.system(grepp)
os.system(sedd)
try:
with open (r"/root/Desktop/NewserialTest.txt","r") as data:
global newserial
newserial = data.readline().strip()
except:
return "File not found!"
try:
os.rename(r'/root/Desktop/NewserialTest.txt',rf'/root/Desktop/{newserial}.txt')
os.remove(r"/root/Desktop/serialTest.txt")
except:
return "File not found!"
return ""
print(serialNumber())
try:
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip = socket.gethostname()
port = 5555
s.connect((ip,port))
except socket.error as e:
print(f"UNABLE to connect! you got error:\n{e}.")
exit(-1)
try:
with open(rf'/root/Desktop/{newserial}.txt', "rb") as fd:
toSend = fd.read()
s.send(toSend)
except socket.error as e:
print(f"you got error:\n{e}.")
This is my Server code:这是我的服务器代码:
#!/usr/bin/python3
import os, socket
from _thread import *
server_socket = socket.socket()
host = socket.gethostname()
port = 5555
try:
server_socket.bind((host, port))
except socket.error as e:
print(f"You have a error:\n{str(e)}")
print("\nWaiting for connection....\n")
server_socket.listen(100)
while True:
# Recive the serial number from the client
sc, address = server_socket.accept()
print(address)
f = open(r'/root/Desktop/LAB_Test/NewserialTest.txt' ,'wb') #open in binary
while (True):
l = sc.recv(1024)
f.write(l)
if not l:
break
f.close()
sc.close()
try:
with open (r'/root/Desktop/LAB_Test/NewserialTest.txt',"r") as data:
global newserial
newserial = data.readline().strip()
except:
print("File not found!")
os.rename(r'/root/Desktop/LAB_Test/NewserialTest.txt',rf'/root/Desktop/LAB_Test/{newserial}.txt')
what could be the problem?可能是什么问题呢?
I changed the bind to 0.0.0.0, and now it works.我将绑定更改为 0.0.0.0,现在它可以工作了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.