title: Python - How can I send a rsa.key.PublicKey object through socket?
body: <p>I'm using the python rsa module to generate a public/private key pair. I want to send the public key to the other computer through a socket connection.</p>
  
  <p>When I try to encode the public key to send it, I get this error:</p>
  
  <pre><code>  File "chatclient.py", line 128, in &lt;module&gt;
      s.sendall(pubkey.encode('utf-8'))
  AttributeError: 'PublicKey' object has no attribute 'encode'
  </code></pre>
  
  <p>I cannot figure out a way to encode the key other than the method that causes the error. If I try to convert it to a string, encode it and send it through, I cannot use the key to encrypt any messages, nor are there any documented ways to turn it back into a PublicKey object.</p>
  
  <p>This is what causes the error:</p>
  
  <pre><code>s.sendall(pubkey.encode('utf-8'))
  </code></pre>
  
  <p>Here's the package on pypi and the documentation:</p>
  
  <p><a href="https://pypi.org/project/rsa/" rel="nofollow noreferrer">https://pypi.org/project/rsa/</a></p>
  
  <p><a href="https://stuvel.eu/python-rsa-doc/usage.html" rel="nofollow noreferrer">https://stuvel.eu/python-rsa-doc/usage.html</a></p>