def safe_urlencode(params, doseq=0):
 if hasattr(params, 'items'):
  params = params.items()
 new_params = list()
 for (k, v) in params:
  k = k.encode('utf-8')
  if isinstance(v, six.string_types):
   new_params.append((k, v.encode('utf-8')))
  elif isinstance(v, (list, tuple)):
   new_params.append((k, [i.encode('utf-8') for i in v]))
  else:
   new_params.append((k, six.text_type(v)))
 return urlencode(new_params, doseq)

def safe_urlencode ( params , doseq = 0 ) : if hasattr ( params , 'items' ) : params = params . items ( ) new_params = list ( ) for ( k , v ) in params : k = k . encode ( 'utf-8' ) if isinstance ( v , six . string_types ) : new_params . append ( ( k , v . encode ( 'utf-8' ) ) ) elif isinstance ( v , ( list , tuple ) ) : new_params . append ( ( k , [ i . encode ( 'utf-8' ) for i in v ] ) ) else : new_params . append ( ( k , six . text_type ( v ) ) ) return urlencode ( new_params , doseq )
utf-8-safe version of safe_urlencode the stdlib safe_urlencode prior to python 3 .