471,348 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

HTTP Protocol

Hi,

I am writing http protocol to get some data from servers. If i was using my localhost, getting replay from local and if want access other remote sites, i am getting error. Please reply it

My localhost client script:::

Expand|Select|Wrap|Line Numbers
  1. import httplib
  2. h = httplib.HTTP('localhost',80)
  3. h.putrequest('GET','')
  4. h.putheader('User-Agent','Lame Tutorial Code')
  5. h.putheader('Accept','text/html')
  6. h.endheaders()
  7.  
  8. errcode,errmsg, headers = h.getreply()
  9. print errcode,errmsg, headers
  10.  
  11.  
  12.  
  13. f = h.getfile() # Get file object for reading data
  14. data = f.read()
  15. print data
  16. f.close()
  17.  
  18.  
  19. the Reply getting from this::::
  20.  
  21.  
  22.  
  23.  403 Forbidden Date: Tue, 18 Sep 2007 05:20:36 GMT
  24. Server: Apache/2.0.52 (Red Hat)
  25. Accept-Ranges: bytes
  26. Content-Length: 3985
  27. Connection: close
  28. Content-Type: text/html; charset=UTF-8
  29.    And some Html script
  30.  
  31.  
  32. If I want to access other sites:::
  33.  
  34.        import httplib
  35. h = httplib.HTTP('http://Google.com',80)
  36. h.putrequest('GET','')
  37. h.putheader('User-Agent','Lame Tutorial Code')
  38. h.putheader('Accept','text/html')
  39. h.endheaders()
  40.  
  41. errcode,errmsg, headers = h.getreply()
  42. print errcode,errmsg, headers
  43.  
  44. f = h.getfile() # Get file object for reading data
  45. data = f.read()
  46. print data
  47. f.close()
  48.  
I got the error like:::

Traceback (most recent call last):
File "c.py", line 6, in ?
h.endheaders()
File "/usr/lib/python2.3/httplib.py", line 712, in endheaders
self._send_output()
File "/usr/lib/python2.3/httplib.py", line 597, in _send_output
self.send(msg)
File "/usr/lib/python2.3/httplib.py", line 564, in send
self.connect()
File "/usr/lib/python2.3/httplib.py", line 532, in connect
socket.SOCK_STREAM):
socket.gaierror: (-2, 'Name or service not known')


How can I access Remote sites using http protocol . I did'nt write server script.


Thanks And Regards
Allavarapu
Sep 18 '07 #1
5 5549
bartonc
6,596 Expert 4TB
This module defines classes which implement the client side of the HTTP and HTTPS protocols. It is normally not used directly -- the module urllib uses it to handle URLs that use HTTP and HTTPS.
11.4 urllib -- Open arbitrary resources by URL
Sep 18 '07 #2
11.4 urllib -- Open arbitrary resources by URL


I also tried for this url script::

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. import urllib
  4.  
  5. proxies = proxies={'http': '192.168.0.2:80'}
  6.  
  7. f = urllib.urlopen("http://www.google.com",proxies={})
  8. buf = f.read()
  9. print buf
  10. f.close()    
  11.  
  12.  
Hear also it say the same Like
........
IOError: [Errno socket error] (-2, 'Name or service not known')

If I use IP addres Insted of this www.google.com Like::

Expand|Select|Wrap|Line Numbers
  1. import urllib
  2.  
  3. proxies = proxies={'http': '192.168.0.2:80'}
  4.  
  5. f = urllib.urlopen("http://209.85.135.147",proxies={})
  6. buf = f.read()
  7. print buf
  8. f.close()
  9.  
  10.  
I am getting different error Like:::

raceback (most recent call last):
File "url.py", line 5, in ?
f = urllib.urlopen("http://209.85.135.147",proxies={})
File "/usr/lib/python2.3/urllib.py", line 76, in urlopen
return opener.open(url)
File "/usr/lib/python2.3/urllib.py", line 181, in open
return getattr(self, name)(url)
File "/usr/lib/python2.3/urllib.py", line 297, in open_http
h.endheaders()
File "/usr/lib/python2.3/httplib.py", line 712, in endheaders
self._send_output()
File "/usr/lib/python2.3/httplib.py", line 597, in _send_output
self.send(msg)
File "/usr/lib/python2.3/httplib.py", line 564, in send
self.connect()
File "/usr/lib/python2.3/httplib.py", line 548, in connect
raise socket.error, msg
IOError: [Errno socket error] (110, 'Connection timed out')


Please Help for this.

Thanks
Allavarapu
Sep 19 '07 #3
bartonc
6,596 Expert 4TB
I also tried for this url script::

Expand|Select|Wrap|Line Numbers
  1. import urllib
  2.  
  3. proxies = proxies={'http': '192.168.0.2:80'}
  4.  
  5. f = urllib.urlopen("http://www.google.com",proxies={})
  6. buf = f.read()
  7. print buf
  8. f.close()    
Please Help for this.

Thanks
Allavarapu
You have a small confusion over python syntax here. Try it this way:
Expand|Select|Wrap|Line Numbers
  1. import urllib
  2.  
  3. proxies = {'http': '192.168.0.2:80'}
  4.  
  5. f = urllib.urlopen("http://www.google.com", proxies=proxies)
  6. buf = f.read()
  7. f.close()
  8. print buf    
  9.  
  10.  
Sep 19 '07 #4
You have a small confusion over python syntax here. Try it this way:
Expand|Select|Wrap|Line Numbers
  1. import urllib
  2.  
  3. proxies = {'http': '192.168.0.2:80'}
  4.  
  5. f = urllib.urlopen("http://www.google.com", proxies=proxies)
  6. buf = f.read()
  7. f.close()
  8. print buf    
  9.  
  10.  

Hello

I tried your suggested code. I got the error::

Traceback (most recent call last):
File "first.py", line 5, in <module>
f = urllib.urlopen("http://www.google.com", proxies=proxies)
File "/usr/local/lib/python2.5/urllib.py", line 82, in urlopen
return opener.open(url)
File "/usr/local/lib/python2.5/urllib.py", line 185, in open
return self.open_unknown_proxy(proxy, fullurl, data)
File "/usr/local/lib/python2.5/urllib.py", line 204, in open_unknown_proxy
raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
IOError: [Errno url error] invalid proxy for http: '192.168.0.2:80'

Please Help
Thanks for sending reply

Divakar
Sep 19 '07 #5
Hello

I tried your suggested code. I got the error::

Traceback (most recent call last):
File "first.py", line 5, in <module>
f = urllib.urlopen("http://www.google.com", proxies=proxies)
File "/usr/local/lib/python2.5/urllib.py", line 82, in urlopen
return opener.open(url)
File "/usr/local/lib/python2.5/urllib.py", line 185, in open
return self.open_unknown_proxy(proxy, fullurl, data)
File "/usr/local/lib/python2.5/urllib.py", line 204, in open_unknown_proxy
raise IOError, ('url error', 'invalid proxy for %s' % type, proxy)
IOError: [Errno url error] invalid proxy for http: '192.168.0.2:80'

Please Help
Thanks for sending reply

Divakar



Hello bartonc,

It is problem with Firewall. It is blocking me to access the network. If i run the script from public IP it's working.

But i have one doubt about this, i can access websites form mozilla firefox or other browser. How can i run my script with in my private network, there is any possiblety to access http protocol.


Thanks
divakar
Sep 20 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Justin Malloy | last post: by
5 posts views Thread by Henrik | last post: by
reply views Thread by Helen | last post: by
10 posts views Thread by Mat | last post: by
1 post views Thread by Khadim | last post: by
reply views Thread by allavarapu | last post: by
1 post views Thread by welcomestocontact | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.