简体   繁体   中英

How can i handle [WinError 10057] error in python?

Exercise 1: Change the socket program socket1.py to prompt the user for the URL so it can read any web page. You can use split('/') to break the URL into its component parts so you can extract the host name for the socket connect call. Add error checking using try and except to handle the condition where the user enters an improperly formatted or non-existent URL.

import socket
url = input('name:')
word = url.split('/')
host = word[2]
print(host)
try:
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysock.connect(('host', 80))
    mysock.send(('GET '+url+' HTTP/1.0\r\n\r\n').encode())

except:
    print ("Try your best")

while True:
    data = mysock.recv(512)
    if len(data) < 1:
        break
    print(data.decode(),end='')

mysock.close()

OSError: [WinError 10057] A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using a sendto call) no address was supplied

would you please help me? why code return me this error? how can I resolve it without any other new function?

    mysock.connect(('host', 80))

Should be

    mysock.connect((host, 80))

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