InfoSec Forums InfoSec Forums Programming Send get and post requests with httplib module in Python

This topic contains 0 replies, has 1 voice, and was last updated by do son do son 2 months, 1 week ago.

  • Author
    Posts
  • #3623
    do son
    do son
    Moderator
    • Topics:202
    • Comments:51
    • Super Member
    • ★★★★

    httplib 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.

    1.  GET requests in Python

      #!/usr/bin/python
      import httplib
      httpClient = None
      try:
           httpClient = httplib.HTTPConnection(‘localhost’, 80, timeout=30)
           httpClient.request(‘GET’, ‘/test.php’)     response = httpClient.getresponse()
           print response.status
           print response.reason
           print response.read()
      except Exception, e:
           print e
      finally:
           if httpClient:
           httpClient.close()

      screenshot-from-2016-11-12-15-17-23

    2.  sent POST request in Python

      #!/usr/bin/python
      import httplib, urllib
      httpClient = None
      try:
      params = urllib.urlencode({‘name’: ‘DDOS’, ‘age’: 25})
      headers = {“Content-type”: “application/x-www-form-urlencoded”, “Accept”: “text/plain”}
      httpClient = httplib.HTTPConnection(“localhost”, 80, timeout=30)
      httpClient.request(“POST”, “/test.php”, params, headers)
      response = httpClient.getresponse()
      print response.status
      print response.reason
      print response.read()
      print response.getheaders()
      except Exception, e:
      print e
      finally:
      if httpClient:
      httpClient.close()

      screenshot-from-2016-11-12-15-22-26

    No system is truly secure!

You must be logged in to reply to this topic.