0

I'm new to using Python and sockets in general (only started yesterday) so I've been having a lot of issues trying to set up a TCP client and server. The issue I'm having is that I want to send a key from the server to the client. I know that the server grabs the key correctly as it prints out the correct key, however it has a 0 appended to it in a new line and when the key is sent to the client the only thing that is displayed is " b'0' ".

I've made very little progress due to my lack of experience and after searching for hours I still haven't found a solution to my problem.

Here is the server code:

import os
from socket import * #import the socket library


HOST = '' #We are the host
PORT = 29876
ADDR = (HOST, PORT)
BUFFSIZE = 4096
message = 'Hello, World!'


serv =  socket( AF_INET,SOCK_STREAM)
serv.bind(ADDR,) 
serv.listen(5) 
print ('listening...')


conn,addr = serv.accept()
print (conn,addr)
print ('...connected')


key = os.system("cat ~/.ssh/id_rsa.pub")
conn.send(str(key))
print (key)


conn.close()

Here is the client code

from socket import *
import os

HOST = 'xxx.xxx.xxx.xxx'
PORT = 29876 
ADDR = (HOST,PORT)
BUFFSIZE = 4096
message = "Hello, World!"

cli = socket( AF_INET, SOCK_STREAM)
cli.connect(ADDR,)

data = cli.recv(BUFFSIZE)
print (data)

cli.close()

As you can tell from my code I'm using Python 3.3 Any help with this issue is greatly appreciated.

1 Answer 1

2

os.system() does not return the process's output, but the return value (ie. integer 0).

If you only want to read a file, do it manually:

with open(os.path.expanduser("~/.ssh/id_rsa.pub")) as f:
    key = f.read()
    conn.sendall(key)

If you need process output, read the documentation for the subprocess module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.