IPv6地址Python

时间:2018-04-13 12:31:11

标签: python ip-address ipv6 ipv4

我的IPv6地址等于“2001:200:e000 :: / 35”。 但是,我无法从库ipaddress中的函数ip_address()输入它。 该函数适用于不包含“/”的地址,例如IPv4“1.0.0.0”和IPv6“2001:12f8:0:17 :: 23”:

package com.nepalpolice.mnemonics.chat;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.RemoteMessage;
import com.nepalpolice.mnemonics.R;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Created by filipp on 6/28/2016.
 */
public class Chat_Room  extends AppCompatActivity{
    private Button btn_send_msg;
    private EditText input_msg;
    private TextView chat_conversation;
    private Toolbar mainToolbar;
    private String user_name,room_name;
    private DatabaseReference root ;
    private String temp_key;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chat_room);



        btn_send_msg = (Button) findViewById(R.id.btn_send);
        input_msg = (EditText) findViewById(R.id.msg_input);
        chat_conversation = (TextView) findViewById(R.id.textView);
        user_name = getIntent().getExtras().get("user_name").toString();
        room_name = getIntent().getExtras().get("room_name").toString();

        mainToolbar = (Toolbar) findViewById(R.id.main_chat);
        setSupportActionBar(mainToolbar);
        getSupportActionBar().setTitle(" Room - "+room_name);


        root = FirebaseDatabase.getInstance().getReference().child(room_name);





        btn_send_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Map<String,Object> map = new HashMap<String, Object>();
                temp_key = root.push().getKey();
                root.updateChildren(map);
                DatabaseReference message_root = root.child(temp_key);
                Map<String,Object> map2 = new HashMap<String, Object>();
                map2.put("name",user_name);
                map2.put("msg",input_msg.getText().toString());
                message_root.updateChildren(map2);
                input_msg.getText().clear();

            }
        });




        root.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                append_chat_conversation(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

                append_chat_conversation(dataSnapshot);

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }

    private String chat_msg,chat_user_name;

    private void append_chat_conversation(DataSnapshot dataSnapshot) {

        Iterator i = dataSnapshot.getChildren().iterator();

        while (i.hasNext()){

               chat_msg = (String) ((DataSnapshot)i.next()).getValue();
            chat_user_name = (String) ((DataSnapshot)i.next()).getValue();

            chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
        }

        
    }
   
  
}

返回:

ipaddress.ip_address(unicode("1.0.0.0","utf-8"))

并且

IPv4Address(u'1.0.0.0')

返回:

ipaddress.ip_address(unicode("2001:12f8:0:17::23","utf-8"))

然而,当我尝试使用IPv6“2001:200:e000 :: / 35”时,

IPv6Address(u'2001:12f8:0:17::23')

返回:

ipaddress.ip_address(unicode("2001:200:e000::/35","utf-8"))

我应该如何输入包含“/”的IP地址到ip_address?

1 个答案:

答案 0 :(得分:3)

&#34;带/在其中的IP地址&#34;你打电话给他们实际上并不是IP地址。它们是IP网络(意思是一系列IP地址),称为&#34; CIDR表示法&#34;。

您可以使用ipaddress.ip_network()处理IP网络。例如:

>>> ipn = ipaddress.ip_network("2001:200:e000::/35")
>>> ipn.num_addresses
9903520314283042199192993792