user14624548
user14624548

Reputation:

How to start a new python script without ending the other one

I want to be able to open a new python script in a new terminal, without ending the other. Im trying to create a server with one script dishing out all the clients to new scripts, but it wont work if the other script is ended.

is there a way to fix this?

here are some of my code

starter:

import socket; import time
import os
def clear():
  os.system('cls' if os.name == 'nt' else 'clear')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = socket.gethostbyname(socket.gethostname())
print(ip, "thats da ip")
port = 8080
print(port, "da port")
s.bind((ip, port))
print("binded")
def idk():
    s.listen(1)
    c, a = s.accept()
    print(f"connection from {a}")
    print("authenticating")
    time.sleep(1)
    g = c.recv(300).decode("utf-8")
    if g == ">><<":
        print("accepted")
        time.sleep(3)
        print("starting script")
        time.sleep(2)
        clear()
        os.system("main.py")
        print("script started")
        while True:
            e = open("connection_script", "r")
            p = e.read()
            e.close()
            if p == "":
                pass
            else:
                print(f"port is {p}")
                print("sending port")
                c.sendall(bytes(p, "utf-8"))
                print("ending...")
                time.sleep(2)
                s.close()


    else:
        print("failed to pass")
        time.sleep(1)
        clear()
        c, a = None
        pass
idk()

the other script:


file_list = ">file<list<"
connection = "connection_script"

import time
import os
import socket
def_size = 0
req_size = 900
lv1_size = 1200
admin_size = 10000000000
wait_time = 1
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = socket.gethostbyname(socket.gethostname())
print(f"ip = {ip}")
time.sleep(1)

print("finding open port")

j = 0
while True:
  j+=1
  print(j)
  try:
    s.bind((ip, j))
    port = j
    break
  except:
    print("failed")
time.sleep(1)
def clear():
  os.system('cls' if os.name == 'nt' else 'clear')
clear()
time.sleep(2)
print(f"port is {port}")
time.sleep(2)
clear()
print("opening connection for client")
time.sleep(1)
s.listen(1)
clear()
e = open(connection, "w")
e.write(str(port))
e.close()
time.sleep(0.1)

print("conection for client open")
time.sleep(wait_time)
e = open(connection, "w")
e.write("")
e.close()
c, a = s.accept()
print(f"connection made by {a}, conn is {c}")
time.sleep(1)
e = open("_file_list_", "r")
p = e.read()
e.close()
s.sendall(bytes(p, "utf-8"))

the client im using:


import socket
import time


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = "192.168.1.65"
port = 8080
s.connect((ip, port))


s.sendall(bytes(">><<", "utf-8"))
b = s.recv(300)
print(b)
s.close()
s.connect((ip, b))
while True:
    print(s.recv(3000))
    s.sendall(bytes(input(), "utf-8"))

Upvotes: 0

Views: 46

Answers (2)

Shriyash Warghade
Shriyash Warghade

Reputation: 213

If you are using windows you can open multiple "windows command prompt" and if you are using linux based os you can open multiple "Terminals".

Upvotes: 0

MrHola21
MrHola21

Reputation: 321

Yo man if u r using PyCharm do this :

  1. right click on the server script and press run
  2. then go to the client script and press run
  3. they should be running together
  4. if u want to run multiple client scripts then in the "run" terminal on the left hand side there would be icons click the second one which states "modify run configurations"(only if u hover on it)
  5. after clicking it on the right hand side of the "name" field there should be a checkbox which states "allow parrell run" ,click on that
  6. now u will b able to run many client files at the same time

if u DONT have Pycharm do this(windows) :

  1. go in the directry where u have saved ur files
  2. press Shift+Right
  3. there would be an option of open powershell here click that
  4. then write "python " and do the same thing for the other files

WORKS ONLY FOR PYTHON!!!!!!

Upvotes: 2

Related Questions