""" (1)UDP C/S结构通信: 服务器端和客户端IP:当前主机IP(192.168.?.?) 服务器监听端口:服务器启动时默认侦听端口为1060,当客户端有udp数据传过来时, 可以对数据进行解码(utf-8)并打印出客户端传输的字符串数据, 同时给发送数据的客户端传输字符串数据“你好,我是服务器+(ip,port),有事情可以随时联系我!” 客户端端口:系统随机选定。 要求客户端启动后给服务器发送一个字符串数据“服务器你好,我是客户端+(ip,port)”, 发送完数据后客户端便处于等待服务器端使用udp协议回复信息状态,接收到回复信息后,在屏幕上输出解码后的数据。 """ import argparse, socket, time from datetime import datetime MAX_BYTES = 65535 def server(port): #创建udp对象 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #绑定ip地址+端口号 sock.bind(('127.0.0.1', port)) #加入提示词 print('Listening at {}'.format(sock.getsockname())) while True: data, address = sock.recvfrom(MAX_BYTES) text = data.decode('utf-8') print('The client at {} says {!r}'.format(address, text)) text = 'Your data was {} bytes long'.format(len(data)) data = text.encode('ascii') time.sleep(30) sock.sendto(data, address) def client(port): #创建udp对象 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) #text为当前时间 text = 'The time is {}'.format(datetime.now()) data = text.encode('ascii') sock.sendto(data, ('127.0.0.1', port)) print('The OS assigned me the address {}'.format(sock.getsockname())) data, address = sock.recvfrom(MAX_BYTES) text = data.decode('ascii') print('The server {} replied {!r}'.format(address, text)) if __name__ == '__main__': choices = {'client': client, 'server': server} parser = argparse.ArgumentParser(description='Send and receive UDP locally') parser.add_argument('role', choices=choices, help='which role to play') parser.add_argument('-p', metavar='PORT', type=int, default=1060, help='UDP port (default 1060)') args = parser.parse_args() function = choices[args.role] function(args.p)
""" 扫描主机端口状态 建立程序能够对本机的常用端口状态进行检测,并将扫描到的活动端口信息写入到scanResult.txt文件中 常用的端口列表portLs=[21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] """ import socket def main(): ip_start = input('请输入开始IP:(默认:127.0.0.1)') #若无输入则以127.0.0.1 if ip_start == '': ip_start = '127.0.0.1' ip_end = '127.0.0.1' #否则还要输入结束IP else: ip_end = input('请输入结束IP:') if ip_end == '': ip_end = '127.0.0.1' s = input('请输入目标主机开始端口:(默认扫描常用端口)') if s == '': portList = [21, 22, 23, 25, 80, 135, 137, 139, 445, 1433, 1502, 3306, 3389, 8080, 9015] else: startport = int(s) s = input('请输入目标主机结束端口:(默认:65535)') if s == '': endport = 65535 else: endport = int(s) portList = [i for i in range(startport, endport + 1)] # 思考:这里为什么要+1? while True: # ip_start<ip_end x1 = ip_start.rfind('.'); # x1存放起始ip的最后一位,i.e., 127.0.0.1 x2 = ip_end.rfind('.') # x2存放终止ip的最后一位 if int(ip_start[x1 + 1:]) > int(ip_end[x2 + 1:]): # 判断起始ip的最后一位是否大于终止ip的最后一位,假定在一个C类网段内 break; # 开始扫描端口 for port in portList: print('正在扫描%s :%d' % (ip_start, port)) try: sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sk.settimeout(10) sk.connect((ip_start, port)) sk.settimeout(None) print('Server %s port %d OK!' % (ip_start, port)) sk.close() # 结果保存在文件中 f = open("IP_Port.txt", 'a') f.write(ip_start + ' : ' + str(port) + ' ') f.close() except Exception: print('Server %s port %d is not connected!' % (ip_start, port)) # 更新ip_start,ip_start增1 i = ip_start.rfind('.') x = int(ip_start[i + 1:]) + 1 ip_start = ip_start[:i + 1] + str(x) if __name__ == '__main__': main() print('扫描完成,结果保存在IP_Port.txt文件中')