Python网络编程积累


import requests #发送request.post脚本
import os
header = {'Cookie': 'PHPSESSID=7700b4d1f9a37e43b5dd61c952eee149'}

url = 'http://xxx.com/xxx.php'
code = '123'
data = {'username': '13388886666', }
r = requests.post(url, data=data, headers=header, timeout=10)
response = r.content.decode('utf-8')
print response

import socket #小服务端脚本    

s=socket.socket() #socket方法有三个参数,都用默认即可

host=socket.gethostname() #在服务端这个方法就是获取自己的主机名
port=1234
s.bind((host,port))  #绑定host:port

s.listen(5)  #只能一个一个连,5表示队列最多5个
while True:
    c,addr=s.accept() #c和addr等于s当前接收的客户端
    print 'Got connection from', addr
    c.send('Thankyouforconnecting')
    c.close()

urlretrieve('http://www.python.org', 'C:\\python_webpage.html') #保存本地脚本
from urllib2 import urlopen #读取页面脚本
webpage = urlopen("http://www.siela.space") #webpage为打开的连接
text = webpage.read() 
#!/usr/bin/env python #分析返回页面数据 并自动计算和发送 脚本
# -*- coding: utf-8 -*- 
# 网上一位大哥的解法
# mathy.py 
# by 0h1in9e weibo@ohlinge
# time:2016/7/14

from requests import *
import re,time

url = "http://lab1.xseclab.com/xss2_0d557e6d2a4ac08b749b61473a075be1/index.php"
s = Session()

r = s.get(url)
l = re.findall(r'
\s+(.*?)=',r.content)[0]
print "需要计算的是 %s = %d\n" % (l,eval(l))

r = s.post(url, data={'v': eval(l)})

print "flag 就藏在下面哦:\n"
print re.findall(r'(.*?)',r.content)[0]
#一个小型服务器

from SocketServer import TCPServer, StreamRequestHandler #如果分叉在这里加上ForkingMinIn, 如果是线程替换为ThreadningMinIn
class Handler(StreamRequestHandler): def handle(self): #如果分叉在上一行和这一行中间加上 class Server(ForkingMinIn, TCPServer):pass
        addr = self.request.getpeername() 
        print 'Got connection from ', addr
        self.wfile.write('Thank you for connecting')

server = TCPServer(('', 1234), Handler) #''表示的是服务器机器名
server.serve_forever()

发表评论