python通过urlunsplit函数将网址的多个部分合成一个完整的urlimport urlparseprint urlparse.urlunsplit(('http','www.sharejs.org:80', '/faq.cgi','src=fie','……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1553浏览 1840个赞
python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信import SocketServerimport netstringimport sysimport stringclass MyRequestHandler(SocketServer……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2845浏览 167个赞
python连接url的不同部分,例如将http://www.75271.com/ 和 /other/path连接成一个完整的urlimport urlparseprint urlparse.urljoin('http://www.75271.com/', '/other/path')……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2835浏览 358个赞
python中迭代器(iterator)的用法#------------------------------------------------------------------------------# Name: iterators.py# Author: Kevin Harris# Last Modi……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1523浏览 1551个赞
python编写的简单的socket serverimport sockethost = ''port = 55555myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)myServerSocket.setsockopt(socket.SOL……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2513浏览 2916个赞
下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1567浏览 783个赞
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) 2470浏览 2190个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2355浏览 801个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1691浏览 2786个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1325浏览 1271个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2707浏览 2765个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2859浏览 216个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2202浏览 1991个赞
通过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) 3072浏览 818个赞
python访问系统环境变量#------------------------------------------------------------------------------# Name: enviroment_variables.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2000浏览 770个赞
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) 1416浏览 1146个赞
python为socket handler创建一个处理类及调用方法import SocketServerport = 8000class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3150浏览 1760个赞
带错误处理的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) 2363浏览 843个赞
python编写的一个简单的socket c/s用于发送和接受数据包 服务端代码import socketHOST = "127.0.0.1"PORT = 5000mySocket = socket.socket( socket.AF_INET, s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1628浏览 716个赞
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) 2140浏览 2520个赞
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) 3384浏览 101个赞
python通过socket获得点对点的信息import socketprint "Creating socket...",s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "done."print "Looking up ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1615浏览 1504个赞
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) 2663浏览 652个赞
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) 3157浏览 141个赞
python清除字典内的全部数据(clear)d = {}d['name'] = 'Gumby'd['age'] = 42print dreturned_value = d.clear()print dprint returned_value……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2582浏览 398个赞
python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂print 3 in [1, 2, 3] # membership (1 means trueinventory = ["sword", "armor", "shield&qu……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1839浏览 1024个赞
python获取元素在数组中的索引号,python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2165浏览 2267个赞
python查找两个字符串中相同的字符并输出seq1 = "spam" seq2 = "scam" res = [] for x in seq1: if x in seq2: ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3290浏览 706个赞
python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3076浏览 651个赞
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) 3130浏览 1580个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3518浏览 765个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2503浏览 2782个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 4505浏览 1618个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2288浏览 1753个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2237浏览 501个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1502浏览 2457个赞
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) 3385浏览 2821个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3961浏览 701个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3410浏览 1095个赞
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) 1674浏览 752个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 1673浏览 1994个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3100浏览 607个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 4061浏览 1288个赞
归并排序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) 1870浏览 1079个赞
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) 2114浏览 2206个赞