下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2416浏览 107个赞
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) 1566浏览 1629个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2533浏览 2449个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2795浏览 2638个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1312浏览 2190个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2638浏览 452个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3002浏览 2748个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1656浏览 2329个赞
通过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) 3026浏览 765个赞
python访问系统环境变量#------------------------------------------------------------------------------# Name: enviroment_variables.py# Author: Kevin Harris# Last Mo……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2804浏览 2795个赞
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) 1481浏览 2289个赞
python为socket handler创建一个处理类及调用方法import SocketServerport = 8000class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2705浏览 1183个赞
带错误处理的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) 2536浏览 1855个赞
python编写的一个简单的socket c/s用于发送和接受数据包 服务端代码import socketHOST = "127.0.0.1"PORT = 5000mySocket = socket.socket( socket.AF_INET, s……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2875浏览 1059个赞
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) 1353浏览 2657个赞
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) 3148浏览 560个赞
python通过socket获得点对点的信息import socketprint "Creating socket...",s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "done."print "Looking up ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3122浏览 2648个赞
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) 2636浏览 471个赞
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) 3565浏览 1684个赞
python清除字典内的全部数据(clear)d = {}d['name'] = 'Gumby'd['age'] = 42print dreturned_value = d.clear()print dprint returned_value……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2087浏览 1080个赞
python获取元素在数组中的索引号,python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2642浏览 856个赞
python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂print 3 in [1, 2, 3] # membership (1 means trueinventory = ["sword", "armor", "shield&qu……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2604浏览 1548个赞
python查找两个字符串中相同的字符并输出seq1 = "spam" seq2 = "scam" res = [] for x in seq1: if x in seq2: ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3058浏览 946个赞
python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3292浏览 1207个赞
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) 3590浏览 2343个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2427浏览 2756个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2837浏览 1845个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2895浏览 681个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2683浏览 2738个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2872浏览 2594个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2060浏览 1618个赞
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) 3836浏览 1096个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2630浏览 2067个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2334浏览 1211个赞
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) 1451浏览 849个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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) 3359浏览 965个赞
这个函数给定日期,输出星期几,到底0是星期一还是星期天,这和时区有关,反正我这是0表示星期一def get_week_day(date): week_day_dict = { 0 : '星期一', 1 : '星期二', 2 : '星期三'……继续阅读 » 水墨上仙 5年前 (2021-01-15) 2785浏览 2143个赞
python读取文件到字节数组def get_bytes_from_file(filename): return open(filename, "rb").read() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3002浏览 2546个赞
归并排序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) 1731浏览 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) 3295浏览 2932个赞
python实现插入法排序算法def insertsort(array): for removed_index in range(1, len(array)): removed_value = array[removed_index] insert_index = removed_index ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3027浏览 1959个赞
python获得当前工作目录import os curDir = os.getcwd() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3485浏览 1922个赞
python生成随机密码或随机字符串import string,random def makePassword(minlength=5,maxlength=25): length=random.randint(minlength,maxlength) letters=string.ascii_letters+string.digit……继续阅读 » 水墨上仙 5年前 (2021-01-15) 1874浏览 2951个赞
python信息替换演示代码Escape = "^" def subst(Msg, *Args) : """substitutes Args into Msg.""" Result = "" while Tr……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3268浏览 276个赞
python创建文件的方法file("filename", "w").close() ……继续阅读 » 水墨上仙 5年前 (2021-01-15) 3054浏览 1766个赞