尝试使用fileReader.readAsBinaryString通过AJAX将PNG文件上传到服务器,剥离代码(fileObject是包含我文件信息的对象);
var fileReader = new FileReader();
fileReader.onload = function(e) {
var xmlHttpRequest = new XMLHttpRequest();
//Some AJAX-y stuff - callbacks, handlers etc.
xmlHttpRequest.open("POST", '/pushfile', true);
var dashes = '--';
var boundary = 'aperturephotoupload';
var crlf = "\r\n";
//Post with the correct MIME type (If the OS can identify one)
if ( fileObject.type == '' ){
filetype = 'application/octet-stream';
} else {
filetype = fileObject.type;
}
//Build a HTTP request to post the file
var data = dashes + boundary + …
Run Code Online (Sandbox Code Playgroud) 试图掌握Python中的正则表达式,我试图输出一些在URL的一部分中突出显示的HTML.我的意见是
images/:id/size
Run Code Online (Sandbox Code Playgroud)
我的输出应该是
images/<span>:id</span>/size
Run Code Online (Sandbox Code Playgroud)
如果我在Javascript中这样做
method = 'images/:id/size';
method = method.replace(/\:([a-z]+)/, '<span>$1</span>')
alert(method)
Run Code Online (Sandbox Code Playgroud)
我得到了理想的结果,但是如果我用Python做的话
>>> method = 'images/:id/huge'
>>> re.sub('\:([a-z]+)', '<span>$1</span>', method)
'images/<span>$1</span>/huge'
Run Code Online (Sandbox Code Playgroud)
我没有,我如何让Python返回正确的结果而不是$1
?是re.sub
连做这个正确的功能?
在SQL Server Management Studio中,如果我运行一个类似的查询
SELECT TOP 1000 *
FROM SomeDB
SELECT TOP 1000 *
FROM SomeOtherDB
Run Code Online (Sandbox Code Playgroud)
结果显示在一个选项卡中,但在中间水平分割,有没有办法让多个查询的结果显示在多个选项卡中?
我们在过去一周左右开始运营一个URL缩短器,我们开始看到{normal url}/no_facebook_preview_picture.jpg
来自Facebook拥有的IP和用户代理的许多奇怪的请求facebookexternalhit/1.0 (+http://www.facebook.com/externalhit_uatext.php)
如果我在我的墙上发布我们站点的正常链接(设置为Only Me
我可以测试),我在访问日志中获得以下条目
66.220.152.6 - - [05/Feb/2013:16:31:36 +0000] "GET /44_U HTTP/1.1" 200 1314 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-"
Run Code Online (Sandbox Code Playgroud)
但是,如果我发布一个返回404或410的链接(创建后删除垃圾链接),我会得到这个
69.171.237.15 - - [05/Feb/2013:16:49:16 +0000] "GET /notexistURL HTTP/1.1" 404 1319 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-"
Run Code Online (Sandbox Code Playgroud)
然后在一个小时左右
173.252.110.113 - - [05/Feb/2013:17:15:15 +0000] "GET /notexistURL/no_facebook_preview_picture.jpg HTTP/1.1" 404 0 "-" "facebookexternalhit/1.0 (+http://www.facebook.com/externalhit_uatext.php)" "-"
Run Code Online (Sandbox Code Playgroud)
该IP的WhoIs报告
NetName FACEBOOK-INC
NetHandle NET-173-252-64-0-1
Run Code Online (Sandbox Code Playgroud)
所以他们肯定是Facebook的IP.
我们每天都会收到大约10-20个这样的请求,都是相同的.我们只能获得7天的日志文件,但这些请求发生在7天前.
我已经测试了独特的链接,因此没有其他方法可以找到该链接.我没有亲自使用Facebook,除了我的测试链接之外的所有链接都是由其他用户创建/发布的,但我认识到链接到我的Facebook帐户的所有应用程序并没有什么不寻常的,所以我不认为这是第三方应用程序(我可以提供一个列表,如果需要,但他们都是大牌应用程序)
在我检查日志文件期间,Facebook甚至似乎没有智能地创建这些请求,它只是盲目地将字符串粘贴在/no_facebook_preview_picture.jpg
URL的末尾,即使使用查询字符串也是如此.例如;
69.171.228.114 - - [05/Feb/2013:17:19:13 +0000] "GET /iAmNotARealURL1234777?ref=fb&cows_go=moo HTTP/1.1" 404 1118 "-" "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" "-"
69.171.228.114 …
Run Code Online (Sandbox Code Playgroud) 组合变量和要在Python中打印的字符串时,似乎有很多方法可以做同样的事情;
test = "Hello"
print "{} World".format(test) #Prints 'Hello World'
print test+" World" #Prints 'Hello World'
print "%s World" % test #Prints 'Hello World'
Run Code Online (Sandbox Code Playgroud)
这些方法在性能,兼容性和一般偏好方面的差异(如果有的话).即使在开源项目之间,所有三种方法似乎都可以互换使用.
我正在尝试创建一个Gatling场景,需要在测试期间将协议切换到不同的主机.用户旅程是
https://example.com/page1
https://example.com/page2
https://accounts.example.com/signin
https://example.com/page3
Run Code Online (Sandbox Code Playgroud)
因此,作为单个场景的一部分,我需要protocol
在场景设置中定义的ether switch ,或者切换baseUrl
协议上定义但我无法弄清楚如何做到这一点.
基本情况可能看起来像
package protocolexample
import io.gatling.core.Predef._
import io.gatling.http.Predef._
class Example extends Simulation {
val exampleHttp = http.baseURL("https://example.com/")
val exampleAccountsHttp = http.baseURL("https://accounts.example.com/")
val scn = scenario("Signin")
.exec(
http("Page 1").get("/page1")
)
.exec(
http("Page 2").get("/page2")
)
.exec(
// This needs to be done against accounts.example.com
http("Signin").get("/signin")
)
.exec(
// Back to example.com
http("Page 3").get("/page3")
)
setUp(
scn.inject(
atOnceUsers(3)
).protocols(exampleHttp)
)
}
Run Code Online (Sandbox Code Playgroud)
我只需要弄清楚如何以太网切换主机或协议进行第3步.我知道我可以创建多个场景,但这需要是跨多个主机的单个用户流.
我试过直接使用其他协议
exec(
// This needs to be done against accounts.example.com …
Run Code Online (Sandbox Code Playgroud) 在一个MongoEngine模型中,当我使用时,我正在使用参考字段
schedule = ReferenceField('Schedule',required=True)
Run Code Online (Sandbox Code Playgroud)
并尝试插入文档
#my_schedule being a 'Schedule' object that has been created and saved successfully
record.schedule = my_schedule
record.save()
Run Code Online (Sandbox Code Playgroud)
我明白了
ValidationError: ValidationError (Calling:None) (A ReferenceField only accepts DBRef or documents: ['schedule'])
Run Code Online (Sandbox Code Playgroud)
但是,如果我将字段定义更改为
schedule = ReferenceField(path.to.Schedule,required=True)
Run Code Online (Sandbox Code Playgroud)
(IE直接引用Schedule模型)
该文档可以成功保存.我怎样才能避免这个错误?
对计划模型的完全捍卫
class Schedule(Document):
uid = StringField(required=True)
start = DateTimeField(required=True)
end = DateTimeField(required=True)
days = ListField(required=True)
toc = StringField(required=False)
meta = {
'indexes':['uid']
}
Run Code Online (Sandbox Code Playgroud)
并呼吁
class Calling(Document):
"""
Calling Point
"""
schedule = ReferenceField('Schedule',required=True)
tiploc = StringField(required=True)
calling = ListField(StringField(required=True))
arrive = …
Run Code Online (Sandbox Code Playgroud) 我经常使用 Tornado,但这是我第一次遇到这种错误。我一直在研究一个非常基本的 URL 缩短器。URL 由不同的应用程序放入数据库,该应用程序仅从 MongoDB 存储中读取 URL 并重定向客户端。在编写基本代码后,我针对它设置了一个简单的“Siege”测试,在运行 siege(针对siege -c 64 -t 5m -r 1 http://example.com/MKy
4 个应用程序线程运行)大约 30 秒后,我开始收到 500 个响应。查看错误日志我看到了这个;
ERROR:root:500 GET /MKy (127.0.0.1) 2.05ms
ERROR:root:Exception in I/O handler for fd 4
Traceback (most recent call last):
File "/opt/python2.7/lib/python2.7/site-packages/tornado-2.1-py2.7.egg/tornado/ioloop.py", line 309, in start
File "/opt/python2.7/lib/python2.7/site-packages/tornado-2.1-py2.7.egg/tornado/netutil.py", line 314, in accept_handler
File "/opt/python2.7/lib/python2.7/socket.py", line 200, in accept
error: [Errno 24] Too many open files
ERROR:root:Uncaught exception GET /MKy (127.0.0.1)
HTTPRequest(protocol='http', host='shortener', method='GET', uri='/MKy', version='HTTP/1.0', remote_ip='127.0.0.1', body='', headers={'Host': 'shortener', …
Run Code Online (Sandbox Code Playgroud) 由于关于模型的游戏文档很糟糕,我会问这里.我有基本代码;
public static void Controller() {
List<Item> item = Item.find("SELECT itemname,id FROM Item WHERE itembool = true ORDER BY itemcreated ASC LIMIT 0,1").fetch();
if ( item == null ) {
notFound();
}
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是获取从SQL查询返回的第一个值返回的'itemname'的值(真正的查询要复杂得多,其他的东西也不能用方法替换).我可以获得整个第一个对象,item.get(0)
但我无法弄清楚如何将'itemname'的值作为字符串获取,并且它似乎没有记录在任何地方.
可能应该在原始问题中提到,我需要通过字段名称而不是索引来检索.IE我不能做items.get(0)[0];
我需要做的items.get(0)['itemname'];
我有3个文件,文件1导入文件2,文件2导入文件3和文件3需要在文件1中引用某些东西.一个基本的例子是
#---------------------File1.py---------------------
import File2
class core():
def __init__(self):
self.something = "Hello World"
self.run = File2.run()
c = core()
#---------------------File2.py---------------------
import File3
class run():
def __init__(self):
self.somethingelse = "Cows"
File3.func()
#---------------------File3.py---------------------
import File1
class func():
print File1.c.something
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,File3需要访问File1中存在的变量'c.something',该变量是调用文件2的类的一部分,后者又调用文件3.
这是来自我已经"固有"的一段代码,其余代码非常复杂,所以我真的不想改变它.为了给出一个更现实的例子,这里基本上是实际代码的作用;
#---------------------File1.py---------------------
import File2
import config
class core():
def __init__(self):
self.config = config.load() #Config.load is an expensive DB opperation, and should only be run once
self.run = File2.run()
c = core()
#---------------------File2.py---------------------
import File3
class run():
def __init__(self):
#
# Do some things …
Run Code Online (Sandbox Code Playgroud) Python noob所以我可能会以错误的方式解决这个问题
我想使用try/except块来查找dict中的值是否未设置为
try:
if entry['VALUE1'] == None: void()
if entry['VALUE2'] == None: void()
except KeyError:
print "Values not Found"
Run Code Online (Sandbox Code Playgroud)
当然'void'函数不存在,我可以做些什么来解决这个问题,以便代码能够正常工作
如果我创建一个dicts的词典
mydict = {
'zstuff':{
'zzz':True,
'aaa':True
},
'astuff':{
'zzz':True,
'aaa':True
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用'for'来循环切换
for key in mydict:
for tag in mydict[key]:
print "{}:{}".format(key,tag)
Run Code Online (Sandbox Code Playgroud)
Python输出
zstuff:aaa
zstuff:zzz
astuff:aaa
astuff:zzz
Run Code Online (Sandbox Code Playgroud)
有没有办法让Python不重新排序第二级dicts?
python ×7
mongodb ×2
dictionary ×1
encoding ×1
facebook ×1
filereader ×1
function ×1
gatling ×1
import ×1
java ×1
javascript ×1
jpa ×1
mongoengine ×1
regex ×1
sql-server ×1
ssms ×1
tornado ×1
upload ×1
void ×1
web-crawler ×1