title: How do you build a URL that contains a username and password from user input in Python?
body: <p>I see you can parse usernames and passwords with:</p>
  
  <pre><code>from urlparse import urlparse
  r = urlparse('http://myuser:mypass@example.com')
  print r.username
  # =&gt; 'myuser'
  </code></pre>
  
  <p>How can I go the other way? I can't use <code>urlunparse</code> because I can't do this:</p>
  
  <pre><code>r.username = request.args['username']
  # =&gt; AttributeError: can't set attribute
  </code></pre>
  
  <p>I'm interested because the username contains characters that need escaped (namely: <code>@</code>, <code>/</code>).</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>This string is coming from user input, so I won't know ahead of time what it is. It's a security risk to maintain your own list of escape characters, so string concatenation and custom character escaping with <code>replace</code> won't help here.</p>