1

I am trying to send a string from a program 1 to another program program 2, both in python 3
e.g.

#_____________________________________1.py
a = input('Type in a string: ')
#                                     send somehow a string a from this program
#                                     to the second program

I want to somehow send a string a to my second program so it will print out a:

#_____________________________________2.py
#                                     receive somehow a string from the first
#                                     program and store it in a
print(a)

How do I do this?

I am still a beginner programmer and would love it if you could help me.

  1. I need to be able to enter the string in 1.py
  2. I need then be able to access the string that I entered from 2.py.
  3. I have to have them as two separate files.

ANSWER:

I found a way to solve this.

import subprocess
username = input()
subprocess.Popen(['python.exe', 'file.py', username])
user3666197
  • 1
  • 6
  • 50
  • 92
Nic
  • 1,549
  • 2
  • 13
  • 13
  • and what are you trying to achieve specifically? – Divisadero May 13 '16 at 07:36
  • @Divisadero I basically have one program with takes the account you are logging in to and says to the other program that it is logged into an account e.g. "Greg" – Nic May 13 '16 at 07:45
  • @Divisadero Better explanation: A user types in their username and password and the program checks if it username and password is correct. If the information is correct, it runs another program which is the program you do all your account stuff in. I need the program to know that you are user "Dave" or whatever. – Nic May 13 '16 at 07:53
  • In that case maybe you do NOT need SOA, if the first program can facilitate the second directly by using its modules. The question is if it can :D – Divisadero May 13 '16 at 08:37
  • Can you give an example? @Divisadero – Nic May 13 '16 at 08:46
  • Cannot. I do not know what do you use to validate the credentials. But if you have access to the validation module or the app has API which you can call, then you can access it directly. By importing module and using it or by calling the program API with arguments specified in documentation. – Divisadero May 13 '16 at 10:04
  • Confused, I found another way which I knew I could do which is writing to a file so I've chose that but I am hashing it so it is safer. Thanks for all of your help though @Divisadero – Nic May 13 '16 at 10:10
  • It may work that way, but it is definitely wrong approach. But as far as it suits you, your choice. – Divisadero May 13 '16 at 10:20
  • Well @Divisadero I don't really want to do it this way but I need to be able to understand it. Could you perhaps give me some links then to what you think I should use? That would give me a big help! – Nic May 13 '16 at 21:38
  • 1
    @Divisadero I found a way to do what I wanted to do. I edited my post for the answer. – Nic May 14 '16 at 03:53

5 Answers5

1

There are multiple ways to do that, you could use socket, file, pipe, shared-memory, message, ... to transfer a string from one process to another.

As an example of using messages, ZeroMQ provides an easy messaging library to do that smarter, than with system (raw, low level) sockets:
for more details look into http://zguide.zeromq.org/

A HelloWorld server example:

import time
import zmq

context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555")

while True:
    #  Wait for next request from client
    message = socket.recv()
    print("Received request: %s" % message)

    #  Do some 'work'
    time.sleep(1)

    #  Send reply back to client
    socket.send(b"World")

A HelloWorld client example:

import zmq

context = zmq.Context()

#  Socket to talk to server
print("Connecting to hello world server…")
socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")

#  Do 10 requests, waiting each time for a response
for request in range(10):
    print("Sending request %s …" % request)
    socket.send(b"Hello")

    #  Get the reply.
    message = socket.recv()
    print("Received reply %s [ %s ]" % (request, message))

With files, you write a file with program A then poll on it with program B.

user3666197
  • 1
  • 6
  • 50
  • 92
Alex Garcia
  • 773
  • 7
  • 21
1

You have many way communicate between two or N python program, Ex:

  • Socket
  • Database - MySQL, Mongodb, SQL Server... etc

or maybe you can try ZeroMQ

Karl Lin
  • 307
  • 4
  • 16
0
# file_1.py
def get_input():
    return input('Type in a string: ')

# file_2.py
from file_1 import get_input

print(get_input())
Levi Noecker
  • 3,142
  • 1
  • 15
  • 30
  • Question is about two different programs running, not just importing modules. – Divisadero May 13 '16 at 07:32
  • Levi, look at the edit I made in the last paragraph. I must have them as two separate programs. – Nic May 13 '16 at 07:34
  • 1
    Ahh, I see. Similar question was answered here: [link](http://stackoverflow.com/questions/16213235/communication-between-two-python-scripts) – Levi Noecker May 13 '16 at 07:37
  • 1
    Although this code may answer the question, providing additional context regarding _why_ and/or _how_ it answers the question would significantly improve its long-term value. Please [edit] your answer to add some explanation. – Toby Speight May 13 '16 at 12:03
0

Most common way of two programs communicating together is through http, tcp or other protocol. The same way as your browser (one program) communicates with the web server (another program).

You can send http request from one program and the second has to listen for that.

If you want more info, look for SOA. It is a bit more complicated than that, so if you have any questions, ask.

Divisadero
  • 895
  • 5
  • 18
0

I found the answer.

import subprocess
username = input()
subprocess.Popen(['python.exe', 'file.py', username], subprocess.creationflags=CREATE_NEW_CONSOLE)
Nic
  • 1,549
  • 2
  • 13
  • 13
  • 1
    Welcome to StackOverflow, Nic. Great sign that you try to find solutions on your own for your initial question. With a hope you would not consider it impolite/harmfull, let me post a few notes about the proposal made in this answer. While it might look as a solution, there are several weaknesses and your further learning shall benefit from these points of view. The worst is, that process 1 cannot send anything to process 2 in arbitrary time. That is the strongest objection. Next, your architecture requires process 2 not to exist before 1 decides to send the 1st & the only piece of data. – user3666197 May 14 '16 at 11:50
  • Thanks @user3666197 I edited my answer as I realized that I had left some things out. It may not be the correct way of doing it but it dose exactly what I want it to do. – Nic May 14 '16 at 22:56