简体   繁体   English

无法建立连接,因为目标机器主动拒绝它——我如何检查我的服务器是否正在运行并且客户端是否能够连接

[英]No connection could be made because the target machine actively refused it— How can i check if my server is running and client is able to connect

I tried to run a server and a client on local host but an error keeps popping up the sql part is okay i want to send the tuple from client to server then realised server isnt really working how can i run the server and client on the same machine at once and also what is this issue that keeps popping up Server Code:我试图在本地主机上运行一个服务器和一个客户端,但一个错误不断弹出 sql 部分没问题我想将元组从客户端发送到服务器然后意识到服务器不能正常工作我怎样才能同时运行服务器和客户端一次机器,还有这个不断弹出服务器代码的问题是什么:

import socket
import psycopg2 as sq

conn= sq.connect(database="metro", user = "postgres", password = "12345678",host="localhost", port="5432")
cur = conn.cursor()
try:
    s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print("Socket creation successful")
except:
    print("Socket creation Failed")

port=8050
s.bind(('localhost',port))
print("Socket is binded to", port)
s.listen(10)
print("Socket is listening")

while True:
    c, addr = s.accept()
    print("got connection from", addr)
    var=c.recv(2056).decode()           #Ticket data from CLIENT

    cur.execute("SELECT * from ticket") #Read DATABASE
    rows = cur.fetchall()
    flag=0
    for row in rows:                    #Check DATABASE FOR VERIFICATION
        if row==var:
            flag=1

    if flag==1:
        data= 'Gate opened'.encode()
    else:
        data= 'Ticket Not Found'.encode()

    c.send(data)
    c.shutdown(SHUT_WR)
    s.shutdown(SHUT_WR)

Client Code:客户代码:

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #define obj

port = 8050 #port no.
s.connect(('192.168.0.120', port))   #connect to server
data=('1','101','123')
for dat in data:
    print(dat)
    s.send(dat.encode())

vari = s.recv(2056)
print(vari.decode())
s.close()

Traceback (most recent call last): File "C:\\Users\\welcome\\Desktop\\Python\\Metro\\Client.py", line 5, in s.connect(('192.168.0.120', port)) #connect to server ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it [Finished in 2.074s]回溯(最近一次调用最后一次):文件 "C:\\Users\\welcome\\Desktop\\Python\\Metro\\Client.py", line 5, in s.connect(('192.168.0.120', port)) #connect to server ConnectionRefusedError: [WinError 10061] 由于目标机器主动拒绝,无法建立连接 [2.074s 内完成]

I have noted some errors in your code.我注意到您的代码中有一些错误。

You may try 'localhost' or '127.0.0.1' in client to connect with server您可以在客户端尝试使用 'localhost' 或 '127.0.0.1' 来连接服务器
SHUT_WR should be replaced with socket.SHUT_WR SHUT_WR应替换为socket.SHUT_WR
You should surround network operations with try except block你应该用try except block 包围网络操作
And there are logically misaligned while loop in server and client.并且在服务器和客户端中存在逻辑错位的 while 循环。

I have mitigated these errors.我已经减轻了这些错误。 You can refer below code.你可以参考下面的代码。
I just tested this with dummy data, not with database connection.我只是用虚拟数据测试了这个,而不是数据库连接。

Server.py服务器.py

import socket
import psycopg2 as sq
try:
    conn= sq.connect(database="metro", user = "postgres", password = "12345678",host="localhost", port="5432")
    cur = conn.cursor()
    try:
        s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print("Socket creation successful")
    except:
        print("Socket creation Failed")

    port=8050
    s.bind(('localhost',port))
    print("Socket is binded to", port)
    s.listen(10)
    print("Socket is listening")

    while True:
        c, addr = s.accept()
        print("got connection from", addr)
        var=c.recv(2056).decode()           #Ticket data from CLIENT
        while var:
            cur.execute("SELECT * from ticket") #Read DATABASE
            rows = cur.fetchall()
            flag=0
            for row in rows:                    #Check DATABASE FOR VERIFICATION
                if row==var:
                    flag=1

            if flag==1:
                data= 'Gate opened'.encode()
            else:
                data= 'Ticket Not Found'.encode()
            c.send(data)
            var=c.recv(2056).decode()
        c.close()
except Exception as e:
    print(e)
finally:
    s.close()

client.py客户端.py

import socket
try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #define obj
    port = 8050 #port no.
    s.connect(('127.0.0.1', port))   #connect to server
    data=('1','101','123')
    for dat in data:
        print(dat)
        s.send(dat.encode())
        vari = s.recv(2056)
        print(vari.decode())
except Exception as e:
    print(e)
finally:
    s.close()

暂无
暂无

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

相关问题 Python客户端无法与ruby服务器通信。 我得到[Errno 10061]无法建立连接,因为目标计算机主动拒绝了它 - Python client can not communicate with ruby server. I get [Errno 10061] No connection could be made because the target machine actively refused it Errno 10061:无法建立连接,因为目标机器主动拒绝它(客户端-服务器) - Errno 10061 : No connection could be made because the target machine actively refused it ( client - server ) 无法连接到MySQL服务器,无法连接,因为被皮重的机器主动拒绝了它 - Can't connect to MySQL server, No connection could be made because the tared machine actively refused it Python-无法建立连接,因为目标计算机主动拒绝了它 - Python - No connection could be made because the target machine actively refused it Python:无法建立连接,因为目标机器主动拒绝它 - Python: No connection could be made because the target machine actively refused it WinError 10061 由于目标机器主动拒绝,无法建立连接 - WinError 10061 No connection could be made because the target machine actively refused it “[WinError 10061] 无法建立连接,因为目标机器主动拒绝它” - "[WinError 10061] No connection could be made because the target machine actively refused it" EmailMultiAlternatives无法建立连接,因为目标计算机主动拒绝了它 - EmailMultiAlternatives no connection could be made because the target machine actively refused it 无法建立连接,因为目标计算机主动拒绝它 - No connection could be made because the target machine actively refused it Django Python - 无法建立连接,因为目标机器主动拒绝它 - Django Python - No connection could be made because the target machine actively refused it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM