简体   繁体   English

尝试使用python连接到TCP服务器时拒绝连接

[英]Getting connection refused when trying to connect to a tcp server with python

I've written two programs according to a guide on sockets in python. 我根据python套接字指南编写了两个程序。 I'm using a rasbperry pi 3 as a client, and a regular linux ubuntu computer as a server. 我正在使用rasbperry pi 3作为客户端,并使用常规的Linux ubuntu计算机作为服务器。 this is the server software: 这是服务器软件:

import socket
import sys

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

server_address = ('localhost', 15000)
print("starting up on %s port %s" % server_address, file=sys.stderr)
sock.bind(server_address)

# Listen for incoming connections
sock.listen(1)

while True:
    # Wait for a connection
    print("waiting for a connection", file=sys.stderr)
    connection, client_address = sock.accept()

try:
        print("connection from ", client_address, file=sys.stderr)

    # Receive the data in small chunks and retransmit it
    while True:
        data = connection.recv(16)
        print("received %s" % data, file=sys.stderr)
        if data:
            print("sending data back to the client", file=sys.stderr)
            connection.sendall(data)
        else:
            print("no more data from ", client_address, file=sys.stderr)
            break

finally:
        # Clean up the connection
        connection.close()

and this is the client software: 这是客户端软件:

import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Connect the socket to the port where the server is listening
server_address = ('192.168.18.250', 15000)
print("connecting to %s port %s" % server_address, file=sys.stderr)
sock.connect(server_address)

try:
# Send data
message = "This is the message.  It will be repeated."
print("sending %s" % message, file=sys.stderr)
sock.sendall(message)

# Look for the response
amount_received = 0
amount_expected = len(message)

while amount_received < amount_expected:
    data = sock.recv(16)
    amount_received += len(data)
    print("received %s" % data, file=sys.stderr)

finally:
    print("closing socket")
    sock.close()

this is the output on the server: 这是服务器上的输出:

peter@GIGAS:~/thermServer$ python3 thermServer.py
starting up on localhost port 15000
waiting for a connection

and this is the output on the raspberry pi: 这是树莓派的输出:

pi@raspberrypi:~ $ python3 thermClient.py
connecting to 192.168.18.250 port 15000
Traceback (most recent call last):
  File "thermClient.py", line 10, in <module>
    sock.connect(server_address)
ConnectionRefusedError: [Errno 111] Connection refused

I have forwarded the port in my router, but as this is internal traffic that shouldn't matter, did I miss adding something in the server that opens the port properly or do I need to fiddle with something outside of the project in my linux machine? 我已经在路由器中转发了端口,但由于这是内部流量,所以不重要,我是否错过了在服务器中添加一些可以正确打开端口的东西,还是我需要在Linux机器中摆弄项目之外的东西? ?

In your server code you have: 在服务器代码中,您具有:

server_address = ('localhost', 15000)

This sets up a listener on ipnumer 127.0.0.1. 这将在ipnumer 127.0.0.1上设置一个侦听器。 This 'localhost' ip cannot be contacted from clients outside of this server. 无法从该服务器外部的客户端联系此“ localhost” IP。

If you want your server to listen on all assigned ip-adresses, use: 如果希望服务器在所有分配的IP地址上进行侦听,请使用:

server_address = ('0.0.0.0', 15000)

When you create a socket, bound to the localhost address, you create a socket listening on the "loopback interface", ie 127.0.0.1. 创建绑定到localhost地址的套接字时,将创建一个侦听“回送接口”(即127.0.0.1)的套接字。 You appear to be trying to connect to the server by the local IP - 192.168.18.250, which is assigned to a different interface (generally the one connected to your LAN). 您似乎正在尝试通过本地IP-192.168.18.250连接到服务器,该IP已分配给另一个接口(通常是连接到LAN的接口)。 Because nothing is listening to port 15000 on that interface you get a connection refused. 由于没有人在该接口上监听端口15000,因此连接被拒绝。

You have two solutions here: either change the server to be listening to the correct interface (listening to 0.0.0.0 generally works here, although if it's a guaranteed static IP, use that instead), or change the client to try to connect to the loopback interface - try connect on 127.0.0.1 or "localhost" 您在这里有两种解决方案:要么更改服务器以侦听正确的接口(虽然可以保证使用静态IP,但在这里通常听0.0.0.0即可,或者改为使用该客户端尝试连接到该接口)。回送接口-尝试在127.0.0.1或“ localhost”上连接

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

相关问题 Errno 61:尝试连接到python服务器时出现连接拒绝错误 - Errno 61: Connection refused error when trying to connect to python server 尝试连接到蓝牙服务器时连接被拒绝 - Connection refused when trying to connect to bluetooth server 连接拒绝尝试连接到服务器 - Connection refused trying to connect to server 为什么我在尝试将我的 python 客户端连接到服务器时不断收到 [Erno 111] 连接被拒绝? - Why do i keep getting [Erno 111] connection refused while trying to connect my python client to server? 尝试连接到 mysql 服务器时连接被拒绝 - Connection refused while trying to connect to mysql server 从android tcp客户端到python tcp服务器的连接被拒绝 - Connection refused from android tcp client to python tcp server 在 Python 中使用 ftplib 将文件传输到 FTP 服务器时出现“连接被拒绝” - Getting "Connection refused" when transferring a file to FTP server with ftplib in Python Python Server / Java客户端“连接被拒绝:连接” - Python Server / Java Client “Connection refused: connect” 尝试使用Python连接到mysql时,2013年丢失了连接错误 - Getting 2013 lost connection error when trying to connect to mysql with Python 尝试向 Django REST 服务器发出请求时获取 net::ERR_CONNECTION_REFUSED - Getting net::ERR_CONNECTION_REFUSED when trying to make a request to Django REST Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM