簡體   English   中英

我在這個 python 練習中遇到了一些麻煩

[英]I am having some trouble in this python exercise

我對這個練習有問題,這是練習:

更改您的套接字程序,使其計算字符數,它已接收並在顯示 3000 個字符后停止顯示任何文本。 程序應該檢索整個文檔並計算總字符數並在文檔末尾顯示字符數計數。

原始代碼:

import socket
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
mysock.connect(('data.pr4e.org', 80))
cmd = 'GET http://data.pr4e.org/romeo.txt 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(),end='')
mysock.close()

我的代碼:

import socket
url=input("Enter a URL :\n")
count=0
host_name=url.split("/")[2]
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    mysock.connect((host_name, 80))
    cmd=('GET url HTTP/1.0\n\n'.encode())
    mysock.send(cmd)
except:
    print("Enter a valid URl")
exit()


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

我的輸出:

Enter a URL :
http://www.py4inf.com/code/romeo.txt
HTTP/1.1 400 Bad Request
Server: nginx
Date: Mon, 06 May 2019 10:29:37 GMT
Content-Type: text/html
Content-Length: 166
Connection: close

<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx</center>
</body>
</html>

我收到一條錯誤消息“400 Bad request”

誰能幫幫我,謝謝

這段代碼對我有用:

import socket
from urllib.parse import urlsplit

url = urlsplit('http://www.py4inf.com/code/romeo.txt')
host = url.hostname
path = url.path

request = f'GET {path} HTTP/1.1\nHost: {host}\n\n'.encode('utf-8')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 80))
s.send(request)
result = s.recv(10000)
while (len(result) > 0):
    print(result.decode('utf-8'))

這是上面代碼的輸出:

 HTTP/1.1 200 OK
 Content-Type: text/plain
 Content-Length: 167
 Connection: keep-alive
 Keep-Alive: timeout=15
 Date: Thu, 09 May 2019 08:49:50 GMT
 Server: Apache
 Last-Modified: Fri, 04 Dec 2015 19:05:04 GMT
 ETag: "a7-526172f58b800"
 Accept-Ranges: bytes
 Cache-Control: max-age=604800, public
 Access-Control-Allow-Origin: *
 Access-Control-Allow-Headers: origin, x-requested-with, content-type
 Access-Control-Allow-Methods: GET

 But soft what light through yonder window breaks
 It is the east and Juliet is the sun
 Arise fair sun and kill the envious moon
 Who is already sick and pale with grief

編輯:這是您更正的原始代碼:

import socket
from urllib.parse import urlsplit

url = urlsplit(input("Enter a URL: ").strip())
count = 0
host = url.hostname
path = url.path
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    print(f'Connecting to {host} and fetching {path}')
    s.connect((host, 80))
    cmd = f'GET {path} HTTP/1.1\nHost: {host}\n\n'.encode('utf-8')
    s.send(cmd)
except:
    print("Enter a valid URl")
    exit()

while True:
    data = s.recv(512)
    count += len(data)
    if len(data) < 1:
        break
    print(data.decode())

s.close()

這是我使用上述代碼得到的輸出:

Enter a URL: http://www.py4inf.com/code/romeo.txt
Connecting to www.py4inf.com and fetching /code/romeo.txt
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 167
Connection: keep-alive
Keep-Alive: timeout=15
Date: Thu, 09 May 2019 15:30:16 GMT
Server: Apache
Last-Modified: Fri, 04 Dec 2015 19:05:04 GMT
ETag: "a7-526172f58b800"
Accept-Ranges: bytes
Cache-Control: max-age=604800, public
Access-Control-Allow-Origin: *
Access-Control-Allow-Headers: origin, x-requested-with, content-type
Access-Control-Allow-Methods: GET

But soft what light through yonder window breaks
It is the east and Juliet 
is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

試試這個,在 Python 3 上運行

import socket

url = input("Enter a URL :")
try:
    host_name = url.split("/")[2]
    mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    mysock.connect((host_name, 80))
    s = 'GET '+url+' HTTP/1.0\r\n\r\n'
    cmd = s.encode()
    mysock.send(cmd)
except:
    print("Enter a valid URl")
    exit()

count=0
while True:
    data = mysock.recv(512)
    for c in data:
        count += 1
    if count > 3000 or len(data) < 1:
        break
    #print(data.decode())
print(count)
mysock.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM