Skip to content

Instantly share code, notes, and snippets.

@rokujyouhitoma
Created September 19, 2014 04:40
Show Gist options
  • Save rokujyouhitoma/173b75f030a1f1f22591 to your computer and use it in GitHub Desktop.
Save rokujyouhitoma/173b75f030a1f1f22591 to your computer and use it in GitHub Desktop.
PythonでHTTP RequestとResponseの生文字列を確認したい時のモンキーパッチ
def http_connection_send():
import httplib
def patch_send():
old_send= httplib.HTTPConnection.send
def new_send(self, data, *args, **kwargs):
print(data)
body = old_send(self, data, *args, **kwargs)
return body
httplib.HTTPConnection.send = new_send
patch_send()
def http_response_read():
import httplib
def patch_read():
old_func= httplib.HTTPResponse.read
def new_func(self, *args, **kwargs):
body = old_func(self, *args, **kwargs)
#このあたりはだいぶ適当
if self.version == 10:
print('HTTP/1.0 %s %s') % (self.status, self.reason)
elif self.version == 11:
print('HTTP/1.1 %s %s') % (self.status, self.reason)
else:
raise Exception('invalid HTTP version: %s' % self.version)
print(self.msg)
print(body)
return body
httplib.HTTPResponse.read = new_func
patch_read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment