How to get html code using python sockets

demogorgon

So I'm trying to get the source code of google using only python sockets and not any other libraries such as urllib. I don't understand why my GET request isn't working, I tried all possible methods. This is the code I have, it's pretty small and I don't wanna get too much details. Just looking for a protocol that's used to get source codes. I assumed it would be the GET method but it doesn't work. I need a response that resembles urllib.request but using python sockets only.

  • If I pass "https://www.google.com" to socket.gethostbyname(), it fails on the getaddrinfo.
  • Also when I try to GET request from python.org, the while loop never ends.


import socket;

s=socket.socket();

host=socket.gethostbyname("www.google.com");

port=80;

send_buf="GET / \r\n"\
        "Host: www.google.com\r\n";

s.connect((host, port));

s.sendall(bytes(send_buf, encoding="utf-8"));

data="";

part=None;

while( True ):

    part=s.recv(2048);

    data+=str(part, "utf-8");

    if( part==b'' ):

        break;

s.close();
Alec

The following worked for me:

import socket
s=socket.socket()
host=socket.gethostbyname('www.google.com')
port=80
s.connect((host,port))
s.sendall("GET /\r\n")
val = s.recv(10000)
# Split off the HTTP headers
val = val.split('\r\n\r\n',1)[1]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the Domain name in Python sockets?

From Dev

how can I get html using python?

From Dev

how to get the text from the HTML using java code?

From Dev

how to receive large data from a broadcast server using python sockets

From Dev

Send messages using python sockets

From Dev

Using the browser as a client in python sockets

From Dev

Send messages using python sockets

From Dev

how to get nested tags with a loaded html page using python or php?

From Dev

How to get text object from a html table using selenium python

From Dev

Get html using Python requests?

From Dev

How to pass a variable in Python sockets?

From Dev

How to use sockets in Python and subprocess?

From Dev

Using DOM to get HTML code.

From Dev

Netty get response using standard sockets

From Dev

Using Python requests.get to parse html code that does not load at once

From Dev

How to get the part of the html code from html code in c#?

From Dev

How to get the part of the html code from html code in c#?

From Dev

How can i get the headers request from client side for sockets using NodeJS

From Dev

Cache a HTTP GET REQUEST in Python Sockets

From Dev

Sockets Programming with Python: Get the port of a server

From Dev

Sockets Programming with Python: Get the port of a server

From Dev

How do I get the HTML code using luacurl / libcurl / curl and Lua

From Dev

How to talk to UDP sockets with HTML5?

From Dev

How to display HTML code as HTML code using PHP

From Dev

How to display HTML code as HTML code using PHP

From Dev

Server(Python) - Client(Java) communication using sockets

From Dev

TCP handshake using python RAW sockets

From Dev

Mail Client in Python using sockets only(no smtplib)

From Dev

python chat with sockets using select model

Related Related

  1. 1

    How to get the Domain name in Python sockets?

  2. 2

    how can I get html using python?

  3. 3

    how to get the text from the HTML using java code?

  4. 4

    how to receive large data from a broadcast server using python sockets

  5. 5

    Send messages using python sockets

  6. 6

    Using the browser as a client in python sockets

  7. 7

    Send messages using python sockets

  8. 8

    how to get nested tags with a loaded html page using python or php?

  9. 9

    How to get text object from a html table using selenium python

  10. 10

    Get html using Python requests?

  11. 11

    How to pass a variable in Python sockets?

  12. 12

    How to use sockets in Python and subprocess?

  13. 13

    Using DOM to get HTML code.

  14. 14

    Netty get response using standard sockets

  15. 15

    Using Python requests.get to parse html code that does not load at once

  16. 16

    How to get the part of the html code from html code in c#?

  17. 17

    How to get the part of the html code from html code in c#?

  18. 18

    How can i get the headers request from client side for sockets using NodeJS

  19. 19

    Cache a HTTP GET REQUEST in Python Sockets

  20. 20

    Sockets Programming with Python: Get the port of a server

  21. 21

    Sockets Programming with Python: Get the port of a server

  22. 22

    How do I get the HTML code using luacurl / libcurl / curl and Lua

  23. 23

    How to talk to UDP sockets with HTML5?

  24. 24

    How to display HTML code as HTML code using PHP

  25. 25

    How to display HTML code as HTML code using PHP

  26. 26

    Server(Python) - Client(Java) communication using sockets

  27. 27

    TCP handshake using python RAW sockets

  28. 28

    Mail Client in Python using sockets only(no smtplib)

  29. 29

    python chat with sockets using select model

HotTag

Archive