-3

I need this for a project that I am making but I am not sure how to do it.

I'm look in for syntax like:
SENDER.py

string = "Hello"
send(hello)

READER.py

string = read()
print(string)

EDIT
Made a solution.
https://github.com/Ccode-lang/simpmsg

Ccode
  • 3
  • 2
  • 2
    Have you look at `import` at all? – Grismar Dec 31 '21 at 03:09
  • 1
    If they're actually two different programs, and not just different modules of the same program, then maybe look into [pipes](https://www.geeksforgeeks.org/python-os-pipe-method/) – Green Cloak Guy Dec 31 '21 at 03:10
  • I have looked at import. It does not seem like it would work in my situation. I need multiple sender programs. I thought about maybe using tcp but that did not work. I am looking into pipes at the moment. – Ccode Dec 31 '21 at 03:17
  • 2
    Welcome to Stack Overflow! Please take the [tour]. What are you trying to accomplish exactly? If you're looking to grab a string from a Python file, you can import it. If you're looking to literally send a string from one Python program to another, there's an existing question: [Interprocess communication in Python](/q/6920858/4518341) (though I have no experience with it myself). Please [edit] to clarify. For more tips, see [ask]. – wjandrea Dec 31 '21 at 03:18
  • Maybe just write the string to a file, and have the other program read from the same file ? – Gwendal Delisle Arnold Dec 31 '21 at 03:22
  • @GwendalDelisleArnold If there are to many programs sending they all overwrite each other. – Ccode Dec 31 '21 at 03:28

1 Answers1

0

If both programmers are on different computers, you can try using sockets

server.py

import socket

s = socket.socket()
port = 12345
s.bind(('', port))
s.listen(5)
c, addr = s.accept()
print "Socket Up and running with a connection from",addr
while True:
    rcvdData = c.recv(1024).decode()
    print "S:",rcvdData
    sendData = raw_input("N: ")
    c.send(sendData.encode())
    if(sendData == "Bye" or sendData == "bye"):
        break
c.close()

client.py

import socket

s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
    str = raw_input("S: ")
    s.send(str.encode());
    if(str == "Bye" or str == "bye"):
        break
    print "N:",s.recv(1024).decode()
s.close()

If you want to store it first so the other programmer can read it next time use files

sender.py

file1 = open("myfile.txt","w")
file1.write("hello")
file1.close()
  

reader.py

file1 = open("myfile.txt","r")
data = file1.read()
file1.close()
print(data)
  
Sashank
  • 557
  • 6
  • 20