怎样使用Python socket套接字模块

2018年6月13日 | 分类: 【技术】

参考:http://www.iplaypy.com/module/socket.html

标题:python通过socket查询whois的方法
参考:http://www.aspku.com/tech/jiaoben/python/57419.html
测试:

import socket 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect(('whois.networksolutions.com', 43)) 
s.send('sina.com.cn \r\n') 
while 1: 
v = s.recv(1024) 
if v == '' or v == None:
break
print v
s.close()

标题:python通过socket查询whois的方法
参考:http://www.cnblogs.com/zhangmengqin/p/9144022.html
测试:Py3.6实证成功

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('whois.internic.net', 43))
s.send(b"sina.com.cn \r\n")
response =b''
while True:
	data = s.recv(4096)
	response += data
	if not data :
		break
s.close()
print (response.decode())

标题:python通过socket查询whois的方法
参考:http://www.aichengxu.com/python/1256124.htm
测试:Py3.6实证成功

from socket import *

HOST = '202.12.29.220'
PORT = 43
BUFSIZ = 1024
ADDR = (HOST, PORT)
EOF="\r\n"
request_ip="219.146.3.110"

data_send=request_ip+EOF
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
tcpCliSock.send(data_send)

while True: 
    data_rcv = tcpCliSock.recv(BUFSIZ)
    if not len(data_rcv):
        break  
    print data_rcv

tcpCliSock.close()