Đó là bởi vì bạn đang kiểm tra
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[["172","192","10"]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
0i.e]
1=="1"
Out[10]: False
Modification:
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[["172","192","10"]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
edit:
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[[172,192,10]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
edit2:
Vì
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[["172","192","10"]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
1 hoặc làm điều tương tự, bạn có thể hợp nhất chúng như import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[["172","192","10"]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
2import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[[172,192,10]]
for i in range [0,n]:
if [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
output:
How many IP addresses need to be generated
20
10.88.254.205
10.205.49.201
10.67.147.81
10.10.75.63
10.136.166.197
10.241.237.2
10.33.204.114
10.10.190.132
10.11.72.207
10.24.178.32
10.215.156.125
10.75.79.15
10.47.159.174
10.177.12.191
10.96.189.105
10.141.216.118
10.99.138.176
10.92.138.176
10.81.147.16
10.246.147.4
edit3:
Và cá nhân tôi nghĩ rằng
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
x1 = random.choice[["172","192","10"]]
for i in range [0,n]:
if [int[x1] == 10 ]:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
elif [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
3Do đó mã chỉnh sửa này:
import random
n = int[raw_input["How many IP addresses need to be generated \n"]]
for i in range [0,n]:
x1 = random.choice[[172,192,10]] #moved inside loop
if [int[x1] == 172]:
x2 = random.randint[16,31]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
else:
x2 = random.randint[0,255]
x3 = random.randint[0,255]
x4 = random.randint[0,255]
print ".".join[map[str,[[x1,x2,x3,x4]]]]
output:
How many IP addresses need to be generated
20
192.178.231.68
192.253.233.190
192.114.158.193
192.196.30.127
172.22.44.3
172.23.180.6
10.69.55.105
10.105.63.195
172.21.0.195
192.83.125.135
10.36.196.137
10.8.251.102
192.38.130.4
172.22.21.131
10.204.243.231
192.136.121.203
172.30.89.149
192.40.178.100
192.155.127.75
172.23.97.228
Nhận cuốn sách này -> Vấn đề về Array: Đối với các cuộc phỏng vấn và lập trình cạnh tranh Trong bài viết này, chúng tôi đã phát triển một kịch bản Python để có được địa chỉ IP công cộng riêng tư cùng với các chi tiết khác. Mục lục Địa chỉ IP công cộng và riêng Trường hợp sử dụng Tay: Hãy vui vẻ với địa chỉ IP Địa chỉ tư nhân và công cộng Khi bạn thiết lập mạng gia đình của mình, các địa chỉ được gán cho các thiết bị mạng của bạn sẽ riêng tư trừ khi bạn công khai chúng. Địa chỉ riêng tư không thể truy cập được cho công chúng trừ khi bạn định cấu hình chuyển tiếp cổng trên cổng của bạn [bộ định tuyến đối diện Internet]. Tóm lại, địa chỉ riêng không thể định tuyến.Địa chỉ IP là gì
Hệ thống tên miền [DNS]
Mỗi nút [máy tính] trên mạng được gán một số duy nhất xác định nó trên mạng. Số duy nhất này được mã hóa trong mỗi gói được gửi và định mệnh cho nút đó trên mạng.
Và trong khi đó, bạn đạt được một địa chỉ công khai khi bạn kết nối với mạng ISP [Nhà cung cấp dịch vụ Internet] của ISP. Không giống như địa chỉ riêng, địa chỉ công cộng có thể định tuyến.
Hệ Thống Tên MiềnAccess Control, Geographical Location
Tay: Hãy vui vẻ với địa chỉ IP
#!/usr/bin/python
import sys
import socket
import requests
import json
from argparse import ArgumentParser
ipify_json_endpoint = "//api.ipify.org?format=json"
def get_my_public_ip[]:
response = requests.get[ipify_json_endpoint]
return json.loads[response.text]["ip"]
def get_my_private_ip[]:
return socket.gethostbyname[socket.gethostname[]]
def resolve_fqdn[fqdn]:
"""
fqdn: Fully Qualified Domain Name of the server
"""
return socket.gethostbyname[fqdn]
def echo_ip_address[ip_address]:
print["The IP Address is: %s" %[ip_address]]
if __name__ == "__main__":
args_parser = ArgumentParser[]
args_parser.add_argument["--server", action="store_true"]
args_parser.add_argument["--fqdn", help="FQDN of server"]
args_parser.add_argument["--public", action="store_true"]
args_parser.add_argument["--private", action="store_true"]
args = args_parser.parse_args[]
if args.server:
if not args.fqdn:
args_parser.error["The Fully Qualified Domain Name is required"]
sys.eixt[-1]
ip_address = resolve_fqdn[args.fqdn]
echo_ip_address[ip_address]
elif args.private:
ip_address = get_my_private_ip[]
echo_ip_address[ip_address]
elif args.public:
ip_address = get_my_public_ip[]
echo_ip_address[ip_address]
else:
args_parser.error["Missing an action: --server or --private or --public"]
Mỗi nút [máy tính] trên mạng được gán một số duy nhất xác định nó trên mạng. Số duy nhất này được mã hóa trong mỗi gói được gửi và định mệnh cho nút đó trên mạng.
The script has three procedures[functions]:
Địa chỉ tư nhân và công cộng
Calls the ipify JSON endpoint and then converts the response into a python dictionary which only has the ip key[field]. And this field has the public address of the node that made the request to the API and that node is your computer.Khi bạn thiết lập mạng gia đình của mình, các địa chỉ được gán cho các thiết bị mạng của bạn sẽ riêng tư trừ khi bạn công khai chúng. Địa chỉ riêng tư không thể truy cập được cho công chúng trừ khi bạn định cấu hình chuyển tiếp cổng trên cổng của bạn [bộ định tuyến đối diện Internet]. Tóm lại, địa chỉ riêng không thể định tuyến.
Returns the private address assigned to the node.
It uses two functions from the socket module:
gethostname[]: Returns hostname of the node
gethostbyname[]: Resolves the hostname to an IP address.Và trong khi đó, bạn đạt được một địa chỉ công khai khi bạn kết nối với mạng ISP [Nhà cung cấp dịch vụ Internet] của ISP. Không giống như địa chỉ riêng, địa chỉ công cộng có thể định tuyến.
Obtaining a server's IP address is a DNS resolution and so, you will need a server's fully qualified domain name.
The FQDN resolution is accomplished by socket.gethostbyname[], and it takes a host name as its argument but for our case, we shall need the FQDN, since the server doesnot belong to our domain or local network.Hệ Thống Tên Miền
Nếu __name__ == '__main__': Điều này kiểm tra xem mô -đun [tập lệnh này] đang được chạy làm chương trình chính và không chỉ được chạy từ một mô -đun khác, tức là được nhập bởi một mô -đun Python khác [tệp].Imported by another python module[file].
Kịch bản cũng sử dụng mô -đun ArgumentParser vì nó thực hiện hai tác vụ liên quan tức là nhận IP công khai và giải quyết IP của máy chủ. Vì vậy, tôi đã sử dụng một số công tắc để thao túng hành vi của nó. Tôi đã cố gắng để được ưa thích, vì vậy hãy thoải mái phân tách mã thành hai tệp hoặc thậm chí thực hiện một cách tiếp cận tốt hơn.
I was trying to be fancy, so feel free to separate the code into two files or even implement a better approach.
Usage:
Nhận IP công cộng.
Nhận IP riêng.
Nhận địa chỉ IP của máy chủ.
Lưu ý: Một tên miền đủ điều kiện [FQDN] là tên miền có chứa tên máy chủ và tên miền được nối bởi một dấu chấm [.] Ví dụ:: A Fully Qualified Domain Name [FQDN] is one that contains a hostname and the domain name concatenated by a dot[.] Examples:
- www.google.com
- www.linkedin.com
Nhận IP công cộng mà không có API bên ngoài về địa chỉ IP công cộng, phương pháp đầu tiên là giải pháp phổ biến nhất bạn sẽ tìm thấy, mặc dù không phải là phương pháp duy nhất. Tùy chọn khác là sử dụng tiêu chuẩn UPNP để liên lạc với Thiết bị Gateway Internet [IGD] của bạn. Tiêu chuẩn UPNP cho phép các thiết bị trên mạng cục bộ khám phá lẫn nhau.
Regarding the public IP address, the first method is the most common solution you will find, though not the
only one. The other option is to use the UpnP standard to communicate with your Internet Gateway Device [IGD]. The UpnP standard allows devices on the local network to discover each other.
import upnpy
upnp = upnpy.UPnP[]
internet_gateway_device = upnp.get_igd[] #1
service_id = "WANPPPConnection.1" #2 It can also be WANIPConnection depeding on the connection type. PPP Point to Point protocol
service = device[service_id] #3
print[service.GetExternalIPAddress[["NewExternalIPAddress"]]
Yêu cầu
- Gói Upnpy
pip install upnpy
Hiểu mã
- Dòng số 1: Xác định vị trí thiết bị cổng Internet [IGD] trên mạng bằng cách gửi tin nhắn phát sóng [dưới mui xe].Under the hood].
- Dòng số 2: Các thiết bị mạng có thể cung cấp một loạt các dịch vụ và các dịch vụ này được xác định bởi ID và đặc biệt đối với trường hợp của chúng tôi, chúng tôi có thể sử dụng wanpppconnection.1 hoặc wanipconnection. Đối với danh sách các dịch vụ mà thiết bị cung cấp bạn có thể sử dụng:
4import random n = int[raw_input["How many IP addresses need to be generated \n"]] x1 = random.choice[["172","192","10"]] for i in range [0,n]: if [int[x1] == 10 ]: x2 = random.randint[0,255] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]] elif [int[x1] == 172]: x2 = random.randint[16,31] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]] else: x2 = random.randint[0,255] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]]
4import random n = int[raw_input["How many IP addresses need to be generated \n"]] x1 = random.choice[["172","192","10"]] for i in range [0,n]: if [int[x1] == 10 ]: x2 = random.randint[0,255] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]] elif [int[x1] == 172]: x2 = random.randint[16,31] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]] else: x2 = random.randint[0,255] x3 = random.randint[0,255] x4 = random.randint[0,255] print ".".join[map[str,[[x1,x2,x3,x4]]]]
- Dòng số 3: Gán dịch vụ đích cho biến dịch vụ.
- Và cuối cùng, tập lệnh đưa ra yêu cầu cho dịch vụ để truy xuất địa chỉ IP công cộng.
Usage:
Suprised !!!!, Vâng, bạn nên được.
Đầu ra trên có nghĩa là, mạng của tôi không có IGD, nhưng tôi có nó, chỉ là nó không hiểu các giao thức UPNP.
Không phải tất cả các thiết bị đều tuân thủ UPNP và do đó, đối với mạng của tôi, tôi đã thử nghiệm điều này, cổng của tôi không hiểu các giao thức UPNP.
Mã hóa hạnh phúc.