python通过urlunsplit函数将网址的多个部分合成一个完整的urlimport urlparseprint urlparse.urlunsplit(('http','www.sharejs.org:80', '/faq.cgi','src=fie','……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2278浏览 1487个赞
python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信import SocketServerimport netstringimport sysimport stringclass MyRequestHandler(SocketServer……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2241浏览 1802个赞
python连接url的不同部分,例如将http://www.75271.com/ 和 /other/path连接成一个完整的urlimport urlparseprint urlparse.urljoin('http://www.75271.com/', '/other/path')……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1434浏览 2111个赞
python中迭代器(iterator)的用法#------------------------------------------------------------------------------# Name: iterators.py# Author: Kevin Harris# Last Modi……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1477浏览 887个赞
python编写的简单的socket serverimport sockethost = ''port = 55555myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)myServerSocket.setsockopt(socket.SOL……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3225浏览 255个赞
下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1368浏览 1916个赞
python执行get提交的操作import sys, urllib2, urllibdef addGETdata(url, data): """Adds data to url. Data should be a list or tuple consisting of 2-item lists o……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1354浏览 2529个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3021浏览 1128个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2112浏览 119个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1363浏览 1704个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2452浏览 1270个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2971浏览 916个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1821浏览 2342个赞
通过tcp实现字符大写转换的python客户端代码,服务端代码参考:http://www.75271.com/codes/python/8081import socketimport netstrings = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(('1……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2677浏览 2688个赞
python访问系统环境变量#------------------------------------------------------------------------------# Name: enviroment_variables.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1987浏览 2195个赞
python获得linux下的所有挂载点(mount points)# execute the external "mount" command and parse the output.import commands mount = commands.getoutput('mount -v')line……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2355浏览 738个赞
python为socket handler创建一个处理类及调用方法import SocketServerport = 8000class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3221浏览 840个赞
带错误处理的python socket server服务范例import socket, tracebackhost = ''port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2865浏览 1321个赞
python编写的一个简单的socket c/s用于发送和接受数据包 服务端代码import socketHOST = "127.0.0.1"PORT = 5000mySocket = socket.socket( socket.AF_INET, s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2683浏览 170个赞
python获得本地socket可用的设置信息import socketsolist = [x for x in dir(socket) if x.startswith('SO_')]solist.sort()for x in solist: print x ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2071浏览 2876个赞
python实现绑定到特定地址和端口的socket serverimport socket, tracebackhost = '127.0.0.1'port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SO……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2310浏览 1457个赞
python通过socket获得点对点的信息import socketprint "Creating socket...",s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "done."print "Looking up ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1894浏览 363个赞
python通过socket远程连接错误处理代码演示import socket, syshost = sys.argv[1]textport = sys.argv[2]filename = sys.argv[3]try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)e……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2123浏览 139个赞
python字典遍历演示代码#the key and corresponding value can be retrieved at the same time using the #iteritems() method.knights = {'Key 1': 'value 1', 'key 2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3653浏览 1894个赞
python清除字典内的全部数据(clear)d = {}d['name'] = 'Gumby'd['age'] = 42print dreturned_value = d.clear()print dprint returned_value……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2022浏览 1286个赞
python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂print 3 in [1, 2, 3] # membership (1 means trueinventory = ["sword", "armor", "shield&qu……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1434浏览 296个赞
python获取元素在数组中的索引号,python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1453浏览 2624个赞
python查找两个字符串中相同的字符并输出seq1 = "spam" seq2 = "scam" res = [] for x in seq1: if x in seq2: ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3244浏览 2999个赞
python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2591浏览 2562个赞
python字典复制演示代码L = [1,2,3]D = {'a':1, 'b':2}A = L[:] # instead of: A = L (or list(L))B = D.copy() # instead of: B = DA[1] = 'Ni……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1817浏览 712个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2641浏览 2966个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2797浏览 2713个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3175浏览 187个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1769浏览 1194个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2581浏览 945个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2294浏览 575个赞
python检测是文件和是目录import os if os.path.isdir(path): print "it's a directory" elif os.path.isfile(path): print "it's a normal file" ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1999浏览 1124个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2633浏览 1033个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3468浏览 1895个赞
python创建临时文件夹import tempfile, os tempfd, tempname = tempfile.mkstemp('.suffix') os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(te……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1893浏览 2129个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/usr/bin/python#+# This script computes the number of different ways that combinations# of a given set of coin denominations can add up to a s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3310浏览 1516个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3024浏览 768个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3408浏览 2705个赞
归并排序python实现代码def mergesort(arr): if len(arr) == 1: return arr m = len(arr) / 2 l = mergesort(arr[:m]) r = mergesort(arr[m:]) i……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2842浏览 2517个赞
python列出指定目录下的文件和子目录# if you know the exact name: import os files = os.listdir('/path/to/dir/') # if you want shell-style globbing: import glob files = gl……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2799浏览 956个赞