简体   繁体   English

在python中设置IP和子网

[英]Setting IP and Subnet in python

I've been doing the programming for a start up company I know to get some experience. 我一直在为自己了解的经验的初创公司进行编程。 I've been told I'll need to relay data from my original piece of software running on one computer to 8 others using ethernet. 有人告诉我,我需要使用以太网将数据从一台计算机上运行的原始软件中继到另外8台计算机。 Bear in mind I haven't really done much network stuff before. 请记住,我之前并没有做太多的网络工作。

I see that I'll need the python socket module so I've learned a bit of that. 我看到我需要python套接字模块,所以我学到了一些。 I also get that I'll need a network switch hardware-wise. 我也明白,我将需要一个硬件上的网络交换机。

What I'm unsure about is how I set up the network in the first place. 我不确定的是首先如何设置网络。 I get that I can do this via Device Manager but setting all computers to the same subnet and different IP addresses. 我知道可以通过“设备管理器”来执行此操作,但是将所有计算机设置为相同的子网和不同的IP地址。 However, the idea is that people will bring their laptops to be connected, so I don't want to have to mess around with each laptop to connect it to the network. 但是,这个想法是人们将携带他们的笔记本电脑进行连接,所以我不想弄乱每台笔记本电脑将其连接到网络。 So is there a way of doing this directly in Python so that I can just write it in the program that I'll have running on each laptop? 那么有没有一种直接在Python中执行此操作的方法,这样我就可以在要在每台笔记本电脑上运行的程序中编写它了?

Thanks! 谢谢!

You need a DHCP server and people's NIC's to retrieve their network configuration automatically. 您需要DHCP服务器和人们的NIC才能自动检索他们的网络配置。

Windows: 视窗:

netsh interface ip set address "Local Area Connection" dhcp

Debian (/etc/network/interfaces): Debian(/ etc / network / interfaces):

auto eth0
iface eth0 inet dhcp

Red Hat (/etc/sysconfig/network-scripts/ifcfg-eth0): 红帽(/ etc / sysconfig / network-scripts / ifcfg-eth0):

DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes

I suppose you could use Python or whatever language you're convenient in to fetch the relevant interface's name, as they're not guaranteed to be named "eth0" or "Local Area Connection", eg subprocess.Popen("cat /proc/net/dev/"). 我想您可以使用Python或任何方便的语言来获取相关接口的名称,因为它们不能保证被命名为“ eth0”或“ Local Area Connection”,例如subprocess.Popen(“ cat / proc / net / dev /“)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM