簡體   English   中英

使用Python套接字的意外HTML錯誤響應

[英]Unexpected HTML Error Response using Python Sockets

我正在遵循一個教程,該教程使用此處找到的Python套接字從網頁檢索HTML。

我有一個在Ubuntu來賓上運行的Apache服務器,該來賓為我的網站托管一個HTML文件。 我已經在主機操作系統的/etc/hosts文件中創建了DNS條目,以使該網頁可訪問url vulnerable

我已驗證可以從主機上的Web瀏覽器訪問網頁。

我對代碼做了一些修改以適合我的情況。

import socket
import sys  # needed for sys.exit()

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
    print ("Failed to initialize socket")
    sys.exit()

print ("Socket initialized")

host = "vulnerable"
port = 80

try:
    remote_ip = socket.gethostbyname(host)
except socket.gaierror as e:
    print ("Hostname could not be resolved. Exiting")
    sys.exit()

s.connect((remote_ip, port))

print ("Socket Connected to " +host+ " on IP " + remote_ip)

message = "GET /HTTP/1.1\r\n\r\n".encode('utf-8')   # convert string to byte message, otherwise won't send

try:
    s.sendall(message)
except socket.error:
    print ("Send Failed")
    sys.exit()

print ("Message sent successfully")

reply = s.recv(4096)
print (reply)

當我嘗試從網站檢索HTML時,出現意外錯誤404。

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>404 Not Found</title>
</head><body>
<h1>Not Found</h1>
<p>The requested URL /HTTP/1.1 was not found on this server.</p>
<hr>
<address>Apache/2.4.10 (Ubuntu) Server at 127.0.1.1 Port 80</address>
</body></html>

我不明白為什么可以從Web瀏覽器訪問網頁時沒有出現此404錯誤的原因。

這是你的問題

message = "GET /HTTP/1.1\\r\\n\\r\\n".encode('utf-8')

您需要指定要檢索的資源-這就是為什么您要接收的原因The requested URL /HTTP/1.1 was not found on this server作為來自Web服務器的響應。 您正在請求/HTTP/1.1資源,該資源未找到並導致404響應。

message確保您指定要檢索的資源,例如

message = "GET /index.html HTTP/1.1\\r\\n\\r\\n".encode('utf-8')

暫無
暫無

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

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