【问题标题】:Getting 400 Bad Request error with Python Socket GET request使用 Python Socket GET 请求获取 400 Bad Request 错误
【发布时间】:2021-03-05 21:38:23
【问题描述】:

对于学校作业,我需要发送 GET 请求并仅使用套接字接收数据。无论我如何尝试格式化 GET 请求,我都会收到 HTTP/1.1 400 Bad Request 错误。

这是我的代码(如果很糟糕请见谅,这是我第一个 python 项目):

import socket
import sys

try:
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    print ("Creation successful")
except socket.error as err:
    print ("Creation unsuccessful. Error: %s" %(err))
    
port = 80

try:
    ip = socket.gethostbyname('gaia.cs.umass.edu')
except socket.gaierror:
    print("Error resolving host")
    sys.exit()

s.connect((ip, port))

print("Connection successful")
print("Connected to %s" %(ip))       


try:
    s.sendall("GET wireshark-labs/HTTP-ethereal-lab-file3.html HTTP/1.1\r\nHost: gaia.cs.umass.edu\r\n\r\n".encode())
    while True:
        data = s.recv(1024)
        print("Data received")
        print(data.decode('UTF-8'))
        if not data:
            print("No Data")
            s.close()
            break
except socket.gaierror:
    print("Error sending data")
    sys.exit()

这是我收到的错误:

HTTP/1.1 400 Bad Request

Date: Mon, 23 Nov 2020 07:04:03 GMT

Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.12 mod_perl/2.0.11 Perl/v5.16.3

Content-Length: 226

Connection: close

Content-Type: text/html; charset=iso-8859-1



<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>

试图让这个 GET 请求工作让我发疯了,提前感谢你们给我的帮助。

【问题讨论】:

    标签: python http get


    【解决方案1】:

    抱歉,不能将其写为注释,因为必须包含输出。我正在尝试使用您的代码,当我更改 host 和 wireshark-labs 部分时......发生了一件奇怪的事情。我把它改成 s.sendall(b"GET / HTTP/1.1\r\nHost: www.cnn.com\r\n\r\n") 了解这是如何工作的...... 我得到的回应是......

    Creation successful
    Connection successful
    Connected to 128.119.245.12
    Data received
    HTTP/1.1 200 OK
    Date: Mon, 23 Nov 2020 07:48:51 GMT
    Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.12 mod_perl/2.0.11 Perl/v5.16.3
    Last-Modified: Tue, 01 Mar 2016 18:57:50 GMT
    ETag: "a5b-52d015789ee9e"
    Accept-Ranges: bytes
    Content-Length: 2651
    Content-Type: text/html; charset=UTF-8
    
    <html>
    <head>
    <title>Computer Network Research Group - UMass Amherst
    </title>
    </head>
    <body bgcolor="#ffffff">
    <center>
    <p><img 
    src="cnrg_imap.jpg" 
    border="0" usemap="#cnrg_imapMAP">
    <map name="cnrg_imapMAP">
    <area coords="290,177,407,205" shape="rect" href="/networks/resources/index.html">
    <area coords="163,178,275,205" shape="rect" href="/networks/education/index.html">
    <area coords="62,165,145,191" shape="rect" href="/search.html">
    <area coords="6,63,157,90" shape="rect" href="/networks/collaborations.html">
    <area coords="64,7,146,34" shape="rect" href="/networks/people.html">
    <area coords="163,7,270,33" shape="rect" href="/networks/research.html">
    <area coords="288,6,417,33" shape="rect" 
    href="/networks/
    Data received
    publications.html">
    </map>
    <P>
    <BR>
    <BR>
    <P>
    <table width=100% border=0 cellpadding=0 cellspacing=0>
      <tr>
        <td width=60>&nbsp;</td>
        <td width="10%">&nbsp;</td>
        <td width="70%"> 
    <font face="Helvetica,Arial,TimesNewRoman" color="#000000">
    <h4>
    The <a href="/index.html"> Computer Networks Research Group </a> in the <a href = "https://www.cics.umass.edu/"> School of Information and Computer Sciences</a> at
    the <a href = "http://www.umass.edu/"> University of Massachusetts, Amherst </a> is led by Professors <a href = "http://www-net.cs.umass.edu/kurose/index.html">Jim Kurose </a>, and <a href = "/personnel/towsley.html">Don Towsley</a>. 
    Our research spans a broad range of topics in networking, including network protocols and architecture, modeling and analysis, sensor networks, wireless networks, and network measurement. We seek a principled understanding of new and emerging areas through a complementary mix of theoretical and applied experimental research. 
      </td>
      <td width="10%">&nbsp;</td>
     </
    Data received
    tr>
    </table>
    
     
    </h4>
    
    </font>
    <p>
    <!-- QUP Button & QualNet link markup start -->
    <a href="http://www.scalable-networks.com/customers/qup/index.php">
    <img src="http://www.scalable-networks.com/images/qupmember.gif" border=0
    alt="QualNet  Network Simulator University Program"></a>
    <a href="http://www.scalable-networks.com">QualNet Network Simulator</a>
    <!-- End of QUP Button HTML & QualNet link markup -->
    <p>
    <p>
    <center>
    <font size="1">
    <a href="/networks/people.html"> PEOPLE |</a> 
    <a href="/networks/research.html"> RESEARCH |</a> 
    <a href="http://www-net.cs.umass.edu/networks/publications.html"> PUBLICATIONS |</a> 
    <a href="/networks/collaborations.html"> COLLABORATIONS |</a> 
    <a href="http://gaia.cs.umass.edu/search.html"> SEARCH |</a> 
    <a href="/networks/education/index.html"> EDUCATION |</a> 
    <a href="/networks/resources/index.html"> 
    RESOURCES</a> 
    <p>
    
    </font>
    </center>
    </body>
    </html>
    
    Data received
    
    No Data
    
    Process finished with exit code 0
    

    您正在寻找什么。似乎sendall 命令中的host 值在某处被覆盖。不知道wireshark-labs 部分有多少。希望信息有所帮助。此外,当我将您的代码粘贴到 pycharm 中时,它会在此处发出警告 s.connect((ip, port)) s could be undefined。 P.S:- 我不是socket 编程方面的专家。如有错误请见谅。只是想帮忙。

    【讨论】:

      猜你喜欢
      • 2013-10-02
      • 2021-10-28
      • 2014-08-22
      • 1970-01-01
      • 2019-06-01
      • 2017-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多