Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 545157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:43:54+00:00 2026-05-13T10:43:54+00:00

import httplib def httpCode(theurl): if theurl.startswith(http://): theurl = theurl[7:] head = theurl[:theurl.find(‘/’)] tail =

  • 0
import httplib
def httpCode(theurl):
    if theurl.startswith("http://"): theurl = theurl[7:]
    head = theurl[:theurl.find('/')]
    tail = theurl[theurl.find('/'):]
    response_code = 0
    conn = httplib.HTTPConnection(head)
    conn.request("HEAD",tail)
    res = conn.getresponse()
    response_code = int(res.status)
    return response_code

Basically, this function will take a URL and return its HTTP code (200, 404, etc)
The error I got was:

Exception Value:  (-2, 'Name or service not known')

I must do it with this method. That is, I am usually passing in large video files. I need to get the “header” and get the HTTP code. I cannot download the file and then get the HTTP code, because it would take too long.

Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> def httpCode(theurl):
...     if theurl.startswith("http://"): theurl = theurl[7:]
...     head = theurl[:theurl.find('/')]
...     tail = theurl[theurl.find('/'):]
...     response_code = 0
...     conn = httplib.HTTPConnection(head)
...     conn.request("HEAD",tail)
...     res = conn.getresponse()
...     response_code = int(res.status)
...     print response_code
...
>>> httpCode('http://youtube.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in httpCode
  File "/usr/lib/python2.6/httplib.py", line 874, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 911, in _send_request
    self.endheaders()
  File "/usr/lib/python2.6/httplib.py", line 868, in endheaders
    self._send_output()
  File "/usr/lib/python2.6/httplib.py", line 740, in _send_output
    self.send(msg)
  File "/usr/lib/python2.6/httplib.py", line 699, in send
    self.connect()
  File "/usr/lib/python2.6/httplib.py", line 683, in connect
    self.timeout)
  File "/usr/lib/python2.6/socket.py", line 498, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
>>>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:43:54+00:00Added an answer on May 13, 2026 at 10:43 am

    Your code worked for me, and for one other person who commented. This implies that the URL you’re using is causing a problem with your parsing somehow. head and tail should both be examined in order to determine what it thinks the host is. For example:

    head = theurl[:theurl.find('/')]
    print head
    tail = theurl[theurl.find('/'):]
    print tail
    

    Once you can see what head and tail are, you can determine if it really should be able to resolve head. For example, what if the url was:

    http://myhost.com:8080/blah/blah
    

    It would fail because of the port number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import sys words = { 1 : 'one', 2 : 'two', 3 : 'three',
I need to import a csv file into Firebird and I've spent a couple
How can you import a foxpro DBF file in SQL Server?
I want to import an oracle dump into a different tablespace. I have a
How can you automatically import the latest build/revision number in subversion? The goal would
Are there any import and export tools that would let us move projects into
I need to import largish (24MB) text files into a MySQL table. Each line
When used like this: import static com.showboy.Myclass; public class Anotherclass{} what's the difference between
I need to import a large CSV file into an SQL server. I'm using
I'm writing an import utility that is using phone numbers as a unique key

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.