我试图创建一个使用下面的代码蟒蛇一个简单的Web服务器。然而,当我运行这段代码,我面对这样的错误:
ConnectionRefusedError:WinError 10061]无连接可以作出,因为目标机器积极地拒绝它
这一点,实在值得一提的是我已经尝试了一些解决方案建议的代理服务器设置在操纵internet选项。我已经运行的代码都在取消选中和代理服务器的确认情况,但无法解决的问题。能否请您指导我这个?
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))
套接字连接的标准的例子
服务器和客户端
在怠速运转这个
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()
运行之后,在你空闲执行该命令,看看反应
>>> client(HOST,PORT,'Hello server, client sending greetings')
This is the server's hostname: localhost
Hello server, client sending greetings
>>>
如果你尝试做服务器端口60000,但不同的端口上发送消息,您会收到同样的错误在你的OP。这表明,该端口上没有服务器监听连接