Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Passing Multiple Variables Across TCP Client/Server
    text
    copied!<p>I'm creating a TCP client and server using Python 3.3 and I'm new to using both Python and sockets. My issue is that I need to be able to store anything passed across the connection as a separate variable for writing to various files, etc. </p> <p>I'm unsure how to do this as it all seems to be one stream of data that I cannot store separately. Below is my latest piece of working code and all it does is send the data I need across the connection. Should I be trying to send all the data across as a list and de-construct it into separate variables? Can I already access them separately and I just haven't figured it out yet? Is my approach all wrong? </p> <p>Server code:</p> <pre><code>import os from socket import * HOST = '' #We are the host PORT = 29876 ADDR = (HOST, PORT) BUFFSIZE = 4096 serv = socket( AF_INET,SOCK_STREAM) serv.bind(ADDR,) #Create tuple with one element serv.listen(5) print ('listening...') conn,addr = serv.accept() print (conn,addr) print ('...connected') with open(os.path.expanduser("~/.ssh/id_rsa.pub")) as f: key = f.read() conn.sendall(key) print(key) while True: data = conn.recv(BUFFSIZE) print(data) conn.close() </code></pre> <p>Client code:</p> <pre><code>from socket import * import os import socket HOST = 'xxx.xxx.xxx.xxx' PORT = 29876 #Must match server.py ADDR = (HOST,PORT) BUFFSIZE = 4096 cli = socket.socket( AF_INET, SOCK_STREAM) cli.connect(ADDR,) s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.connect(("gmail.com",80)) ip = ((s.getsockname()[0])) ip = ip.encode('utf-8') cli.send(ip) while True: data = cli.recv(BUFFSIZE) print (data) cli.close() </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload