简体   繁体   中英

Python, socket.error: [Errno 10049]

Working on a base for a simple chat client, and got the following error:
socket.error: [Errno 10049] The requested address is not valid in its context

The code is:

from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT))
i = True
while i is True:
    msg = raw_input("Write A MSG: ")
    s.send(msg)
    print "Awaiting reply"
    reply = s.recv(1024)
    print "Recived: ", repr(reply)

s.close()

Thanks for helping.

The error is:

...
s.connect((HOST, PORT))

And it is because HOST = "" . You may use HOST = "" when binding sockets. But when connecting, you should use HOST = "localhost" or HOST = "someaddr.com" .

IP address ( HOST ) is not correct. If you want to access it from local computer you can use '127.0.0.1' or 'localhost' . To access from anywhere use '0.0.0.0' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM