1

I've a python file named "config.py" which actually checks the ip of user and run the django server on that port accordingly.
The method I used there was to create a batch file using python and execute it later.

import socket
import subprocess
x = socket.gethostbyname(socket.gethostname())
x = str(x)
with open("run.bat", "w") as f:
    f.write(f'manage.py runserver {x}:0027')
subprocess.call([r'run.bat'])

But this method is not very effective. I want a way like:
import something something.run("manage.py")

or something accordingly
Kindly Help me doing this

Edit:
I've seen Django's manage.py.
Can I edit this file (the sys.argv section) in such a way that it automatically runs a server on 192.168.x.x:8000 just by clicking on manage.py?
Please help

1
  • 1
    not sure what you actually want to do, but if you want to check the IP, then use middleware for that. Commented Sep 29, 2021 at 15:43

1 Answer 1

2

I think the method you're trying to reach is to run a shell command inside a python script, which can be achieved by using the os.system() function.

You can use it in your code as the following:

import socket
import subprocess
import os
x = socket.gethostbyname(socket.gethostname())
x = str(x)
os.system(f'manage.py runserver {x}:0027')
Sign up to request clarification or add additional context in comments.

5 Comments

That did it. Thank you very much
@Ojas_Gupta Great! Don't forget to accept as the answer ;D
Hi! @Bernardo Duarte , would you like to please edit your answer according to edited question please? Thank you very much
@Ojas_Gupta I believe it is a whole new question, I think you should create a new one due to community guidelines and for better visibility so that I or someone else can help you ;D
You can also do subprocess.call(['manage.py', 'runserver', x + ':0027']) instead of the os.system line.

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.