Skip to content

Instantly share code, notes, and snippets.

@liuguiyangnwpu
Last active December 8, 2016 11:18
Show Gist options
  • Save liuguiyangnwpu/16564677621318dd817d0131b5df9521 to your computer and use it in GitHub Desktop.
Save liuguiyangnwpu/16564677621318dd817d0131b5df9521 to your computer and use it in GitHub Desktop.
socket-python.md
  • server
#coding=utf-8

#创建SocketServerTCP服务器:
import SocketServer
from SocketServer import StreamRequestHandler as SRH
from time import ctime

host = '127.0.0.1'
port = 9999
addr = (host,port)

class Servers(SRH):
    def handle(self):
        print 'got connection from ',self.client_address
        self.wfile.write('connection %s:%s at %s succeed!' % (host,port,ctime()))
        while True:
            data = self.request.recv(1024)
            if not data: 
                break
            print data
            print "RECV from ", self.client_address[0]
            self.request.send("Hello")
print 'server is running....'
print addr
server = SocketServer.ThreadingTCPServer(addr,Servers)
server.serve_forever()
  • client
import re
import os

def fetch_url(filename):
    if os.path.exists(filename) is False:
        print "filepath does not exists !"
        return -1
    url_list = []
    with open(filename, 'r') as handle:
        for line in handle:
            line = line.strip()
            target_url = re.findall(r'"target_url":"(.+?)"', line)[0]
            target_url = target_url.replace("\/", "/")
            cur_url = re.findall(r'"cur_url":"(.+?)"', line)[0]
            cur_url = "http://" + cur_url.replace("\/", "/")
            if cur_url.split("?")[0].split("/")[-1] == "none":
                #print target_url, cur_url
                pass
            else:
                print target_url, cur_url

if __name__ == "__main__":
    filename = "./a.log"
    fetch_url(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment