python使用utf-8编码编写代码,只需要在.py文件头部加上这句代码即可,加上后可以支持中文# -*- coding: utf-8 -*-……继续阅读 » 4年前 (2021-01-15) 1619浏览 862个赞
python中字典(dict)和字符串之间的相互转换 字典(dict)转为字符串(string)我们可以比较容易的将字典(dict)类型转为字符串(string)类型。通过遍历dict中的所有元素就可以实现字典到字符串的转换:for key, value in sample_d……继续阅读 » 4年前 (2021-01-15) 1371浏览 764个赞
python中sleep函数用法演示,sleep用来暂停线程执行,单位为秒#------------------------------------------------------------------------------# Name: sleep.py# Author: Kevin Harris# ……继续阅读 » 4年前 (2021-01-15) 2945浏览 1552个赞
下面的python代码用于监控本机的8080端口,当用于通过http请求,服务器返回固定的html代码import SocketServerclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(self): print "From:&q……继续阅读 » 4年前 (2021-01-15) 3023浏览 1303个赞
python通过urlunsplit函数将网址的多个部分合成一个完整的urlimport urlparseprint urlparse.urlunsplit(('http','www.sharejs.org:80', '/faq.cgi','src=fie','……继续阅读 » 4年前 (2021-01-15) 1491浏览 1348个赞
python实现的多线程字符转换成大写的tcp服务端代码,下面的代码用于将用户通过tcp发过来的字符串转换成大写后返回,如果客户端发送过来空字符串,则结束通信import SocketServerimport netstringimport sysimport stringclass MyRequestHandler(SocketServer……继续阅读 » 4年前 (2021-01-15) 1248浏览 1779个赞
python连接url的不同部分,例如将http://www.75271.com/ 和 /other/path连接成一个完整的urlimport urlparseprint urlparse.urljoin('http://www.75271.com/', '/other/path')……继续阅读 » 4年前 (2021-01-15) 2737浏览 345个赞
python中迭代器(iterator)的用法#------------------------------------------------------------------------------# Name: iterators.py# Author: Kevin Harris# Last Modi……继续阅读 » 4年前 (2021-01-15) 2627浏览 2608个赞
python编写的简单的socket serverimport sockethost = ''port = 55555myServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)myServerSocket.setsockopt(socket.SOL……继续阅读 » 4年前 (2021-01-15) 2102浏览 1369个赞
下面的python代码执行后通过tcp监控8081端口,用于将用户发送的请求字符串转换成大写后返回,如果用户发送的是end,则中断连接import SocketServerimport netstringclass MyRequestHandler(SocketServer.BaseRequestHandler): def handle(……继续阅读 » 4年前 (2021-01-15) 1770浏览 2949个赞
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……继续阅读 » 4年前 (2021-01-15) 2812浏览 2284个赞
python打开url并按指定快大写读取网页内容import urllibpagehandler = urllib.urlopen("http://www.google.com")outputfile = open("index.html", "wb")while 1: data……继续阅读 » 4年前 (2021-01-15) 1786浏览 1060个赞
python对一个完整的url进行分割,将url分割成单独的部分,包括协议、域名、端口、路径、参数等等import urlparseprint urlparse.urlsplit('http://www.75271.com:80/faq.cgi?src=fie') ……继续阅读 » 4年前 (2021-01-15) 3116浏览 530个赞
python if else语句使用演示#------------------------------------------------------------------------------# Name: if_conditional.py# Author: Kevin Harris# Last Mo……继续阅读 » 4年前 (2021-01-15) 1646浏览 1581个赞
下面的代码可以先创建一个目录,然后调用自定义的deleteDir函数删除整个目录#------------------------------------------------------------------------------# Name: create_directory.py# Author: K……继续阅读 » 4年前 (2021-01-15) 1938浏览 664个赞
python自定义函数演示,计算Fibonacci数列#------------------------------------------------------------------------------# Name: function.py# Author: Kevin Harris# Last M……继续阅读 » 4年前 (2021-01-15) 1480浏览 563个赞
python实现的一个简单的用于等待连接的socket服务器代码 import sockethost = '' port = 53333s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsocko……继续阅读 » 4年前 (2021-01-15) 2021浏览 173个赞
通过tcp实现字符大写转换的python客户端代码,服务端代码参考:http://www.75271.com/codes/python/8081import socketimport netstrings = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.connect(('1……继续阅读 » 4年前 (2021-01-15) 1383浏览 2733个赞
python访问系统环境变量#------------------------------------------------------------------------------# Name: enviroment_variables.py# Author: Kevin Harris# Last Mo……继续阅读 » 4年前 (2021-01-15) 3096浏览 2781个赞
python获得linux下的所有挂载点(mount points)# execute the external "mount" command and parse the output.import commands mount = commands.getoutput('mount -v')line……继续阅读 » 4年前 (2021-01-15) 2882浏览 1103个赞
python为socket handler创建一个处理类及调用方法import SocketServerport = 8000class myRequestHandler(SocketServer.StreamRequestHandler): def handle(self): print "connection ……继续阅读 » 4年前 (2021-01-15) 2533浏览 286个赞
带错误处理的python socket server服务范例import socket, tracebackhost = ''port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SOL_SOCKET, s……继续阅读 » 4年前 (2021-01-15) 2671浏览 849个赞
python编写的一个简单的socket c/s用于发送和接受数据包 服务端代码import socketHOST = "127.0.0.1"PORT = 5000mySocket = socket.socket( socket.AF_INET, s……继续阅读 » 4年前 (2021-01-15) 2320浏览 2234个赞
python获得本地socket可用的设置信息import socketsolist = [x for x in dir(socket) if x.startswith('SO_')]solist.sort()for x in solist: print x ……继续阅读 » 4年前 (2021-01-15) 1860浏览 662个赞
python实现绑定到特定地址和端口的socket serverimport socket, tracebackhost = '127.0.0.1'port = 51423s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)s.setsockopt(socket.SO……继续阅读 » 4年前 (2021-01-15) 2410浏览 203个赞
python通过socket获得点对点的信息import socketprint "Creating socket...",s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)print "done."print "Looking up ……继续阅读 » 4年前 (2021-01-15) 1505浏览 2446个赞
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……继续阅读 » 4年前 (2021-01-15) 2156浏览 696个赞
python字典遍历演示代码#the key and corresponding value can be retrieved at the same time using the #iteritems() method.knights = {'Key 1': 'value 1', 'key 2……继续阅读 » 4年前 (2021-01-15) 2469浏览 1250个赞
python清除字典内的全部数据(clear)d = {}d['name'] = 'Gumby'd['age'] = 42print dreturned_value = d.clear()print dprint returned_value……继续阅读 » 4年前 (2021-01-15) 2181浏览 2379个赞
python判断数组是否包含指定的元素的方法,直接使用in即可,python真是简单易懂print 3 in [1, 2, 3] # membership (1 means trueinventory = ["sword", "armor", "shield&qu……继续阅读 » 4年前 (2021-01-15) 1961浏览 2600个赞
python获取元素在数组中的索引号,python是通过index方法获取索引号的li = ['a', 'b', 'new', 'D', 'z', 'example', 'new', 'two'……继续阅读 » 4年前 (2021-01-15) 1475浏览 2294个赞
python查找两个字符串中相同的字符并输出seq1 = "spam" seq2 = "scam" res = [] for x in seq1: if x in seq2: ……继续阅读 » 4年前 (2021-01-15) 3551浏览 140个赞
python获取数组元素的几种方法L = ['spam', 'Spam', 'SPAM!']print L[2] # offsets start at zeroprint L[-2] ……继续阅读 » 4年前 (2021-01-15) 2510浏览 1379个赞
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……继续阅读 » 4年前 (2021-01-15) 3257浏览 447个赞
python字典基本操作范例代码d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2……继续阅读 » 4年前 (2021-01-15) 1681浏览 2044个赞
python中字典赋值方法x = {}y = xx['key'] = 'value'print yx = {}print yx = {}y = xy['key'] = 'value'print yprint x.clear()print y……继续阅读 » 4年前 (2021-01-15) 3097浏览 1882个赞
python修改字典内key对应的值d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionaryprint d2 # order is scrambledd2[……继续阅读 » 4年前 (2021-01-15) 2651浏览 771个赞
python保存字符串到文件def save(filename, contents): fh = open(filename, 'w') fh.write(contents) fh.close() save('file.name', 'some stuff&……继续阅读 » 4年前 (2021-01-15) 1516浏览 1507个赞
python从文件读取文本# get contents of file as a list, one line per element data = open('/path/to/file').readlines() ……继续阅读 » 4年前 (2021-01-15) 1851浏览 1993个赞
python按单词翻转字符串,如 hello world 翻转成world hellodef reverseWords(s): return ' '.join(reversed(s.split(' '))) ……继续阅读 » 4年前 (2021-01-15) 1888浏览 2177个赞
python检测是文件和是目录import os if os.path.isdir(path): print "it's a directory" elif os.path.isfile(path): print "it's a normal file" ……继续阅读 » 4年前 (2021-01-15) 1661浏览 1331个赞
python返回3天后的日期import datetimeday3 = datetime.datetime.now() + datetime.timedelta(days=3)……继续阅读 » 4年前 (2021-01-15) 1652浏览 1507个赞
python删除指定文件import os try: os.unlink('/path/to/file') except OSError: pass # Deletion failed... ……继续阅读 » 4年前 (2021-01-15) 1708浏览 1017个赞
python创建临时文件夹import tempfile, os tempfd, tempname = tempfile.mkstemp('.suffix') os.write(tempfd, "aString") # or, if you want a file-object: os.fdopen(te……继续阅读 » 4年前 (2021-01-15) 1298浏览 269个赞
给定一组指定面额的硬币,有多少中方法可以组合成指定的金额算法#!/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……继续阅读 » 4年前 (2021-01-15) 1755浏览 811个赞