我正在尝试使用python中的请求库向服务器发送POST调用.之前我能够成功发送POST调用,但最近,服务器弃用了TLSv1.0,现在只支持TLSv1.1和TLSv1.2.现在相同的代码抛出一个"requests.exceptions.SSLError:EOF发生违反协议(_ssl.c:590)"错误.
我在stackoverflow上发现了这个线程Python请求requests.exceptions.SSLError:[Errno 8] _ssl.c:504:EOF违反了协议,这表示我们需要继承HTTPAdapter,之后会话对象将使用TLSv1.我相应地更改了我的代码,这是我的新代码
class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_TLSv1)
url="https://mywebsite.com/ui/"
headers={"Cookie":"some_value","X-CSRF-Token":"some value","Content-Type":"application/json"}
payload={"name":"some value","Id":"some value"}
s = requests.Session()
s.mount('https://', MyAdapter())
r=s.post(url,json=payload,headers=headers)
html=r.text
print html
Run Code Online (Sandbox Code Playgroud)
但即使使用了这个,我也得到了同样的错误"违反协议的EOF(_ssl.c:590)".
我的第一个问题是,我在某处读取默认使用ssl的请求.我知道我的服务器使用的是TLSv1.0,我的代码是否正常工作,因为TLSv1.0与ssl3.0向后兼容?
我的第二个问题是,我上面提到的stackoverflow线程使用了我将我的代码更改为子类HTTPAdapter,表示这将适用于TLSv1.但是由于我的服务器中不推荐使用TLSv1.0,这段代码仍然有效吗?
使用 python,我并行启动两个子进程。一个是 HTTP 服务器,另一个是另一个程序的执行(CustomSimpleHTTPServer.py,这是一个由 selenium IDE 插件生成的 Python 脚本,用于打开 Firefox、导航到网站并进行一些交互)。另一方面,当第二个子进程完成执行时,我想停止执行第一个子进程(HTTP 服务器)。
我的代码的逻辑是 selenium 脚本会打开一个网站。该网站将自动对我的 HTTP 服务器进行几次 GET 调用。在 selenium 脚本执行完成后,应该关闭 HTTP 服务器,以便它可以将所有捕获的请求记录在一个文件中。
这是我的主要 Python 代码:
class Myclass(object):
HTTPSERVERPROCESS = ""
def startHTTPServer(self):
print "********HTTP Server started*********"
try:
self.HTTPSERVERPROCESS=subprocess.Popen('python CustomSimpleHTTPServer.py', \
shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
except Exception as e:
print "Exception captured while starting HTTP Server process: %s\n" % e
def startNavigatingFromBrowser(self):
print "********Opening firefox to start navigation*********"
try:
process=subprocess.Popen('python navigationScript.py', \
shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
process.communicate()
process.wait()
except Exception as e:
print "Exception …Run Code Online (Sandbox Code Playgroud)