Python套接字编程ConnectionRefusedError:[Errno 61]连接被拒绝

时间:2017-05-12 19:02:44

标签: python-3.x sockets

这是我的服务器程序代码:

import socket
soket = socket.socket(socket.AF_INET,socket.SOCK_STREAM)

HOST = "localhost"
PORT = 8011

soket.bind((HOST,PORT))
print("%s:%d server başlatıldı." % (HOST,PORT))
print("Kullanıcı bekleniyor.")
soket.listen(2)

baglanti,adres = soket.accept()
print("Bir bağlantı kabul edildi.", adres)

baglanti.send("Hoşgeldiniz efendim , hoşgeldiniz.")

data = baglanti.recv(1024)
print(data)

soket.close()

这是给客户的:

import socket
clientsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clientsocket.connect(("localhost", 8011))
clientsocket.send('hello')

虽然我首先启动服务器程序然后启动客户端程序,但是我收到此错误:

Traceback (most recent call last):
  File "/Users/Esat/Desktop/Python/Softwares/socketto copy.py", line 3,  in <module>
    clientsocket.connect(("localhost", 8011))
ConnectionRefusedError: [Errno 61] Connection refused

1 个答案:

答案 0 :(得分:0)

最好使用LAN(本地)IP,而不是localhost。您可以通过运行ipconfig(在Windows中)或ifconfig(在GNU / Linux或Mac OS中)来获取本地IP。您的本地IP应该类似于192.168.1.x