THRIFT-1568 Thrift: Add User-Agent header to Python THttpClient
authorRoger Meier <roger@apache.org>
Fri, 13 Apr 2012 14:20:08 +0000 (14:20 +0000)
committerRoger Meier <roger@apache.org>
Fri, 13 Apr 2012 14:20:08 +0000 (14:20 +0000)
Patch: Dave Watson

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1325781 13f79535-47bb-0310-9956-ffa450edef68

lib/py/src/transport/THttpClient.py

index 293d930..ea80a1a 100644 (file)
 #
 
 import httplib
+import os
 import socket
+import sys
+import urllib
 import urlparse
 import warnings
 
@@ -122,6 +125,13 @@ class THttpClient(TTransportBase):
     self.__http.putheader('Content-Type', 'application/x-thrift')
     self.__http.putheader('Content-Length', str(len(data)))
 
+    if not self.__custom_headers or 'User-Agent' not in self.__custom_headers:
+      user_agent = 'Python/THttpClient'
+      script = os.path.basename(sys.argv[0])
+      if script:
+        user_agent = '%s (%s)' % (user_agent, urllib.quote(script))
+      self.__http.putheader('User-Agent', user_agent)
+
     if self.__custom_headers:
         for key, val in self.__custom_headers.iteritems():
             self.__http.putheader(key, val)