gpt4 book ai didi

python - 尝试从网页读取数据时的回溯

转载 作者:行者123 更新时间:2023-12-03 12:08:58 26 4
gpt4 key购买 nike

# -*- coding: utf-8 -*-
"""
Created on Sat Aug 26 17:31:06 2017

@author: Pavan Vallapureddy
"""
"""
Write a program 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.
"""

import socket

url = input("Enter url: ")
port = int(input("Enter port: "))
urlSplit = url.split("/")
host = urlSplit[2]

mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect((host, port))
cmd = "GET " + url + " HTTP/1.0\r\n\r\n".encode
mysock.send(cmd)

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

输入网址: http://data.pr4e.org/romeo.txt
输入端口:80
追溯(最近一次通话):
在第17行的文件“exercise1.py”
cmd =“GET” + url +“HTTP/1.0\r\n\r\n” .encode
TypeError:必须为str,而不是buildin_function_or_method

最佳答案

您必须为字符串实例encode()调用方法cmd:

cmd = "GET " + url + " HTTP/1.0\r\n\r\n"
mysock.send(cmd.encode())

关于python - 尝试从网页读取数据时的回溯,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45895892/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com