from __future__ import absolute_import

import socket

from httplib import HTTPConnection, HTTPSConnection
from urllib import urlencode
from urlparse import urlparse


class Replayer(object):
    def __init__(self, url, method, data=None, headers=None):
        self.url = url
        self.method = method
        self.data = data
        self.headers = headers

    def replay(self):
        urlparts = urlparse(self.url)
        if urlparts.scheme == 'http':
            conn_cls = HTTPConnection
        elif urlparts.scheme == 'https':
            conn_cls = HTTPSConnection
        else:
            raise ValueError(self.url)

        data = self.data
        if isinstance(data, dict):
            data = urlencode(data)

        if urlparts.query:
            full_url = urlparts.path + '?' + urlparts.query
        else:
            full_url = urlparts.path

        conn = conn_cls(urlparts.netloc)
        try:
            conn.request(self.method, full_url, data, self.headers or {})

            response = conn.getresponse()
        except socket.error as e:
            return {
                'status': 'error',
                'reason': str(e),
            }

        return {
            'status': response.status,
            'reason': response.reason,
            'headers': response.getheaders(),
            'body': response.read(),
        }

from __future__ import absolute_import
import requests


class Replayer(object):
    def __init__(self, url, method, data=None, headers=None):
        self.url = url
        self.method = method
        self.data = data
        self.headers = headers

    def replay(self):
        try:
            response = requests.request(
                self.method,
                self.url,
                data=self.data,
                headers=self.headers or {}
            )
        except requests.RequestException as e:
            return {
                'status': 'error',
                'reason': str(e),
            }

        return {
            'status': response.status_code,
            'reason': response.reason,
            'headers': response.headers,
            'body': response.content,
        }

Use requests instead of httplib to do replay
Use requests instead of httplib to do replay

  from __future__ import absolute_import
+ import requests
- 
- import socket
- 
- from httplib import HTTPConnection, HTTPSConnection
- from urllib import urlencode
- from urlparse import urlparse
  
  
  class Replayer(object):
      def __init__(self, url, method, data=None, headers=None):
          self.url = url
          self.method = method
          self.data = data
          self.headers = headers
  
      def replay(self):
-         urlparts = urlparse(self.url)
-         if urlparts.scheme == 'http':
-             conn_cls = HTTPConnection
-         elif urlparts.scheme == 'https':
-             conn_cls = HTTPSConnection
-         else:
-             raise ValueError(self.url)
- 
-         data = self.data
-         if isinstance(data, dict):
-             data = urlencode(data)
- 
-         if urlparts.query:
-             full_url = urlparts.path + '?' + urlparts.query
-         else:
-             full_url = urlparts.path
- 
-         conn = conn_cls(urlparts.netloc)
          try:
-             conn.request(self.method, full_url, data, self.headers or {})
- 
-             response = conn.getresponse()
-         except socket.error as e:
+             response = requests.request(
+                 self.method,
+                 self.url,
+                 data=self.data,
+                 headers=self.headers or {}
+             )
+         except requests.RequestException as e:
              return {
                  'status': 'error',
                  'reason': str(e),
              }
  
          return {
-             'status': response.status,
+             'status': response.status_code,
              'reason': response.reason,
-             'headers': response.getheaders(),
+             'headers': response.headers,
-             'body': response.read(),
+             'body': response.content,
          }
  
Use requests instead of httplib to do replay
## Code Before:
from __future__ import absolute_import

import socket

from httplib import HTTPConnection, HTTPSConnection
from urllib import urlencode
from urlparse import urlparse


class Replayer(object):
    def __init__(self, url, method, data=None, headers=None):
        self.url = url
        self.method = method
        self.data = data
        self.headers = headers

    def replay(self):
        urlparts = urlparse(self.url)
        if urlparts.scheme == 'http':
            conn_cls = HTTPConnection
        elif urlparts.scheme == 'https':
            conn_cls = HTTPSConnection
        else:
            raise ValueError(self.url)

        data = self.data
        if isinstance(data, dict):
            data = urlencode(data)

        if urlparts.query:
            full_url = urlparts.path + '?' + urlparts.query
        else:
            full_url = urlparts.path

        conn = conn_cls(urlparts.netloc)
        try:
            conn.request(self.method, full_url, data, self.headers or {})

            response = conn.getresponse()
        except socket.error as e:
            return {
                'status': 'error',
                'reason': str(e),
            }

        return {
            'status': response.status,
            'reason': response.reason,
            'headers': response.getheaders(),
            'body': response.read(),
        }

## Instruction:
Use requests instead of httplib to do replay
## Code After:
from __future__ import absolute_import
import requests


class Replayer(object):
    def __init__(self, url, method, data=None, headers=None):
        self.url = url
        self.method = method
        self.data = data
        self.headers = headers

    def replay(self):
        try:
            response = requests.request(
                self.method,
                self.url,
                data=self.data,
                headers=self.headers or {}
            )
        except requests.RequestException as e:
            return {
                'status': 'error',
                'reason': str(e),
            }

        return {
            'status': response.status_code,
            'reason': response.reason,
            'headers': response.headers,
            'body': response.content,
        }
