Python的socket代理脚本

2023-05-17 11:06:10 浏览数 (3)

Python代理脚本

放几个常用的socket脚本

Socket发出请求

代码语言:javascript复制
import socket

def http_sockrt(text):
    global host,port
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect((host, port))
    sock.send(text.encode())
    response = b''
    rec = sock.recv(1024)
    while rec:
        response  = rec
        rec = sock.recv(1024)
    return response.decode()
host = 'www.zhangdongshengtech.com'
port = 80

# 创建TCP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 连接服务端
sock.connect((host, port))
# 创建请求消息头
request_url = 'GET /article-types/6/ HTTP/1.1rnHost: www.zhangdongshengtech.comrnConnection: closernrn'
print(request_url)
# 发送请求
sock.send(request_url.encode())
response = b''
# 接收返回的数据
rec = sock.recv(1024)
while rec:
    response  = rec
    rec = sock.recv(1024)
print(response.decode())

获取请求数据

不断输出请求数据包

代码语言:javascript复制
import socket
import threading

socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket_server.bind(("0.0.0.0", 9999))
socket_server.listen(5)

def socket_thread(num=0):
    try:
        print("Socketer",num, "start Work", )
        client_socket, address = socket_server.accept()
        print("Socketer",num, "connect successfunlly", )
        # 接收客户端的请求
        recvmsg = client_socket.recv(1024)
        # 把接收到的数据进行解码
        strData = recvmsg.decode("utf-8")
        print("-"*25,"n",strData,"-"*25,"n")
        msg = "Server for ou is socketer %d"%num
        # 发送数据,需要进行编码
        client_socket.send(msg.encode("utf-8"))
        # 关闭服务器端
        # socket_server.close()
        threading.Thread(target=socket_thread, args=(num,)).start()
    except:
        pass

print("start")
# for num in range(3):
#     #实际上进程会阻塞,只有一个线程会工作
#     threading.Thread(target=socket_thread,args=(num,)).start()
threading.Thread(target=socket_thread,args=()).start()
print("end")

请求转发(其实不是)

不管请求什么,都返回自己的服务器上的某个资源

代码语言:javascript复制
#!/usr/bin/python2
# coding=utf-8

import time
import socket
import urllib
import random

import urlparse

desc_host = '0.0.0.0'
desc_port = 9999

source_url = "http://127.0.0.1/ctf-temp/"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while 1:
    try:
        server.bind((desc_host, desc_port))
        break
    except:
        # desc_port=random.randint(1000,10000)
        pass

print "Proxying to %s:%s ..."%(desc_host, desc_port)

while 1:
    server.listen(5)
    conn, addr = server.accept()
    recv=conn.recv(1024)
    print recv
    request = recv.split(" ")[1]
    # page = urllib.urlopen(urlparse.urljoin(source_url, request)).read()
    page = urllib.urlopen(urlparse.urljoin("http://47.99.70.18/1.txt","")).read()
    print addr[0], addr[1], request
    print time.strftime('%Y-%m-%d %H:%M:%S')," [%s:%s] %s"%(addr[0], addr[1], request)
    # print page
    head1 = """HTTP/1.1 200 OK
Date: Mon, 18 Apr 2022 11:25:06 GMT
Server: Apache/2.4.38 (Debian)
Last-Modified: Mon, 18 Apr 2022 10:29:12 GMT
ETag: "3ea0-5dceb3b355267"
Accept-Ranges: bytes
Content-Length: 16032
Keep-Alive: timeout=5, max=100
Connection: Keep-Alivetnn""".encode()
    head2=b"""HTTP/1.1 200 OK
Server: gunicorn
Date: Mon, 18 Apr 2022 13:38:20 GMT
Connection: close
Content-Type: text/plain
Content-Length: """ str(len(page)).encode() b"""
Content-Disposition: attachment; filename=333.phptnn"""
    conn.sendall(head2 page)
    print(head2 page)
    conn.close()

返回输入数据

这里和上面没太大区别,就是将返回的内容修改为我们input的数据,交互性强了一点,仅此而已

代码语言:javascript复制
#!/usr/bin/python3
#codind:utf-8
import random
import socket,sys

#创建一个socket对象
socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host="0.0.0.0"
port = 9999
file=b"abcdefg"
if "-h" in sys.argv:
    host = sys.argv[sys.argv.index("-h")   1]
if "-p" in sys.argv:
    port = int(sys.argv[sys.argv.index("-p")   1])
if "-f" in sys.argv:
    fileName = sys.argv[sys.argv.index("-f")   1]
    f = open(fileName, "rb")
    file = f.red()
#绑定地址
while 1:
    try:
        socket_server.bind((host, port))
        break
    except:
        port=random.randint(1000,10000)
print("Your Server Objects is: ",host)
print("Your Server Port is: ",port)
#设置监听
socket_server.listen(5)
# socket_server.accept()返回一个元组, 元素1为客户端的socket对象, 元素2为客户端的地址(ip地址,端口号)
client_socket, address = socket_server.accept()

#while循环是为了让对话持续
while True:
   #接收客户端的请求
    recvmsg = client_socket.recv(1024)
    #把接收到的数据进行解码
    strData = recvmsg.decode("utf-8")
    #设置退出条件
    if strData == 'exit':
        break
    print("接收: n%s" % strData)
    #输入
    if file:
        client_socket.send(file)
        break
    else:
        msg = input("发送: n")
        #发送数据,需要进行编码
        client_socket.send(msg.encode("utf-8"))
        break
#关闭服务器端
socket_server.close()

0 人点赞