python中的套接字服务器拒绝连接 [英] Socket server in python refuses to connect

查看:127
本文介绍了python中的套接字服务器拒绝连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码使用 python 创建一个简单的 Web 服务器.但是,当我运行这段代码时,我遇到了这个错误:

<块引用>

ConnectionRefusedError: [WinError 10061] 无法建立连接因为目标机器主动拒绝了

值得一提的是,我已经尝试了一些建议在 Internet 选项中操作代理设置的解决方案.我已经在代理服务器的未勾选和确认的情况下运行了代码,但无法解决问题.你能指导我完成这个吗?

导入系统导入套接字服务器进口插座主机名 = socket.gethostname()print("这是主机名:" + 主机名)端口号 = 60000soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)soc.connect((主机名,端口号))

解决方案

socket连接标准示例

服务器 &客户

在你的 IDLE 中运行这个

导入时间进口插座进口螺纹HOST = 'localhost' # 标准环回接口地址(localhost)PORT = 60000 # 监听的端口(非特权端口 > 1023)定义服务器(主机,端口):s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.bind((主机,端口))s.听(1)为真:conn, addr = s.accept()数据 = conn.recv(1024)如果数据:打印(数据)数据 = 无时间.睡眠(1)打印('听...')定义客户端(主机,端口,消息):print("这是服务器的主机名:" + HOST)soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)soc.connect((HOST,PORT))soc.send(消息)soc.close()th=threading.Thread(target = server,args = (HOST,PORT))th.daemon = 真th.start()

运行此命令后,在您的 IDLE 中执行此命令并查看响应

<预><代码>>>>客户端(主机,端口,'你好服务器,客户端发送问候')这是服务器的主机名:localhost你好服务器,客户端发送问候>>>

如果您尝试使用端口 60000 执行服务器但在不同端口上发送消息,您将收到与您的 OP 相同的错误.这表明,在该端口上没有服务器侦听连接

I am trying to create a simple web server with python using the following code. However, When I run this code, I face this error:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

It worths mentioning that I have already tried some solutions suggesting manipulation of proxy settings in internet options. I have run the code both in the unticked and the confirmed situation of the proxy server and yet cannot resolve the issue. Could you please guide me through this ?

import sys
import socketserver
import socket

hostname = socket.gethostname()
print("This is the host name:  " + hostname)

port_number = 60000

soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((hostname,port_number))

解决方案

Standard EXAMPLE of socket connection

SERVER & CLIENT

run this in your IDLE

import time
import socket
import threading
HOST = 'localhost'  # Standard loopback interface address (localhost)
PORT = 60000       # Port to listen on (non-privileged ports are > 1023)

def server(HOST,PORT):
    s =  socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)

    while True:
        conn, addr = s.accept()
        data = conn.recv(1024)
        if data:
            print(data)
            data = None
        time.sleep(1)
        print('Listening...')


def client(HOST,PORT,message):            
    print("This is the server's hostname:  " + HOST)


    soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    soc.connect((HOST,PORT))
    soc.send(message)
    soc.close()

th=threading.Thread(target = server,args = (HOST,PORT))
th.daemon = True
th.start()

After running this, in your IDLE execute this command and see response

>>> client(HOST,PORT,'Hello server, client sending greetings')
This is the server's hostname:  localhost
Hello server, client sending greetings
>>> 

If you try to do server with port 60000 but send message on different port, you will receive the same error as in your OP. That shows, that on that port is no server listening to connections

这篇关于python中的套接字服务器拒绝连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆