Is it possible to read only first N bytes from the HTTP server using Linux command?

前端 未结 5 1419
梦如初夏
梦如初夏 2020-12-14 16:52

Here is the question.

Given the url http://www.example.com, can we read the first N bytes out of the page?

  • using wget, we can
相关标签:
5条回答
  • 2020-12-14 17:31

    I came here looking for a way to time the server's processing time, which I thought I could measure by telling curl to stop downloading after 1 byte or something.

    For me, the better solution turned out to be to do a HEAD request, since this usually lets the server process the request as normal but does not return any response body:

    time curl --head <URL>
    
    0 讨论(0)
  • 2020-12-14 17:40

    You can do it natively by the next curl command (no need to donwload whole document). According to culr man page:

    RANGES HTTP 1.1 introduced byte-ranges. Using this, a client can request to get only one or more subparts of a specified document. curl supports this with the -r flag.

    Get the first 100 bytes of a document:
        curl -r 0-99 http://www.get.this/
    
    Get the last 500 bytes of a document:  
        curl -r -500 http://www.get.this/
    
    `curl` also supports simple ranges for FTP files as well.
    Then you can only specify start and stop position.
    
    Get the first 100 bytes of a document using FTP:
        curl -r 0-99 ftp://www.get.this/README
    

    It works for me even with Java web app that deployed to GigaSpaces.

    0 讨论(0)
  • 2020-12-14 17:46
    curl <url> | head -c 499
    

    or

    curl <url> | dd bs=1 count=499
    

    should do

    Also there are simpler utils with perhaps borader availability like

        netcat host 80 <<"HERE" | dd count=499 of=output.fragment
    GET /urlpath/query?string=more&bloddy=stuff
    
    HERE
    

    Or

    GET /urlpath/query?string=more&bloddy=stuff
    
    0 讨论(0)
  • 2020-12-14 17:48

    You should also be aware that many HTTP/1.1 servers do not have this feature enabled, so that when you attempt to get a range, you'll instead get the whole document.

    You will have to get the whole web anyways, so you can get the web with curl and pipe it to head, for example.

    head

    c, --bytes=[-]N print the first N bytes of each file; with the leading '-', print all but the last N bytes of each file

    0 讨论(0)
  • 2020-12-14 17:50

    Make a socket connection. Read the bytes you want. Close, and you're done.

    0 讨论(0)
提交回复
热议问题