问题描述:

在设备中有3个NI, ip分别为192.168.1.5/6/7。其中本端192.168.1.6同对端192.168.1.10建立了一个tunnel。

我希望测试tunnel连通性, 对端起一个socket server。本段作为client。

但是如果本端client直接连接,使用的源ip为192.168.1.5,端口随机。

我的迷惑在寻找一个指定ip的函数,在看了python的manual document中socket部分看了一遍后,没有找到这个函数。

随后我意识到我的一个思维误区:bind()函数

bind(address)之前作为socket server的一部分,启动时候使用制动server的ip和端口

但是bind在客户端一样的作用。指定socket建立链接时使用的ip和端口。

代码:

def start_tcp_client(ip, port):

    #server port and ip
server_ip = ip
servr_port = port tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try:
tcp_client.bind(('192.168.1.6', 11560))
tcp_client.connect((server_ip, servr_port))
except socket.error:
print 'fail to setup socket connection'
else:
print 'sending..........'
tcp_client.sendall("echo message") print 'reading...........'
print tcp_client.recv(1024)
tcp_client.close()

  

搞定!

python - socket - client端指定ip和端口的更多相关文章

  1. centos7 firewall指定IP与端口、端段访问(常用)

    https://blog.csdn.net/yipianfuyunsm/article/details/99998332 https://www.cnblogs.com/co10rway/p/8268 ...

  2. Golang client绑定本地IP和端口

    有时需要指定网络通信时本地使用的IP地址和端口号. 在Go语言中可通过定义 Dialer 中LocalAddr 成员实现. Dialer结构定义如下: // A Dialer contains opt ...

  3. python -socket -client

    socket client 发起连接. 流程为: 创建接口 发起连接 创建接口参数同socket server相同 发起连接的函数为socket.connect(ip,port) 这个地方的ip与po ...

  4. CentOS6.5 配置防火墙+允许指定ip访问端口

    参考博文: iptables防火墙只允许指定ip连接指定端口.访问指定网站 一.配置防火墙 打开配置文件 [root@localhost ~]# vi /etc/sysconfig/iptables ...

  5. server端获得到client端的IP地址的格式

    使用telnet,ping或其他client连接server端时,server端获得的client端的ip地址取决于client端使用的时ipv4还是ipv6地址. 例: client IPv4地址: ...

  6. centos7 firewall指定IP与端口访问(常用)

    1.启动防火墙 systemctl start firewalld.service 2.指定IP与端口 firewall-cmd --permanent --add-rich-rule="r ...

  7. pycharm中指定ip和端口

    pycharm中指定ip和端口 环境: 系统:win7 本机ip:192.168.0.100 1.建立工程请参照:https://www.cnblogs.com/effortsing/p/103945 ...

  8. MySQL指定ip和端口连接数据库,并修改数据库密码

    一.指定ip和端口连接数据库 命令 mysql -u root -h (ip) -P (端口)-p 假设ip是:127.0.0.1:端口是:13326,连接的命令: mysql -u root -h ...

  9. 运行Django项目指定IP和端口

    默认IP和端口 python manage.py runserver 指定端口: python manage.py runserver 192.168.12.12:8080 此时会报错,我们需要修改配 ...

随机推荐

  1. js操作数组的一些小技巧

    1.从数组中随机获取成员 var items = [12, 548 , 'a' , 2 , 5478 , 'foo' , 8852, , 'Doe' , 2145 , 119]; var  rando ...

  2. angularjs表单验证checkbox

    angularjs中默认有表单验证的支持,见文末的refer 我想要验证至少要选择一个checkbox,否则就不能提交 但是checkbox貌似没有简单的方法,想来想去给出下面的解决方案 valida ...

  3. Angular2 入门

    1. 说明 该文档为Angular2的入门文档,可以根据该文档的内如做出一个“helloworld”类型的Angualr2入门程序,通过该文档可以初步了解Angular2的相关知识以及开发流程,同时搭 ...

  4. python习题 (1):login

    #!/uer/bin/env python # _*_ coding: utf-8 _*_ import sys retry_limit = 3 retry_count = 0 account_fil ...

  5. 【转】c# Image获得图片路径的三种方法 winform

    代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\ ...

  6. 疑问:line-height对非文字行内块的影响

    line-height:对子元素是非文字的行内块,表现出来的不是垂直居中.目前还不知道具体细节. 可以看出来两个东西不在一行.老师的解释是line-height对非文字元素解释不一样,但是我没懂细节. ...

  7. Codeforces Round #367 (Div. 2)

    A题 Beru-taxi 随便搞搞.. #include <cstdio> #include <cmath> using namespace std; int a,b,n; s ...

  8. [Java] 位运算

    https://ckjoker.github.io/2015/03/01/Java-bit-primary/

  9. Discuz!用户注册,登陆,生成帖子功能实现

    <?php /* * Disucz!部分功能使用说明: */ /***************************************************************** ...

  10. ZeroMQ接口函数之 :zmq_z85_decode – 从一个用Z85算法生成的文本中解析出二进制密码

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_z85_decode zmq_z85_decode(3)         ØMQ Manual - ØMQ/4.1 ...