PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, October 22, 2022

[FIXED] Why am I getting socket.gaierror?

 October 22, 2022     python, sockets     No comments   

Issue

I'm trying to access and show data from http://data.pr4e.org/romeo.txt . Here's my code:

import socket         
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  
mysock.connect(('data.pr4e', 80))  
cmd = 'GET http://data.pr4e.org/romeo.txt HTTP/1.0\n\n'.encode()
mysock.send(cmd)    

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

I'm getting this error message :

socket.gaierror: [Errno 11001] getaddrinfo failed. 

I saw on another question that this means I do not have a valid host name, but I'm not sure why the host name is invalid.


Solution

The error is because you are passing the wrong host name to connect(). You need to change 'data.pr4e' to 'data.pr4e.org'.

Also, your request data is malformed, too. You need to remove the scheme and hostname from the request, and use \r\n instead of \n.

Try this:

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


Answered By - Remy Lebeau
Answer Checked By - Mildred Charles (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing